Next.js Discord

Discord Forum

Bug with next-auth and Twitter

Unanswered
Beveren posted this in #help-forum
Open in Discord
BeverenOP
Hi,
I'm using Next-Auth to log in with Twitter but I got a bug and I don't know how to fix it

When I click to the sign in button it redirects me to this URL : /api/auth/error and I got a 404 Error.
I can't even see the Twitter OAuth screen

I'll post my file structure and some of my files. Thanks for your help

3 Replies

BeverenOP
File Structure :
Here is my [...nextauth].js file:

import NextAuth from "next-auth";
import TwitterProvider from "next-auth/providers/twitter";

export default NextAuth({
  providers: [
    TwitterProvider({
      clientId: process.env.TWITTER_CLIENT_ID,
      clientSecret: process.env.TWITTER_CLIENT_SECRET,
    }),
  ],
  pages: {
    signIn: "/auth/signin",
    signOut: "/auth/signout",
    error: "/auth/error",
    verifyRequest: "/auth/verify-request",
    newUser: null,
  },
  callbacks: {
    async session({ session, token }) {
      session.user.id = token.id;
      session.user.accessToken = token.accessToken;
      return session;
    },
    async jwt({ token, account }) {
      if (account) {
        token.id = account.providerAccountId;
        token.accessToken = account.access_token;
      }
      return token;
    },
  },
});

UserSession.js
"use client";

import { useSession, signIn, signOut } from "next-auth/react";

export default function UserSession() {
  const { data: session } = useSession();

  if (session) {
    return (
      <>
        <h1>Welcome, {session.user.name}</h1>
        <button onClick={() => signOut()}>Sign out</button>
        <div>
          <h2>Your Referral Link</h2>
          <p>{`${process.env.NEXTAUTH_URL}/?ref=${session.user.id}`}</p>
        </div>
        {/* Add more components to handle referral checking */}
      </>
    );
  }

  return (
    <>
      <h1>Welcome to the Referral System</h1>
      <button onClick={() => signIn("twitter")}>Sign in with Twitter</button>
    </>
  );
}
i'm having the same issue, was anyone able to solve ?