-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: colocate auth stuff and bump some stuff (#1136)
* chore: bump dep and remove unstable * refactor auth setup * fixy * whoops * fix * use new recommended way to setup prisma * rm async/await ref #1129 * docs
- Loading branch information
1 parent
32d2fac
commit 715f6e8
Showing
16 changed files
with
276 additions
and
254 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
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import NextAuth from "next-auth"; | ||
import { authOptions } from "../../../server/auth"; | ||
|
||
export default NextAuth(authOptions); |
34 changes: 0 additions & 34 deletions
34
cli/template/extras/src/pages/api/auth/[...nextauth]/base.ts
This file was deleted.
Oops, something went wrong.
38 changes: 0 additions & 38 deletions
38
cli/template/extras/src/pages/api/auth/[...nextauth]/with-prisma.ts
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import type { GetServerSidePropsContext } from "next"; | ||
import { | ||
getServerSession, | ||
type NextAuthOptions, | ||
type DefaultSession, | ||
} from "next-auth"; | ||
import DiscordProvider from "next-auth/providers/discord"; | ||
import { env } from "../env/server.mjs"; | ||
|
||
/** | ||
* Module augmentation for `next-auth` types | ||
* Allows us to add custom properties to the `session` object | ||
* and keep type safety | ||
* @see https://next-auth.js.org/getting-started/typescript#module-augmentation | ||
**/ | ||
declare module "next-auth" { | ||
interface Session extends DefaultSession { | ||
user: { | ||
id: string; | ||
// ...other properties | ||
// role: UserRole; | ||
} & DefaultSession["user"]; | ||
} | ||
|
||
// interface User { | ||
// // ...other properties | ||
// // role: UserRole; | ||
// } | ||
} | ||
|
||
/** | ||
* Options for NextAuth.js used to configure | ||
* adapters, providers, callbacks, etc. | ||
* @see https://next-auth.js.org/configuration/options | ||
**/ | ||
export const authOptions: NextAuthOptions = { | ||
callbacks: { | ||
session({ session, user }) { | ||
if (session.user) { | ||
session.user.id = user.id; | ||
// session.user.role = user.role; <-- put other properties on the session here | ||
} | ||
return session; | ||
}, | ||
}, | ||
providers: [ | ||
DiscordProvider({ | ||
clientId: env.DISCORD_CLIENT_ID, | ||
clientSecret: env.DISCORD_CLIENT_SECRET, | ||
}), | ||
/** | ||
* ...add more providers here | ||
* | ||
* Most other providers require a bit more work than the Discord provider. | ||
* For example, the GitHub provider requires you to add the | ||
* `refresh_token_expires_in` field to the Account model. Refer to the | ||
* NextAuth.js docs for the provider you want to use. Example: | ||
* @see https://next-auth.js.org/providers/github | ||
**/ | ||
], | ||
}; | ||
|
||
/** | ||
* Wrapper for getServerSession so that you don't need | ||
* to import the authOptions in every file. | ||
* @see https://next-auth.js.org/configuration/nextjs | ||
**/ | ||
export const getServerAuthSession = (ctx: { | ||
req: GetServerSidePropsContext["req"]; | ||
res: GetServerSidePropsContext["res"]; | ||
}) => { | ||
return getServerSession(ctx.req, ctx.res, authOptions); | ||
}; |
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,76 @@ | ||
import type { GetServerSidePropsContext } from "next"; | ||
import { | ||
getServerSession, | ||
type NextAuthOptions, | ||
type DefaultSession, | ||
} from "next-auth"; | ||
import DiscordProvider from "next-auth/providers/discord"; | ||
import { PrismaAdapter } from "@next-auth/prisma-adapter"; | ||
import { env } from "../env/server.mjs"; | ||
import { prisma } from "./db"; | ||
|
||
/** | ||
* Module augmentation for `next-auth` types | ||
* Allows us to add custom properties to the `session` object | ||
* and keep type safety | ||
* @see https://next-auth.js.org/getting-started/typescript#module-augmentation | ||
**/ | ||
declare module "next-auth" { | ||
interface Session extends DefaultSession { | ||
user: { | ||
id: string; | ||
// ...other properties | ||
// role: UserRole; | ||
} & DefaultSession["user"]; | ||
} | ||
|
||
// interface User { | ||
// // ...other properties | ||
// // role: UserRole; | ||
// } | ||
} | ||
|
||
/** | ||
* Options for NextAuth.js used to configure | ||
* adapters, providers, callbacks, etc. | ||
* @see https://next-auth.js.org/configuration/options | ||
**/ | ||
export const authOptions: NextAuthOptions = { | ||
callbacks: { | ||
session({ session, user }) { | ||
if (session.user) { | ||
session.user.id = user.id; | ||
// session.user.role = user.role; <-- put other properties on the session here | ||
} | ||
return session; | ||
}, | ||
}, | ||
adapter: PrismaAdapter(prisma), | ||
providers: [ | ||
DiscordProvider({ | ||
clientId: env.DISCORD_CLIENT_ID, | ||
clientSecret: env.DISCORD_CLIENT_SECRET, | ||
}), | ||
/** | ||
* ...add more providers here | ||
* | ||
* Most other providers require a bit more work than the Discord provider. | ||
* For example, the GitHub provider requires you to add the | ||
* `refresh_token_expires_in` field to the Account model. Refer to the | ||
* NextAuth.js docs for the provider you want to use. Example: | ||
* @see https://next-auth.js.org/providers/github | ||
**/ | ||
], | ||
}; | ||
|
||
/** | ||
* Wrapper for getServerSession so that you don't need | ||
* to import the authOptions in every file. | ||
* @see https://next-auth.js.org/configuration/nextjs | ||
**/ | ||
export const getServerAuthSession = (ctx: { | ||
req: GetServerSidePropsContext["req"]; | ||
res: GetServerSidePropsContext["res"]; | ||
}) => { | ||
return getServerSession(ctx.req, ctx.res, authOptions); | ||
}; |
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.
715f6e8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
create-t3-app – ./
create-t3-app-git-next-t3-oss.vercel.app
beta.create.t3.gg
create.t3.gg
www.ct3.app
create-t3-app-nu.vercel.app
create-t3-app-t3-oss.vercel.app
ct3.app