Skip to content

Commit

Permalink
add automatic login after password reset + lint
Browse files Browse the repository at this point in the history
  • Loading branch information
trinity-y committed Nov 10, 2024
1 parent cb83106 commit d8263b3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
14 changes: 8 additions & 6 deletions backend/typescript/rest/authRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CookieOptions, Router } from "express";
import { CookieOptions, response, Router } from "express";

import { isAuthorizedByEmail, isAuthorizedByUserId } from "../middlewares/auth";
import {
Expand Down Expand Up @@ -118,19 +118,21 @@ authRouter.post(
},
);

// warnings, it doesnt generate a new access token and the old one expires when u reset your password. is that handled somewhere else?
authRouter.post("/setPassword/:email", isAuthorizedByEmail("email"),
async (req, res) => {
try{
const responseSuccess = await authService.setPassword(req.params.email, req.body.newPassword)
if (responseSuccess.success) { // if it was successful
if (responseSuccess.success) {
const user = await userService.getUserByEmail(req.params.email)
if (user.status == UserStatus.INVITED) {
// ig i could modify the user object and send that but then i'd be sending ALL the information
userService.updateUserById(user.id, {firstName: user.firstName, lastName: user.lastName, email: user.email, role: user.role, status: UserStatus.ACTIVE})
userService.updateUserById(user.id, {...user, status: UserStatus.ACTIVE})
}
// automatically log in after password reset
const authDTO = await authService.generateToken(req.params.email, req.body.newPassword);
const { refreshToken, ...rest } = authDTO;
const passwordSetResponse = {success:responseSuccess.success, userDTO:rest}
res.cookie("refreshToken", authDTO.refreshToken, cookieOptions).status(200).json(passwordSetResponse);
}
res.status(200).json(responseSuccess);
} catch(error) {
res.status(500).json({ error: getErrorMessage(error) });
}
Expand Down
1 change: 1 addition & 0 deletions backend/typescript/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export type AuthDTO = Token & UserDTO;
export type ResponseSuccessDTO = {
success: boolean;
errorMessage?: string;
userDTO?: UserDTO;
}

export type Letters = "A" | "B" | "C" | "D";
Expand Down
29 changes: 17 additions & 12 deletions frontend/src/APIClients/AuthAPIClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import AUTHENTICATED_USER_KEY from "../constants/AuthConstants";
import { AuthenticatedUser, PasswordResetResponse } from "../types/AuthTypes";
import { AuthenticatedUser, PasswordSetResponse } from "../types/AuthTypes";
import baseAPIClient from "./BaseAPIClient";
import {
getLocalStorageObjProperty,
Expand Down Expand Up @@ -111,24 +111,29 @@ const refresh = async (): Promise<boolean> => {
};

// // trinity did this VV
const setPassword = async (email: string, newPassword: string): Promise<PasswordResetResponse> => {
const setPassword = async (
email: string,
newPassword: string,
): Promise<PasswordSetResponse> => {
const bearerToken = `Bearer ${getLocalStorageObjProperty(
AUTHENTICATED_USER_KEY,
"accessToken",
)}`
console.log(bearerToken)
)}`;
try {
const response = await baseAPIClient.post(
`/auth/setPassword/${email}`,
{newPassword},
{headers:{Authorization:bearerToken}}
)
console.log(response)
return response.data
{ newPassword },
{ headers: { Authorization: bearerToken } },
);
const { success, userDTO, errorMessage } = response.data;
if (success) {
localStorage.setItem(AUTHENTICATED_USER_KEY, JSON.stringify(userDTO));
}
return { success, errorMessage };
} catch (error) {
return {success:false, errorMessage:"An unknown error occured."}
return { success: false, errorMessage: "An unknown error occured." };
}
}
};

export default {
login,
Expand All @@ -137,5 +142,5 @@ export default {
register,
resetPassword,
refresh,
setPassword
setPassword,
};
2 changes: 1 addition & 1 deletion frontend/src/types/AuthTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export type DecodedJWT =
| null
| { [key: string]: unknown; exp: number };

export type PasswordResetResponse = {
export type PasswordSetResponse = {
success: boolean;
errorMessage?: string;
}

0 comments on commit d8263b3

Please sign in to comment.