Skip to content

Commit

Permalink
Token set on cookie from backend and retrive it from backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Md-Rubel-Ahmed-Rana committed Jul 5, 2024
1 parent 5fc17b6 commit a65a6a3
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion backend/dist/controllers/googleOAuth.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}));
Expand Down
5 changes: 3 additions & 2 deletions backend/dist/controllers/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}));
}
Expand Down
6 changes: 3 additions & 3 deletions backend/dist/middlewares/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}
Expand All @@ -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,
});
}
Expand Down
2 changes: 1 addition & 1 deletion backend/src/controllers/googleOAuth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
Expand Down
5 changes: 3 additions & 2 deletions backend/src/controllers/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
});
}
Expand Down
7 changes: 4 additions & 3 deletions backend/src/middlewares/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}
Expand All @@ -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,
});
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/pages/login/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const Login = () => {
const onSubmit: SubmitHandler<FormData> = 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",
Expand Down

0 comments on commit a65a6a3

Please sign in to comment.