mirror of
https://github.com/PretendoNetwork/website.git
synced 2026-09-29 03:56:56 -05:00
feat: add password change from account page
This commit is contained in:
43
server/api/account/update-password.patch.ts
Normal file
43
server/api/account/update-password.patch.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { ClientError } from 'nice-grpc';
|
||||
import { PasswordUpdateSchema } from '~~/shared/api-types';
|
||||
import type { ApiErrorCodes } from '~~/shared/errors';
|
||||
|
||||
const bucket = createRatelimitBucket({
|
||||
id: 'update-password',
|
||||
points: 15,
|
||||
durationSec: 5 * 60, // 5 minutes
|
||||
blockDurationSec: 1 * 60 * 60 // 1 hour
|
||||
});
|
||||
|
||||
const errors: Record<string, ApiErrorCodes> = {
|
||||
'INVALID_ARGUMENT: Password must be between 6 and 16 characters long': 'INVALID_PASSWORD_LENGTH',
|
||||
'INVALID_ARGUMENT: Password cannot be the same as username': 'INVALID_PASSWORD_USERNAME',
|
||||
'INVALID_ARGUMENT: Password must have combination of letters, numbers, and/or punctuation characters': 'INVALID_PASSWORD_COMBOS',
|
||||
'INVALID_ARGUMENT: Password may not have 3 repeating characters': 'INVALID_PASSWORD_REPEATING',
|
||||
'INVALID_ARGUMENT: Passwords do not match': 'INVALID_PASSWORD_NO_MATCH',
|
||||
'INVALID_ARGUMENT: Password is incorrect': 'INVALID_PASSWORD'
|
||||
};
|
||||
|
||||
export default defineEventHandler(async (event): Promise<void> => {
|
||||
await enforceRatelimit(event, bucket);
|
||||
|
||||
const body = await readZodBody(event, PasswordUpdateSchema);
|
||||
const auth = enforceLoggedIn(event);
|
||||
const grpc = useApiGrpcWithToken(event, auth.token);
|
||||
|
||||
try {
|
||||
await grpc.updatePassword({
|
||||
oldPassword: body.oldPassword,
|
||||
newPassword: body.newPassword,
|
||||
newPasswordConfirm: body.newPasswordConfirm
|
||||
});
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof ClientError) {
|
||||
const errorCode = errors[error.details];
|
||||
if (errorCode) {
|
||||
throw createApiError(errorCode);
|
||||
}
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
@@ -21,10 +21,10 @@ const errors: Record<string, ApiErrorCodes> = {
|
||||
'INVALID_ARGUMENT: Two or more punctuation characters cannot be used in a row': 'USERNAME_INVALID_CHARS',
|
||||
'INVALID_ARGUMENT: PNID already in use': 'USERNAME_IN_USE',
|
||||
'INVALID_ARGUMENT: Mii name too long': 'MIINAME_TOO_LONG',
|
||||
'INVALID_ARGUMENT: Password must be between 6 and 16 characters long': 'INVALID_PASSWORD_INPUT',
|
||||
'INVALID_ARGUMENT: Password cannot be the same as username': 'INVALID_PASSWORD_INPUT',
|
||||
'INVALID_ARGUMENT: Password must have combination of letters, numbers, and/or punctuation characters': 'INVALID_PASSWORD_INPUT',
|
||||
'INVALID_ARGUMENT: Password may not have 3 repeating characters': 'INVALID_PASSWORD_INPUT',
|
||||
'INVALID_ARGUMENT: Password must be between 6 and 16 characters long': 'INVALID_PASSWORD_LENGTH',
|
||||
'INVALID_ARGUMENT: Password cannot be the same as username': 'INVALID_PASSWORD_USERNAME',
|
||||
'INVALID_ARGUMENT: Password must have combination of letters, numbers, and/or punctuation characters': 'INVALID_PASSWORD_COMBOS',
|
||||
'INVALID_ARGUMENT: Password may not have 3 repeating characters': 'INVALID_PASSWORD_REPEATING',
|
||||
'INVALID_ARGUMENT: Passwords do not match': 'INVALID_PASSWORD_NO_MATCH'
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user