Skip to content

Commit 6332a45

Browse files
committed
update user.password to be optional per mongoose schema
1 parent 57d38f7 commit 6332a45

3 files changed

Lines changed: 13 additions & 15 deletions

File tree

server/controllers/user.controller/authManagement.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -129,25 +129,16 @@ export const updateSettings: RequestHandler<
129129
res.status(401).json({ error: 'Current password is not provided.' });
130130
return;
131131
}
132-
132+
}
133+
if (req.body.currentPassword) {
133134
const isMatch = await user.comparePassword(req.body.currentPassword);
134135
if (!isMatch) {
135136
res.status(401).json({ error: 'Current password is invalid.' });
136137
return;
137138
}
138-
user.password = req.body.newPassword;
139+
user.password = req.body.newPassword!;
139140
await saveUser(res, user);
140-
}
141-
// if (req.body.currentPassword) {
142-
// const isMatch = await user.comparePassword(req.body.currentPassword);
143-
// if (!isMatch) {
144-
// res.status(401).json({ error: 'Current password is invalid.' });
145-
// return;
146-
// }
147-
// user.password = req.body.newPassword!;
148-
// await saveUser(res, user);
149-
// }
150-
else if (user.email !== req.body.email) {
141+
} else if (user.email !== req.body.email) {
151142
const EMAIL_VERIFY_TOKEN_EXPIRY_TIME = Date.now() + 3600000 * 24; // 24 hours
152143
user.verified = User.EmailConfirmation().Sent;
153144

server/models/__test__/user.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('User model', () => {
3535
await user.save();
3636

3737
expect(user.password).not.toBe('mypassword');
38-
const match = await bcrypt.compare('mypassword', user.password);
38+
const match = await bcrypt.compare('mypassword', user.password!);
3939
expect(match).toBe(true);
4040
});
4141

server/types/user.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Error, GenericResponseBody, RouteParam } from './express';
99
export interface IUser extends VirtualId, MongooseTimestamps {
1010
name: string;
1111
username: string;
12-
password: string;
12+
password?: string;
1313
resetPasswordToken?: string;
1414
resetPasswordExpires?: number;
1515
verified?: string;
@@ -93,6 +93,13 @@ export type PublicUserOrErrorOrGeneric =
9393
| PublicUserOrError
9494
| GenericResponseBody;
9595

96+
export interface UpdateSettingsRequestBody {
97+
username: string;
98+
email: string;
99+
newPassword?: string;
100+
currentPassword?: string;
101+
}
102+
96103
/**
97104
* Response body used for unlinkGithub and unlinkGoogle
98105
* - If user is not logged in, a GenericResponseBody with 404 is returned

0 commit comments

Comments
 (0)