Add account management settings
This commit is contained in:
19
server/api/auth/me.patch.ts
Normal file
19
server/api/auth/me.patch.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { readBody, createError } from 'h3'
|
||||
import { prisma } from '../../utils/db'
|
||||
import { requireUser } from '../../utils/auth'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const user = await requireUser(event)
|
||||
const body = await readBody<{ name?: string }>(event)
|
||||
const name = body.name?.trim()
|
||||
|
||||
if (!name || name.length > 40) {
|
||||
throw createError({ statusCode: 400, statusMessage: 'Invalid name' })
|
||||
}
|
||||
|
||||
return prisma.user.update({
|
||||
where: { id: user.id },
|
||||
data: { name },
|
||||
select: { id: true, email: true, name: true }
|
||||
})
|
||||
})
|
||||
27
server/api/auth/password.post.ts
Normal file
27
server/api/auth/password.post.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { compare, hash } from 'bcryptjs'
|
||||
import { readBody, createError } from 'h3'
|
||||
import { prisma } from '../../utils/db'
|
||||
import { requireUser } from '../../utils/auth'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const sessionUser = await requireUser(event)
|
||||
const body = await readBody<{ currentPassword?: string; nextPassword?: string }>(event)
|
||||
const currentPassword = body.currentPassword || ''
|
||||
const nextPassword = body.nextPassword || ''
|
||||
|
||||
if (nextPassword.length < 8) {
|
||||
throw createError({ statusCode: 400, statusMessage: 'Password must be at least 8 characters' })
|
||||
}
|
||||
|
||||
const user = await prisma.user.findUnique({ where: { id: sessionUser.id } })
|
||||
if (!user || !(await compare(currentPassword, user.passwordHash))) {
|
||||
throw createError({ statusCode: 401, statusMessage: 'Current password is invalid' })
|
||||
}
|
||||
|
||||
await prisma.user.update({
|
||||
where: { id: user.id },
|
||||
data: { passwordHash: await hash(nextPassword, 10) }
|
||||
})
|
||||
|
||||
return { ok: true }
|
||||
})
|
||||
Reference in New Issue
Block a user