Guest User

NextAuth

a guest
May 2nd, 2024
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import NextAuth, { NextAuthOptions } from "next-auth";
  2. import DiscordProvider from "next-auth/providers/discord";
  3.  
  4. // https://discord.com/developers/docs/topics/oauth2#shared-resources-oauth2-scopes
  5. const scopes = ["identify", "guilds"].join(" ");
  6.  
  7. export const authOptions: NextAuthOptions = {
  8.   session: {
  9.     strategy: "jwt",
  10.   },
  11.  
  12.   providers: [
  13.     DiscordProvider({
  14.       clientId: String(process.env.DISCORD_CLIENT_ID),
  15.       clientSecret: String(process.env.DISCORD_CLIENT_SECRET),
  16.       authorization: {
  17.         params: { scope: scopes.concat(" guilds.members.read") },
  18.       },
  19.  
  20.       async profile(profile, tokens) {
  21.         let isAuthorized = false;
  22.         let isAdult = false;
  23.  
  24.         // Fetch the list of servers the user is a member of
  25.         const response = await fetch(
  26.           "https://discord.com/api/users/@me/guilds",
  27.           {
  28.             headers: {
  29.               Authorization: `Bearer ${tokens.access_token}`,
  30.             },
  31.           }
  32.         );
  33.  
  34.         const guilds = await response.json();
  35.  
  36.         // Check if the user is a member of the target server
  37.         const targetGuild = guilds.find(
  38.           (guild: any) => guild.id === process.env.DISCORD_SERVER_ID
  39.         );
  40.  
  41.         if (targetGuild) {
  42.           // If the user is a member of the target server, they are authorized
  43.           isAuthorized = true;
  44.  
  45.           // Fetch the member data from our auth server
  46.           const memberResponse = await fetch(
  47.             `https://discord.com/api/users/@me/guilds/${process.env.DISCORD_SERVER_ID}/member`,
  48.             {
  49.               headers: {
  50.                 Authorization: `Bearer ${tokens.access_token}`,
  51.               },
  52.             }
  53.           );
  54.  
  55.           const memberData = await memberResponse.json();
  56.  
  57.           // Check if the member has the 'adult' role
  58.           // isAdult =
  59.           //   isAuthorized &&
  60.           //   memberData.roles.includes(process.env.DISCORD_ADULT_ROLE)
  61.  
  62.           // Assign roles to profile
  63.         }
  64.  
  65.         return {
  66.           id: profile.id,
  67.           name: profile.username,
  68.           email: profile.email,
  69.           image: `https://cdn.discordapp.com/avatars/${profile.id}/${profile.avatar}.png`,
  70.           isAuthorized: isAuthorized,
  71.           // isAdult: isAdult,
  72.         };
  73.       },
  74.     }),
  75.   ],
  76.  
  77.   callbacks: {
  78.     async signIn({ account }: any) {
  79.       if (account.provider === "discord") {
  80.  
  81.         // Grant access only if the member has the required role
  82.         return false; // for now
  83.       }
  84.  
  85.       // Allow sign-in for other providers
  86.       return true;
  87.     },
  88.    
  89.     async jwt({ token, user }: any) {
  90.       return { ...token, ...user};
  91.     },
  92.     async session({ session, user, token }: any) {
  93.       // session.isAdult = token?.isAdult
  94.       return session;
  95.     },
  96.   },
  97. };
  98.  
  99. export default NextAuth(authOptions);
  100.  
Advertisement
Add Comment
Please, Sign In to add comment