Skip to content

Commit

Permalink
refactor: use new IDAPI set username route
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphael Kabo committed Feb 7, 2024
1 parent 379d7d9 commit 3a943e8
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
19 changes: 19 additions & 0 deletions client/components/mma/identity/idapi/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { get } from 'lodash';
import {
addCSRFToken,
fetchWithDefaultParameters,
postRequest,
putRequest,
} from '@/client/utilities/fetch';
import type { User, UserError } from '../models';
Expand Down Expand Up @@ -194,3 +195,21 @@ export const read = async (): Promise<User> => {
).then((response) => response.json());
return toUser(response);
};

export const setUsername = async (user: Partial<User>): Promise<User> => {
const url = '/idapi/user/username';
const body = toUserApiRequest(user);
console.log('sending', body);
try {
const response: UserAPIResponse = await fetchWithDefaultParameters(
url,
addCSRFToken(postRequest(body)),
).then((response) => response.json());
if (isErrorResponse(response)) {
throw toUserError(response);
}
return toUser(response);
} catch (e) {
throw isErrorResponse(e) ? toUserError(e) : e;
}
};
3 changes: 3 additions & 0 deletions client/components/mma/identity/identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ export const Users: UserCollection = {
getChangedFields(original: User, changed: User): Partial<User> {
return diffWithCompositeFields(original, changed);
},
async setUsername(user: User): Promise<User> {
return await UserAPI.setUsername(user);
},
};

export const ConsentOptions: ConsentOptionCollection = {
Expand Down
1 change: 1 addition & 0 deletions client/components/mma/identity/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export interface UserCollection {
save: (user: User) => Promise<User>;
saveChanges: (original: User, changed: User) => Promise<User>;
getChangedFields: (original: User, changed: User) => Partial<User>;
setUsername: (user: User) => Promise<User>;
}

export interface ConsentOption {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ export const PublicProfile = (_: { path?: string }) => {
.catch(handleGeneralError);
}, []);

const saveUser = async (originalUser: User, values: User) => {
const changedUser = { ...originalUser, ...values };
return await Users.saveChanges(originalUser, changedUser);
};
const setUsername = async (values: User) => await Users.setUsername(values);

useEffect(() => {
if (error && errorRef.current) {
Expand Down Expand Up @@ -75,7 +72,7 @@ export const PublicProfile = (_: { path?: string }) => {
<>
<ProfileFormSection
user={u}
saveUser={(values) => saveUser(u, values)}
saveUser={(values) => setUsername(values)}
onError={handleGeneralError}
onSuccess={setUser}
/>
Expand Down
9 changes: 9 additions & 0 deletions server/routes/idapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ router.delete(
}),
);

router.post(
'/user/username',
csrfValidateMiddleware,
idapiProxyHandler({
url: '/user/me/username',
method: 'POST',
}),
);

// eslint-disable-next-line @typescript-eslint/no-explicit-any -- assume we don't know the range of possible types for the err argument?
router.use((err: any, _: Request, res: Response, next: NextFunction) => {
if (err.code && err.code === 'EBADCSRFTOKEN') {
Expand Down

0 comments on commit 3a943e8

Please sign in to comment.