From a65a6a397a5ed71c13a994d61fbd589972038e6a Mon Sep 17 00:00:00 2001 From: Md-Rubel-Ahmed-Rana Date: Fri, 5 Jul 2024 18:10:39 +0600 Subject: [PATCH] Token set on cookie from backend and retrive it from backend --- backend/dist/controllers/googleOAuth.controller.js | 2 +- backend/dist/controllers/user.controller.js | 5 +++-- backend/dist/middlewares/auth.js | 6 +++--- backend/src/controllers/googleOAuth.controller.ts | 2 +- backend/src/controllers/user.controller.ts | 5 +++-- backend/src/middlewares/auth.ts | 7 ++++--- frontend/src/components/pages/login/Login.tsx | 2 +- 7 files changed, 16 insertions(+), 13 deletions(-) diff --git a/backend/dist/controllers/googleOAuth.controller.js b/backend/dist/controllers/googleOAuth.controller.js index b03d02f..cf0bdda 100644 --- a/backend/dist/controllers/googleOAuth.controller.js +++ b/backend/dist/controllers/googleOAuth.controller.js @@ -22,7 +22,7 @@ class Controller extends rootController_1.default { this.login = this.catchAsync((req, res) => __awaiter(this, void 0, void 0, function* () { if (req === null || req === void 0 ? void 0 : req.user) { const result = yield googleOAuth_service_1.GoogleOAuthService.login(req.user); - res.cookie("tmAccessToken", result); + res.cookie("tmAccessToken", result, { httpOnly: true, secure: true }); res.redirect(envConfig_1.config.google.redirectUrl); } })); diff --git a/backend/dist/controllers/user.controller.js b/backend/dist/controllers/user.controller.js index 4417438..9c402e8 100644 --- a/backend/dist/controllers/user.controller.js +++ b/backend/dist/controllers/user.controller.js @@ -60,11 +60,12 @@ class Controller extends rootController_1.default { this.login = this.catchAsync((req, res) => __awaiter(this, void 0, void 0, function* () { const { email, password } = req.body; const result = yield user_service_1.UserService.login(email, password); + res.cookie("tmAccessToken", result, { httpOnly: true, secure: true }); this.apiResponse(res, { statusCode: http_status_1.default.OK, success: true, - message: "User logged in successfully", - data: result, + message: "Login successful", + data: null, }); })); } diff --git a/backend/dist/middlewares/auth.js b/backend/dist/middlewares/auth.js index 45b79bc..f71d2fa 100644 --- a/backend/dist/middlewares/auth.js +++ b/backend/dist/middlewares/auth.js @@ -7,12 +7,12 @@ const http_status_1 = __importDefault(require("http-status")); const jsonwebtoken_1 = __importDefault(require("jsonwebtoken")); const verifyJwt = (req, res, next) => { try { - const token = req.headers.authorization; + const token = req.cookies.tmAccessToken; if (!token) { return res.json({ statusCode: http_status_1.default.BAD_REQUEST, success: false, - message: "Did't provide token", + message: "Token not provided", data: null, }); } @@ -33,7 +33,7 @@ const verifyJwt = (req, res, next) => { res.json({ statusCode: http_status_1.default.INTERNAL_SERVER_ERROR, success: false, - message: "There was an error to verify token", + message: "There was an error verifying the token", error: error.message, }); } diff --git a/backend/src/controllers/googleOAuth.controller.ts b/backend/src/controllers/googleOAuth.controller.ts index a076da0..ebe95c2 100644 --- a/backend/src/controllers/googleOAuth.controller.ts +++ b/backend/src/controllers/googleOAuth.controller.ts @@ -7,7 +7,7 @@ class Controller extends RootController { login = this.catchAsync(async (req: Request, res: Response) => { if (req?.user) { const result: string = await GoogleOAuthService.login(req.user); - res.cookie("tmAccessToken", result); + res.cookie("tmAccessToken", result, { httpOnly: true, secure: true }); res.redirect(config.google.redirectUrl); } }); diff --git a/backend/src/controllers/user.controller.ts b/backend/src/controllers/user.controller.ts index 7438a26..d56ac5e 100644 --- a/backend/src/controllers/user.controller.ts +++ b/backend/src/controllers/user.controller.ts @@ -49,11 +49,12 @@ class Controller extends RootController { login = this.catchAsync(async (req: Request, res: Response) => { const { email, password } = req.body; const result = await UserService.login(email, password); + res.cookie("tmAccessToken", result, { httpOnly: true, secure: true }); this.apiResponse(res, { statusCode: httpStatus.OK, success: true, - message: "User logged in successfully", - data: result, + message: "Login successful", + data: null, }); }); } diff --git a/backend/src/middlewares/auth.ts b/backend/src/middlewares/auth.ts index f385ace..ee4134a 100644 --- a/backend/src/middlewares/auth.ts +++ b/backend/src/middlewares/auth.ts @@ -4,12 +4,13 @@ import jwt from "jsonwebtoken"; const verifyJwt = (req: Request, res: Response, next: NextFunction) => { try { - const token = req.headers.authorization; + const token = req.cookies.tmAccessToken; + if (!token) { return res.json({ statusCode: httpStatus.BAD_REQUEST, success: false, - message: "Did't provide token", + message: "Token not provided", data: null, }); } @@ -35,7 +36,7 @@ const verifyJwt = (req: Request, res: Response, next: NextFunction) => { res.json({ statusCode: httpStatus.INTERNAL_SERVER_ERROR, success: false, - message: "There was an error to verify token", + message: "There was an error verifying the token", error: error.message, }); } diff --git a/frontend/src/components/pages/login/Login.tsx b/frontend/src/components/pages/login/Login.tsx index 66e967a..81d4516 100644 --- a/frontend/src/components/pages/login/Login.tsx +++ b/frontend/src/components/pages/login/Login.tsx @@ -26,7 +26,7 @@ const Login = () => { const onSubmit: SubmitHandler = async (data) => { const result: any = await loginUser(data); if (result?.data?.success) { - Cookies.set("tmAccessToken", result?.data?.data, { expires: 6 }); + // Cookies.set("tmAccessToken", result?.data?.data, { expires: 6 }); Swal.fire({ position: "center", icon: "success",