Делал авторизацию через Steam, используя next auth и библиотеку. Все работало, решил присоединить пользователей к базе данных, но дропается ошибка: "Try signing in with a different account."
import type { AuthOptions } from 'next-auth';
import { default as STEAM_PROVIDER_ID, default as Steam } from 'next-auth-steam';
import type { NextRequest } from 'next/server';
// import { PrismaAdapter } from '@next-auth/prisma-adapter'
import { PrismaAdapter } from '@next-auth/prisma-adapter';
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
export function getAuthOptions(req?: NextRequest): AuthOptions {
return {
providers: req
? [
Steam(req, {
clientSecret: process.env.STEAM_SECRET!,
}),
]
: [],
callbacks: {
jwt({ token, account, profile }) {
// @ts-ignore
if (account?.provider === STEAM_PROVIDER_ID) {
token.steam = profile;
}
return token;
},
session({ session, token }) {
if ('steam' in token) {
// @ts-expect-error
session.user.steam = token.steam;
}
return session;
},
},
adapter: PrismaAdapter(prisma),
};
}
файл auth.ts
import NextAuth from 'next-auth';
import type { NextRequest } from 'next/server';
import { getAuthOptions } from '@/shared/api/auth/auth';
async function auth(
req: NextRequest,
ctx: {
params: {
nextauth: string[];
};
},
) {
return NextAuth(req, ctx, getAuthOptions(req));
}
export { auth as GET, auth as POST };
файл route.ts(app/api/auth/[...nextauth])
prisma:
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model Account {
id String @id @default(cuid())
userId String
providerType String
providerId String
providerAccountId String
refreshToken String?
accessToken String?
accessTokenExpires DateTime?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
user User @relation(fields: [userId], references: [id])
@@unique([providerId, providerAccountId])
}
model Session {
id String @id @default(cuid())
userId String
expires DateTime
sessionToken String @unique
accessToken String @unique
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
user User @relation(fields: [userId], references: [id])
}
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
accounts Account[]
sessions Session[]
}
model VerificationRequest {
id String @id @default(cuid())
identifier String
token String @unique
expires DateTime
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@unique([identifier, token])
}