61 lines
1.2 KiB
TypeScript
61 lines
1.2 KiB
TypeScript
"use server";
|
|
import { prisma, Prisma } from "@repo/db";
|
|
import { getServerSession } from "../../api/auth/[...nextauth]/auth";
|
|
import bcrypt from "bcryptjs";
|
|
|
|
export const unlinkDiscord = async (userId: string) => {
|
|
const discordAccount = await prisma.discordAccount.update({
|
|
where: {
|
|
userId: userId,
|
|
},
|
|
data: {
|
|
userId: null,
|
|
},
|
|
});
|
|
|
|
await prisma.formerDiscordAccount.create({
|
|
data: {
|
|
userId,
|
|
discordId: discordAccount.discordId,
|
|
},
|
|
});
|
|
};
|
|
|
|
export const updateUser = async (changes: Prisma.UserUpdateInput) => {
|
|
const session = await getServerSession();
|
|
if (!session) return null;
|
|
|
|
await prisma.user.update({
|
|
where: {
|
|
id: session.user.id,
|
|
},
|
|
data: changes,
|
|
});
|
|
};
|
|
|
|
export const changePassword = async (oldPassword: string, newPassword: string) => {
|
|
const session = await getServerSession();
|
|
if (!session)
|
|
return {
|
|
error: "User not found",
|
|
};
|
|
|
|
if (!(await bcrypt.compare(oldPassword, session.user.password)))
|
|
return {
|
|
error: "Old password is incorrect",
|
|
};
|
|
|
|
const hashedPassword = await bcrypt.hash(newPassword, 12);
|
|
await prisma.user.update({
|
|
data: {
|
|
password: hashedPassword,
|
|
},
|
|
where: {
|
|
id: session.user.id,
|
|
},
|
|
});
|
|
return {
|
|
success: true,
|
|
};
|
|
};
|