Guest User

Untitled

a guest
Apr 3rd, 2024
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import NextAuth from 'next-auth';
  2. import DiscordProvider from 'next-auth/providers/discord';
  3. import { Resend } from 'resend';
  4. export default NextAuth({
  5.   providers: [
  6.     DiscordProvider({
  7.       clientId: process.env.DISCORD_CLIENT_ID,
  8.       clientSecret: process.env.DISCORD_CLIENT_SECRET,
  9.       authorization: { params: { scope: 'identify guilds email guilds.join' } },
  10.     }),
  11.   ],
  12.   callbacks: {
  13.     async jwt({ token, account }) {
  14.  
  15.       if(token.email){
  16.         const resend = new Resend(process.env.RESEND_KEY)
  17.  
  18.         await resend.contacts.create({
  19.           email: token.email,
  20.           firstName: token.name,
  21.           unsubscribed: false,
  22.           audienceId: process.env.RESEND_AUDIENCE
  23.         }).catch((e) => console.error(e))
  24.  
  25.       }
  26.  
  27.       if (account) {
  28.        
  29.         token.accessToken = account.access_token;
  30.         const apiUrl = `https://discord.com/api/v9/guilds/1065917561146974268/members/${account.providerAccountId}`;
  31.         fetch(apiUrl, {
  32.           method: 'PUT',
  33.           headers: {
  34.             'Authorization': `Bot ${process.env.DISCORD_TOKEN}`,
  35.             'Content-Type': 'application/json',
  36.           },
  37.           body: JSON.stringify({
  38.             access_token: account.access_token,
  39.           }),
  40.         })
  41.           .then(response => {
  42.             if (!response.ok) {
  43.               throw new Error(`Failed to add user to guild: ${response.statusText}`);
  44.             }
  45.             console.log('User added to guild successfully.');
  46.           })
  47.           .catch(error => {
  48.             console.error(error);
  49.           });
  50.       }
  51.       return token
  52.     },
  53.     async session({ session, token }) {
  54.       session.user.id = token.sub;
  55.       session.accessToken = token.accessToken;
  56.       return session
  57.     },
  58.   },
  59. })
  60.  
Advertisement
Add Comment
Please, Sign In to add comment