Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix update password #1183

Merged
merged 2 commits into from
Aug 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions server/src/controllers/auth/updatePassword.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@ import { findUserAndUpdate } from '../../services/user/user.service';
import * as validator from '@packrat/validations';
import { hashPassword } from '../../utils/user';
import { type Context } from 'hono';
import { User } from '../../drizzle/methods/User';

export const updatePassword = async (c: Context) => {
try {
const { email, password } = await c.req.json();
const JWT_SECRET = c.env.JWT_SECRET;
const hashedPassword = await hashPassword(JWT_SECRET, password);
const user = await findUserAndUpdate(email, hashedPassword, 'password');
return c.json({ user }, 200);
const currentUser = await findUserAndUpdate(
email,
hashedPassword,
'password',
);
return c.json({ currentUser }, 200);
} catch (error) {
return c.json({ error: `Email Doesnt Exist: ${error.message}` }, 404);
}
Expand All @@ -23,8 +28,17 @@ export function updatePasswordRoute() {
const { email, password } = opts.input;
const { env }: any = opts.ctx;
const JWT_SECRET = env.JWT_SECRET;
const userClass = new User();
const user = await userClass.findByCredentials(email, password);
if (!user) {
throw new Error('Password is not correct');
}
const hashedPassword = await hashPassword(JWT_SECRET, password);
const user = await findUserAndUpdate(email, hashedPassword, 'password');
return user;
const currentUser = await findUserAndUpdate(
email,
hashedPassword,
'password',
);
return currentUser;
});
}
Loading