Next.js Discord

Discord Forum

getServerSession over getSession

Unanswered
Atlantic menhaden posted this in #help-forum
Open in Discord
Atlantic menhadenOP
i''m trying to set up getServerSession as when i used getSession it didn't allow for post requests but i'm receiving this error:

error - pages/api/users/@me/guilds/[guild].js (7:43) @ req
error - TypeError: (0 , next_auth_reactWEBPACK_IMPORTED_MODULE_1.getServerSession) is not a function.


Here is my code:

import NextAuth from 'next-auth';
import DiscordProvider from 'next-auth/providers/discord';

export default NextAuth({
  providers: [
    DiscordProvider({
      clientId: process.env.DISCORD_CLIENT_ID,
      clientSecret: process.env.DISCORD_CLIENT_SECRET,
      authorization: { params: { scope: 'identify guilds email guilds.join' } },
    }),
  ],
  callbacks: {
    async jwt({ token, account }) {
      if (account) {
        token.accessToken = account.access_token;
        const apiUrl = `https://discord.com/api/v9/guilds/1065917561146974268/members/${account.providerAccountId}`;
        fetch(apiUrl, {
          method: 'PUT',
          headers: {
            'Authorization': `Bot ${process.env.DISCORD_TOKEN}`,
            'Content-Type': 'application/json',
          },
          body: JSON.stringify({
            access_token: account.access_token,
          }),
        })
          .then(response => {
            if (!response.ok) {
              throw new Error(`Failed to add user to guild: ${response.statusText}`);
            }
            console.log('User added to guild successfully.');
          })
          .catch(error => {
            console.error(error);
          });
      }
      return token
    },
    async session({ session, token }) {
      session.user.id = token.sub;
      session.accessToken = token.accessToken;
      return session
    },
  },
})

import axios from 'axios';
import { getServerSession } from 'next-auth/react';
import NextAuth from 'pages/api/auth/[...nextauth]';

export default async function handler(req, res) {

    const session = await getServerSession(req, res, NextAuth);

    console.log(session)
    const { user: currentUser } = session;
    const { id: user } = currentUser
    const { guild } = req.query

1 Reply

Atlantic menhadenOP
update: i'm using getToken() now and that does more than enough but would still be interested in knowing why that was happening