Compare commits

...

1 Commits

Author SHA1 Message Date
Paul Popus
1617347c0a feat: add new patch support on me endpoint so users can easily update their own data 2025-06-11 12:13:10 +01:00
2 changed files with 68 additions and 0 deletions

View File

@@ -7,6 +7,7 @@ import { initHandler } from './init.js'
import { loginHandler } from './login.js'
import { logoutHandler } from './logout.js'
import { meHandler } from './me.js'
import { patchMeHandler } from './patchMe.js'
import { refreshHandler } from './refresh.js'
import { registerFirstUserHandler } from './registerFirstUser.js'
import { resetPasswordHandler } from './resetPassword.js'
@@ -47,6 +48,11 @@ export const authCollectionEndpoints: Endpoint[] = wrapInternalEndpoints([
method: 'get',
path: '/me',
},
{
handler: patchMeHandler,
method: 'patch',
path: '/me',
},
{
handler: refreshHandler,
method: 'post',

View File

@@ -0,0 +1,62 @@
import { status as httpStatus } from 'http-status'
import type { PayloadHandler } from '../../config/types.js'
import { getRequestCollection } from '../../utilities/getRequestEntity.js'
import { headersWithCors } from '../../utilities/headersWithCors.js'
export const patchMeHandler: PayloadHandler = async (req) => {
const collection = getRequestCollection(req)
if (!req.user) {
return Response.json(
{
message: req.t('authentication:account'),
},
{
headers: headersWithCors({
headers: new Headers(),
req,
}),
status: httpStatus.UNAUTHORIZED,
},
)
}
if (!req.data) {
return Response.json(
{
message: req.t('error:missingRequiredData'),
},
{
headers: headersWithCors({
headers: new Headers(),
req,
}),
status: httpStatus.BAD_REQUEST,
},
)
}
const updatedUser = await req.payload.update({
id: req.user.id,
collection: collection.config.slug,
data: req.data,
overrideAccess: false, // we don't want to override access here, as this is a user patching their own data
req,
})
return Response.json(
{
...updatedUser,
message: req.t('general:success'),
},
{
headers: headersWithCors({
headers: new Headers(),
req,
}),
status: httpStatus.OK,
},
)
}