-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move next auth into own package (#27)
- Loading branch information
1 parent
bdb491a
commit fb4e529
Showing
15 changed files
with
521 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import { handlers } from "~/auth"; // Referring to the auth.ts we just created | ||
import { handlers } from "@sovoli/auth"; | ||
|
||
export const { GET, POST } = handlers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export { auth as middleware } from "~/auth"; | ||
export { auth as middleware } from "@sovoli/auth"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# `@sovoli/services` | ||
|
||
A collection of external services that we use in our app. | ||
|
||
## Services | ||
|
||
- [Google Books API](https://developers.google.com/books/docs/overview) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import baseConfig, { restrictEnvAccess } from "@sovoli/eslint-config/base"; | ||
|
||
/** @type {import('typescript-eslint').Config} */ | ||
export default [ | ||
{ | ||
ignores: ["dist/**"], | ||
}, | ||
...baseConfig, | ||
//...restrictEnvAccess, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
{ | ||
"name": "@sovoli/auth", | ||
"version": "0.1.0", | ||
"private": true, | ||
"type": "module", | ||
"exports": { | ||
".": { | ||
"react-server": "./src/index.rsc.ts", | ||
"default": "./src/index.ts" | ||
} | ||
}, | ||
"license": "MIT", | ||
"scripts": { | ||
"clean": "git clean -xdf .cache .turbo dist node_modules", | ||
"format": "prettier --check . --ignore-path ../../.gitignore", | ||
"lint": "eslint", | ||
"typecheck": "tsc --noEmit" | ||
}, | ||
"dependencies": { | ||
"@sovoli/db": "workspace:*", | ||
"@auth/core": "0.34.2", | ||
"@auth/drizzle-adapter": "1.4.2", | ||
"@t3-oss/env-nextjs": "0.11.0", | ||
"next": "14.2.5", | ||
"next-auth": "5.0.0-beta.20", | ||
"react": "18.2.0", | ||
"react-dom": "18.2.0", | ||
"zod": "catalog:" | ||
}, | ||
"devDependencies": { | ||
"@sovoli/eslint-config": "workspace:*", | ||
"@sovoli/prettier-config": "workspace:*", | ||
"@sovoli/typescript-config": "workspace:*", | ||
"eslint": "catalog:", | ||
"prettier": "catalog:", | ||
"typescript": "catalog:" | ||
}, | ||
"prettier": "@sovoli/prettier-config" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import type { DefaultSession, NextAuthConfig, Session as NextAuthSession } from "next-auth"; | ||
import { DrizzleAdapter } from "@auth/drizzle-adapter"; | ||
import { db } from "@sovoli/db"; | ||
// import { | ||
// accounts, | ||
// sessions, | ||
// users, | ||
// verificationTokens, | ||
// } from "@sovoli/db/schema"; | ||
import Resend from "next-auth/providers/resend"; | ||
|
||
|
||
const adapter = DrizzleAdapter(db); | ||
|
||
export const authConfig: NextAuthConfig = { | ||
adapter, | ||
providers: [ | ||
Resend({ | ||
from: "[email protected]", | ||
}), | ||
], | ||
callbacks: { | ||
session({ session, user }) { | ||
// `session.user.address` is now a valid property, and will be type-checked | ||
// in places like `useSession().data.user` or `auth().user` | ||
return { | ||
...session, | ||
user: { | ||
...session.user, | ||
username: user.username, | ||
}, | ||
}; | ||
}, | ||
}, | ||
} | ||
|
||
export const validateToken = async ( | ||
token: string, | ||
): Promise<NextAuthSession | null> => { | ||
const sessionToken = token.slice("Bearer ".length); | ||
const session = await adapter.getSessionAndUser?.(sessionToken); | ||
return session | ||
? { | ||
user: { | ||
...session.user, | ||
}, | ||
expires: session.session.expires.toISOString(), | ||
} | ||
: null; | ||
}; | ||
|
||
|
||
declare module "next-auth" { | ||
interface User { | ||
username: string; | ||
} | ||
interface Session { | ||
user: { | ||
username: string; | ||
} & DefaultSession["user"]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { cache } from "react"; | ||
import NextAuth from "next-auth"; | ||
|
||
import { authConfig } from "./config"; | ||
|
||
export type { Session } from "next-auth"; | ||
|
||
const { handlers, auth: defaultAuth, signIn, signOut } = NextAuth(authConfig); | ||
|
||
/** | ||
* This is the main way to get session data for your RSCs. | ||
* This will de-duplicate all calls to next-auth's default `auth()` function and only call it once per request | ||
*/ | ||
const auth = cache(defaultAuth); | ||
|
||
export { handlers, auth, signIn, signOut }; | ||
|
||
export { | ||
validateToken, | ||
} from "./config"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import NextAuth from "next-auth"; | ||
|
||
import { authConfig } from "./config"; | ||
|
||
export type { Session } from "next-auth"; | ||
|
||
const { handlers, auth, signIn, signOut } = NextAuth(authConfig); | ||
|
||
export { handlers, auth, signIn, signOut }; | ||
|
||
export { | ||
validateToken, | ||
} from "./config"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"extends": "@sovoli/typescript-config/base.json", | ||
"include": ["src"], | ||
"exclude": ["node_modules"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.