next.js + discord oauth2
Answered
Yakutian Laika posted this in #help-forum
Yakutian LaikaOP
Hello, I just deployed an app in vercel, and it just has the bare minimum code T3 code. Right now I am just trying to get the discord authentication to work --which it does in development with localhost. However, when trying to sign in to discord in my vercel app, I receive the error: "Try signing in with a different account."
I have double checked everything from all the environmental variables (DISCORD_CLIENT_ID, DISCORD_CLIENT_SECRET, NEXTAUTH_URL, NEXTAUTH_SECRET, DATABASE_URL) to the redirect uri in discord, making sure to add "/api/auth/callback/discord" at the end of the vercel site's url. What else should I be checking to see why this isn't working? Thank you!
I have double checked everything from all the environmental variables (DISCORD_CLIENT_ID, DISCORD_CLIENT_SECRET, NEXTAUTH_URL, NEXTAUTH_SECRET, DATABASE_URL) to the redirect uri in discord, making sure to add "/api/auth/callback/discord" at the end of the vercel site's url. What else should I be checking to see why this isn't working? Thank you!
19 Replies
Please provide the code
From what I understand that it tells that you are trying to sign in with a different account
I don't know how you implemented
But you could be trying to login in with a different discord account or the account is not registered in your database
So I will need access to the code to investigate further
After some research, I found this similar issue which can solve your problem
It was giving him the same error but he was using github not discord
@Bashamega https://github.com/nextauthjs/next-auth/issues/3830
Yakutian LaikaOP
thanks for your response! So it looks like their issue had to do with a unique constraint in the database -the user they were trying to log in already existed. which unfortunately isn't the case for me
Here is the auth.ts file:
/**
* Module augmentation for `next-auth` types. Allows us to add custom properties to the `session`
* object and keep type safety.
*
* @see https://next-auth.js.org/getting-started/typescript#module-augmentation
*/
declare module "next-auth" {
interface Session extends DefaultSession {
user: DefaultSession["user"] & {
id: string;
// ...other properties
// role: UserRole;
};
}
// interface User {
// // ...other properties
// // role: UserRole;
// }
}
/**
* Options for NextAuth.js used to configure adapters, providers, callbacks, etc.
*
* @see https://next-auth.js.org/configuration/options
*/
export const authOptions: NextAuthOptions = {
callbacks: {
session: ({ session, user }) => ({
...session,
user: {
...session.user,
id: user.id,
},
}),
},
adapter: PrismaAdapter(db),
providers: [
DiscordProvider({
clientId: env.DISCORD_CLIENT_ID,
clientSecret: env.DISCORD_CLIENT_SECRET,
}),
/**
* ...add more providers here.
*
* Most other providers require a bit more work than the Discord provider. For example, the
* GitHub provider requires you to add the `refresh_token_expires_in` field to the Account
* model. Refer to the NextAuth.js docs for the provider you want to use. Example:
*
* @see https://next-auth.js.org/providers/github
*/
],
};
/**
* Wrapper for `getServerSession` so that you don't need to import the `authOptions` in every file.
*
* @see https://next-auth.js.org/configuration/nextjs
*/
export const getServerAuthSession = (ctx: {
req: GetServerSidePropsContext["req"];
res: GetServerSidePropsContext["res"];
}) => {
return getServerSession(ctx.req, ctx.res, authOptions);
};Here is the schema.prisma file:
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
relationMode = "prisma"
}
model Example {
id Int @id @default(autoincrement())
name String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@index([name])
}
// Necessary for Next auth
model Account {
id String @id @default(cuid())
userId String
type String
provider String
providerAccountId String
refresh_token String? @db.Text
access_token String? @db.Text
expires_at Int?
token_type String?
scope String?
id_token String? @db.Text
session_state String?
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@unique([provider, providerAccountId])
@@index([userId])
}
model Session {
id String @id @default(cuid())
sessionToken String @unique
userId String
expires DateTime
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@index([userId])
}
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
}
model VerificationToken {
identifier String
token String @unique
expires DateTime
@@unique([identifier, token])
}but a possible reasons since you told it was working on local host is that you forgot to update the discord app uri
So I suggest opening the https://discord.com/developers/ and changing the redirecting URI
Yakutian LaikaOP
That would certainly be a reason for it not working, but I in fact did! :/
Yakutian LaikaOP
fixed it by installing some dependencies haha!
Answer
can you please close this post