Account migration funktioniert nun
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
import { AuthOptions, getServerSession as getNextAuthServerSession } from "next-auth";
|
||||
import { PrismaAdapter } from "@next-auth/prisma-adapter";
|
||||
import Credentials from "next-auth/providers/credentials";
|
||||
import { prisma } from "@repo/db";
|
||||
import { DiscordAccount, prisma, User } from "@repo/db";
|
||||
import bcrypt from "bcryptjs";
|
||||
import oldUser from "./var.User.json";
|
||||
import { OldUser } from "../../../../types/oldUser";
|
||||
|
||||
export const options: AuthOptions = {
|
||||
providers: [
|
||||
@@ -14,9 +16,59 @@ export const options: AuthOptions = {
|
||||
async authorize(credentials) {
|
||||
try {
|
||||
if (!credentials) throw new Error("No credentials provided");
|
||||
const user = await prisma.user.findFirstOrThrow({
|
||||
const user = await prisma.user.findFirst({
|
||||
where: { email: credentials.email },
|
||||
});
|
||||
const v1User = (oldUser as OldUser[]).find((u) => u.email === credentials.email);
|
||||
if (!user && v1User) {
|
||||
if (bcrypt.compareSync(credentials.password, v1User.password)) {
|
||||
const newUser = await prisma.user.create({
|
||||
data: {
|
||||
email: v1User.email,
|
||||
password: v1User.password,
|
||||
migratedFromV1: true,
|
||||
firstname: v1User.firstname,
|
||||
lastname: v1User.lastname,
|
||||
publicId: v1User.publicId,
|
||||
badges: [
|
||||
...v1User.badges
|
||||
.map((badge) => {
|
||||
switch (badge) {
|
||||
case "day-1-member":
|
||||
return "DAY1";
|
||||
case "d-1":
|
||||
return "D1";
|
||||
case "p-1":
|
||||
return "P1";
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
})
|
||||
.filter((badge) => badge !== null),
|
||||
"V1Veteran",
|
||||
],
|
||||
},
|
||||
});
|
||||
if (v1User.discord) {
|
||||
await prisma.discordAccount.create({
|
||||
data: {
|
||||
tokenType: "Bearer",
|
||||
refreshToken: v1User.discord.tokens.refresh_token,
|
||||
discordId: v1User.discord.profile.id,
|
||||
userId: newUser.id,
|
||||
username: v1User.discord.profile.username,
|
||||
globalName: v1User.discord.profile.global_name,
|
||||
avatar: v1User.discord.profile.avatar,
|
||||
email: v1User.discord.profile.email,
|
||||
verified: v1User.discord.profile.verified,
|
||||
},
|
||||
});
|
||||
}
|
||||
return newUser;
|
||||
}
|
||||
}
|
||||
if (!user) return null;
|
||||
|
||||
if (bcrypt.compareSync(credentials.password, user.password)) {
|
||||
return user;
|
||||
}
|
||||
|
||||
749736
apps/hub/app/api/auth/[...nextauth]/var.User.json
Normal file
749736
apps/hub/app/api/auth/[...nextauth]/var.User.json
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user