edit:module:user:*update | add refresh jwt token after user doing chances
This commit is contained in:
@ -7,6 +7,10 @@ import { mainErrorHandler } from "../../../helpers/error/handler";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { editUserService } from "../services/editUser.service";
|
||||
import { getCookie } from "../../../helpers/http/userHeader/cookies/getCookies";
|
||||
import { getUserHeaderInformation } from "../../../helpers/http/userHeader/getUserHeaderInformation";
|
||||
import { setCookie } from "../../../helpers/http/userHeader/cookies/setCookies";
|
||||
import { COOKIE_KEYS } from "../../../constants/cookie.keys";
|
||||
import { jwtEncode } from "../../../helpers/http/jwt/encode";
|
||||
|
||||
export const editUserController = async (
|
||||
ctx: Context & {
|
||||
@ -19,8 +23,18 @@ export const editUserController = async (
|
||||
if (!auth_token)
|
||||
return returnErrorResponse(ctx.set, 401, "User Unauthenticated");
|
||||
|
||||
const editUser = await editUserService(auth_token, ctx.body);
|
||||
return editUser;
|
||||
const userHeaderInfo = getUserHeaderInformation(ctx);
|
||||
|
||||
const newUserData = await editUserService(
|
||||
auth_token,
|
||||
userHeaderInfo,
|
||||
ctx.body
|
||||
);
|
||||
|
||||
const newJwtToken = await jwtEncode(newUserData);
|
||||
setCookie(ctx.set, COOKIE_KEYS.AUTH, newJwtToken);
|
||||
|
||||
return returnWriteResponse(ctx.set, 201, "User data updated", newJwtToken);
|
||||
} catch (error) {
|
||||
return mainErrorHandler(ctx.set, error);
|
||||
}
|
||||
|
||||
@ -4,9 +4,13 @@ import { AppError } from "../../../helpers/error/instances/app";
|
||||
import { ErrorForwarder } from "../../../helpers/error/instances/forwarder";
|
||||
import { updateUserRepo } from "../repositories/updateUser.repository";
|
||||
import { checkUserEmailAndUsernameAvailabillityService } from "./checkUserEmailAndUsernameAvailabillity.service";
|
||||
import { logoutService } from "../../auth/services/logout.service";
|
||||
import { loginFromSystemService } from "../../auth/services/loginFromSystem.service";
|
||||
import { UserHeaderInformation } from "../../../helpers/http/userHeader/getUserHeaderInformation/types";
|
||||
|
||||
export const editUserService = async (
|
||||
cookie: string,
|
||||
userHeaderInfo: UserHeaderInformation,
|
||||
payload: Prisma.UserUpdateInput
|
||||
) => {
|
||||
try {
|
||||
@ -55,11 +59,15 @@ export const editUserService = async (
|
||||
};
|
||||
|
||||
// Update the user in the database, use username from the JWT session to find the user
|
||||
const updateUser = await updateUserRepo(
|
||||
jwtSession.user.username,
|
||||
fieldsToUpdate
|
||||
await updateUserRepo(jwtSession.user.username, fieldsToUpdate);
|
||||
|
||||
await logoutService(cookie);
|
||||
const newUserSession = await loginFromSystemService(
|
||||
jwtSession.userId,
|
||||
userHeaderInfo
|
||||
);
|
||||
return updateUser;
|
||||
|
||||
return newUserSession;
|
||||
} catch (error) {
|
||||
ErrorForwarder(error, 500, "Internal server error");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user