Created
November 21, 2023 08:51
-
-
Save rumitr/4aa3738a562bba9c872d9cf9a2d5cedc to your computer and use it in GitHub Desktop.
next-auth
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
| //app/api/auth/[...nextauth]/route.ts | |
| import { authOptions } from '@/lib/auth'; | |
| import NextAuth from 'next-auth'; | |
| const handler = NextAuth(authOptions); | |
| export { handler as GET, handler as POST }; | |
| //lib/auth.ts | |
| import type { NextAuthOptions } from "next-auth"; | |
| import AppleProvider from "next-auth/providers/apple"; | |
| import GoogleProvider from "next-auth/providers/google"; | |
| import { BASE_URL } from "./constants"; | |
| import { generateAppleToken } from "./generateAppleToken"; | |
| let userData = { | |
| token: "", | |
| user_role: "", | |
| }; | |
| const fetchToken = async ({ | |
| provider, | |
| oauth_token, | |
| oauth_uid, | |
| }: { | |
| provider: string; | |
| oauth_token: string; | |
| oauth_uid: string; | |
| }) => { | |
| const url = `${BASE_URL}/login?oauth_token=${oauth_token}&oauth_uid=${oauth_uid}&oauth_provider=${provider}`; | |
| const response = await fetch(url, { | |
| method: "POST", | |
| body: JSON.stringify({ | |
| oauth_token, | |
| oauth_uid, | |
| oauth_provider: provider, | |
| }), | |
| }); | |
| const data = await response.json(); | |
| return data; | |
| }; | |
| export const authOptions: NextAuthOptions = { | |
| secret: process.env.NEXTAUTH_SECRET, | |
| debug: process.env.NODE_ENV !== "production" ?? false, | |
| providers: [ | |
| GoogleProvider({ | |
| clientId: process.env.GOOGLE_CLIENT_ID as string, | |
| clientSecret: process.env.GOOGLE_CLIENT_SECRET as string, | |
| }), | |
| AppleProvider({ | |
| clientId: process.env.APPLE_ID as string, | |
| clientSecret: generateAppleToken(), | |
| }), | |
| ], | |
| cookies: { | |
| callbackUrl: { | |
| name: `__Secure-next-auth.callback-url`, | |
| options: { | |
| httpOnly: false, | |
| sameSite: "none", | |
| path: "/", | |
| secure: true, | |
| }, | |
| }, | |
| pkceCodeVerifier: { | |
| name: "next-auth.pkce.code_verifier", | |
| options: { | |
| httpOnly: true, | |
| sameSite: "none", | |
| path: "/", | |
| secure: true, | |
| }, | |
| }, | |
| }, | |
| pages: { | |
| signIn: "/login", | |
| error: "/login", | |
| }, | |
| callbacks: { | |
| async signIn(args) { | |
| try { | |
| const response = await fetchToken({ | |
| provider: args.account?.provider as string, | |
| oauth_token: args.account?.id_token as string, | |
| oauth_uid: args.profile?.sub as string, | |
| }); | |
| if ( | |
| response.status === "error" | |
| // || response.data.user_role !== "coach" | |
| ) { | |
| return false; | |
| } | |
| userData = response.data; | |
| } catch (err) { | |
| console.log({ err }); | |
| return false; | |
| } | |
| return true; | |
| }, | |
| async jwt(args) { | |
| const { token, trigger, session } = args; | |
| if ( | |
| token && | |
| trigger === "signIn" && | |
| userData.token && | |
| userData.user_role | |
| ) { | |
| token.role = userData.user_role; | |
| token.accessToken = userData.token; | |
| userData = { | |
| token: "", | |
| user_role: "", | |
| }; | |
| } | |
| if (trigger === "update") { | |
| token.dashboard = session.dashboard; | |
| } | |
| return token; | |
| }, | |
| async session(args) { | |
| let { session, token } = args; | |
| const { role, accessToken } = token; | |
| return { | |
| ...session, | |
| user: { | |
| ...session.user, | |
| role, | |
| accessToken, | |
| dashboard: token.dashboard, | |
| }, | |
| }; | |
| }, | |
| async redirect({ url, baseUrl }) { | |
| return `${baseUrl}/dashboard`; | |
| }, | |
| }, | |
| }; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment