-
Notifications
You must be signed in to change notification settings - Fork 629
[Auth] Add support for next-auth integration #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
337b413
Add next and express builds to cache
adam-maj d486883
Add support for next auth
adam-maj 895e42d
Update to use config domain
adam-maj 7593716
Add changeset
adam-maj 891425e
Merge branch 'main' into am/next-auth
adam-maj 7ccd711
Merge branch 'main' into am/next-auth
adam-maj 2e921e8
Update contracts version
adam-maj cf0c177
Pin contracts to v3.1.3
adam-maj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,3 @@ | ||
| /express | ||
| /next | ||
| /next-auth | ||
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,80 @@ | ||
| import { ThirdwebAuthConfig } from "./types"; | ||
| import { ThirdwebSDK } from "@thirdweb-dev/sdk"; | ||
| import { serialize } from "cookie"; | ||
| import { NextApiRequest, NextApiResponse } from "next"; | ||
| import { NextAuthOptions, Session } from "next-auth"; | ||
| import CredentialsProvider from "next-auth/providers/credentials"; | ||
|
|
||
| export function ThirdwebAuth(cfg: ThirdwebAuthConfig) { | ||
| const sdk = ThirdwebSDK.fromPrivateKey(cfg.privateKey, "mainnet"); | ||
|
|
||
| const ThirdwebProvider = (req: NextApiRequest, res: NextApiResponse) => | ||
| CredentialsProvider({ | ||
| name: "ThirdwebAuth", | ||
| credentials: { | ||
| payload: { | ||
| label: "Payload", | ||
| type: "text", | ||
| placeholder: "", | ||
| }, | ||
| }, | ||
| async authorize({ payload }: any) { | ||
| try { | ||
| const parsed = JSON.parse(payload); | ||
| const token = await sdk.auth.generateAuthToken( | ||
| "thirdweb.com", | ||
| parsed | ||
| ); | ||
| const address = await sdk.auth.authenticate("thirdweb.com", token); | ||
|
|
||
| // Securely set httpOnly cookie on request to prevent XSS on frontend | ||
| // And set path to / to enable thirdweb_auth_token usage on all endpoints | ||
| res.setHeader( | ||
| "Set-Cookie", | ||
| serialize("thirdweb_auth_token", token, { | ||
| path: "/", | ||
| httpOnly: true, | ||
| secure: true, | ||
| sameSite: "strict", | ||
| }) | ||
| ); | ||
|
|
||
| return { address }; | ||
| } catch (err) { | ||
| return null; | ||
| } | ||
| }, | ||
| }); | ||
|
|
||
| const authOptions = (req: NextApiRequest, res: NextApiResponse) => | ||
| ({ | ||
| callbacks: { | ||
| async session({ session }) { | ||
| const token = req.cookies.thirdweb_auth_token || ""; | ||
| try { | ||
| const address = await sdk.auth.authenticate("thirdweb.com", token); | ||
| session.user = { ...session.user, address } as Session["user"]; | ||
| return session; | ||
| } catch { | ||
| return session; | ||
| } | ||
| }, | ||
| }, | ||
| events: { | ||
| signOut() { | ||
| res.setHeader( | ||
| "Set-Cookie", | ||
| serialize("thirdweb_auth_token", "", { | ||
| path: "/", | ||
| expires: new Date(Date.now() + 5 * 1000), | ||
| }) | ||
| ); | ||
| }, | ||
| }, | ||
| } as Omit<NextAuthOptions, "providers">); | ||
|
|
||
| return { | ||
| ThirdwebProvider, | ||
| authOptions, | ||
| }; | ||
| } |
This file contains hidden or 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 @@ | ||
| export type ThirdwebAuthConfig = { | ||
| privateKey: string; | ||
| domain: string; | ||
| }; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.