Created
July 24, 2023 06:24
-
-
Save maskaravivek/be13c79f638c317b4bb8600005c77b12 to your computer and use it in GitHub Desktop.
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
| // pages/api/auth/[...nextauth].js | |
| import NextAuth from "next-auth" | |
| import CognitoProvider from "next-auth/providers/cognito"; | |
| export const authOptions = { | |
| providers: [ | |
| CognitoProvider({ | |
| clientId: process.env.COGNITO_CLIENT_ID, | |
| clientSecret: process.env.COGNITO_CLIENT_SECRET, | |
| issuer: process.env.COGNITO_DOMAIN, | |
| idToken: true, | |
| name: 'Cognito', | |
| checks: 'nonce', | |
| }), | |
| ], | |
| callbacks: { | |
| async jwt({ token, account }) { | |
| if (account) { | |
| if (account['provider'] === 'cognito') { | |
| token.accessToken = account?.access_token; | |
| var tokenParsed = JSON.parse(Buffer.from(account.id_token.split('.')[1], 'base64').toString()); | |
| token.username = tokenParsed['cognito:username']; | |
| } | |
| } | |
| return token | |
| }, | |
| async session({ session, token }) { | |
| session.accessToken = token.accessToken | |
| session.username = token.username | |
| return session | |
| }, | |
| }, | |
| debug: process.env.NODE_ENV === 'development' ? true : false | |
| } | |
| export default NextAuth(authOptions) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment