implement update user, update password and delete account
This commit is contained in:
36
app/settings/mutations/change-password.ts
Normal file
36
app/settings/mutations/change-password.ts
Normal file
@ -0,0 +1,36 @@
|
||||
import { AuthenticationError, NotFoundError, resolver, SecurePassword } from "blitz";
|
||||
import { z } from "zod";
|
||||
|
||||
import db from "../../../db";
|
||||
import { authenticateUser } from "../../auth/mutations/login";
|
||||
import { password } from "../../auth/validations";
|
||||
|
||||
const Body = z.object({
|
||||
currentPassword: z.string(),
|
||||
newPassword: password,
|
||||
});
|
||||
|
||||
export default resolver.pipe(
|
||||
resolver.zod(Body),
|
||||
resolver.authorize(),
|
||||
async ({ currentPassword, newPassword }, ctx) => {
|
||||
const user = await db.user.findFirst({ where: { id: ctx.session.userId! } });
|
||||
if (!user) throw new NotFoundError();
|
||||
|
||||
try {
|
||||
await authenticateUser(user.email, currentPassword);
|
||||
} catch (error) {
|
||||
if (error instanceof AuthenticationError) {
|
||||
throw new Error("Current password is incorrect");
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
|
||||
const hashedPassword = await SecurePassword.hash(newPassword.trim());
|
||||
await db.user.update({
|
||||
where: { id: user.id },
|
||||
data: { hashedPassword },
|
||||
});
|
||||
},
|
||||
);
|
14
app/settings/mutations/delete-user.ts
Normal file
14
app/settings/mutations/delete-user.ts
Normal file
@ -0,0 +1,14 @@
|
||||
import { NotFoundError, resolver } from "blitz";
|
||||
|
||||
import db from "../../../db";
|
||||
import logout from "../../auth/mutations/logout";
|
||||
import deleteUserData from "../api/queue/delete-user-data";
|
||||
|
||||
export default resolver.pipe(resolver.authorize(), async (_ = null, ctx) => {
|
||||
const user = await db.user.findFirst({ where: { id: ctx.session.userId! } });
|
||||
if (!user) throw new NotFoundError();
|
||||
|
||||
await db.user.update({ where: { id: user.id }, data: { hashedPassword: "pending deletion" } });
|
||||
await deleteUserData.enqueue({ userId: user.id });
|
||||
await logout(null, ctx);
|
||||
});
|
25
app/settings/mutations/update-user.ts
Normal file
25
app/settings/mutations/update-user.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import { NotFoundError, resolver } from "blitz";
|
||||
import { z } from "zod";
|
||||
|
||||
import db from "../../../db";
|
||||
import notifyEmailChangeQueue from "../api/queue/notify-email-change";
|
||||
|
||||
const Body = z.object({
|
||||
email: z.string().email(),
|
||||
name: z.string(),
|
||||
});
|
||||
|
||||
export default resolver.pipe(resolver.zod(Body), resolver.authorize(), async ({ email, name }, ctx) => {
|
||||
const user = await db.user.findFirst({ where: { id: ctx.session.userId! } });
|
||||
if (!user) throw new NotFoundError();
|
||||
|
||||
const oldEmail = user.email;
|
||||
await db.user.update({
|
||||
where: { id: user.id },
|
||||
data: { email, name },
|
||||
});
|
||||
|
||||
if (oldEmail !== email) {
|
||||
// await notifyEmailChangeQueue.enqueue({ newEmail: email, oldEmail: user.email });
|
||||
}
|
||||
});
|
Reference in New Issue
Block a user