Guest User

Untitled

a guest
Apr 2nd, 2024
86
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.         token.accessToken = account.access_token;
  29.         const apiUrl = `https://discord.com/api/v9/guilds/1065917561146974268/members/${account.providerAccountId}`;
  30.         fetch(apiUrl, {
  31.           method: 'PUT',
  32.           headers: {
  33.             'Authorization': `Bot ${process.env.DISCORD_TOKEN}`,
  34.             'Content-Type': 'application/json',
  35.           },
  36.           body: JSON.stringify({
  37.             access_token: account.access_token,
  38.           }),
  39.         })
  40.           .then(response => {
  41.             if (!response.ok) {
  42.               throw new Error(`Failed to add user to guild: ${response.statusText}`);
  43.             }
  44.             console.log('User added to guild successfully.');
  45.           })
  46.           .catch(error => {
  47.             console.error(error);
  48.           });
  49.       }
  50.       return token
  51.     },
  52.     async session({ session, token }) {
  53.       session.user.id = token.sub;
  54.       session.accessToken = token.accessToken;
  55.       return session
  56.     },
  57.   },
  58. })
  59.  
Advertisement
Add Comment
Please, Sign In to add comment