From 10938c0e60035cf6345b8a282c1111c5e3aa48ff Mon Sep 17 00:00:00 2001 From: Md-Rubel-Ahmed-Rana Date: Fri, 5 Jul 2024 22:30:42 +0600 Subject: [PATCH] Cookie setting problem solved --- backend/dist/app.js | 1 + backend/src/app.ts | 1 + backend/src/controllers/user.controller.ts | 1 + backend/src/middlewares/auth.ts | 1 - frontend/src/components/pages/login/Login.tsx | 11 +++++----- .../src/components/shared/GoogleLogin.tsx | 2 +- frontend/src/features/api/apiSlice.tsx | 6 +---- frontend/src/features/user/index.ts | 6 +++-- frontend/src/hooks/useGetLoggedInUser.ts | 22 ++++++++----------- 9 files changed, 23 insertions(+), 28 deletions(-) diff --git a/backend/dist/app.js b/backend/dist/app.js index 71723cc..e2fa014 100644 --- a/backend/dist/app.js +++ b/backend/dist/app.js @@ -42,6 +42,7 @@ app.use((0, cors_1.default)({ credentials: true, })); app.use(express_1.default.json()); +app.use(express_1.default.urlencoded({ extended: true })); app.use((0, cookie_parser_1.default)()); app.use((0, helmet_1.default)()); app.use((0, express_session_1.default)({ diff --git a/backend/src/app.ts b/backend/src/app.ts index f806647..1c713b0 100644 --- a/backend/src/app.ts +++ b/backend/src/app.ts @@ -33,6 +33,7 @@ app.use( }) ); app.use(express.json()); +app.use(express.urlencoded({ extended: true })); app.use(cookieParser()); app.use(helmet()); app.use( diff --git a/backend/src/controllers/user.controller.ts b/backend/src/controllers/user.controller.ts index d56ac5e..8a0dbbd 100644 --- a/backend/src/controllers/user.controller.ts +++ b/backend/src/controllers/user.controller.ts @@ -1,3 +1,4 @@ +import { config } from "@/configurations/envConfig"; import { UserService } from "@/services/user.service"; import RootController from "@/shared/rootController"; import { Request, Response } from "express"; diff --git a/backend/src/middlewares/auth.ts b/backend/src/middlewares/auth.ts index ee4134a..181bbdf 100644 --- a/backend/src/middlewares/auth.ts +++ b/backend/src/middlewares/auth.ts @@ -5,7 +5,6 @@ import jwt from "jsonwebtoken"; const verifyJwt = (req: Request, res: Response, next: NextFunction) => { try { const token = req.cookies.tmAccessToken; - if (!token) { return res.json({ statusCode: httpStatus.BAD_REQUEST, diff --git a/frontend/src/components/pages/login/Login.tsx b/frontend/src/components/pages/login/Login.tsx index 81d4516..995a3c3 100644 --- a/frontend/src/components/pages/login/Login.tsx +++ b/frontend/src/components/pages/login/Login.tsx @@ -1,12 +1,12 @@ /* eslint-disable @next/next/no-img-element */ import { SubmitHandler, useForm } from "react-hook-form"; import Swal from "sweetalert2"; -import Cookies from "js-cookie"; import { useLoginUserMutation } from "@/features/user"; import Link from "next/link"; import GoogleLogin from "@/components/shared/GoogleLogin"; import { IoMdEye, IoMdEyeOff } from "react-icons/io"; import { useState } from "react"; +import { useRouter } from "next/router"; type FormData = { email: string; @@ -20,13 +20,13 @@ const Login = () => { formState: { errors }, } = useForm({ mode: "onChange" }); const [togglePassword, setTogglePassword] = useState(false); + const router = useRouter(); const [loginUser] = useLoginUserMutation(); const onSubmit: SubmitHandler = async (data) => { const result: any = await loginUser(data); - if (result?.data?.success) { - // Cookies.set("tmAccessToken", result?.data?.data, { expires: 6 }); + if (result?.data?.statusCode === 200) { Swal.fire({ position: "center", icon: "success", @@ -34,9 +34,8 @@ const Login = () => { showConfirmButton: false, timer: 1500, }); - window.location.replace("/dashboard"); - } - if (!result?.error?.data?.success) { + router.push("/dashboard"); + } else { Swal.fire({ position: "center", icon: "error", diff --git a/frontend/src/components/shared/GoogleLogin.tsx b/frontend/src/components/shared/GoogleLogin.tsx index 32ba464..bc667a2 100644 --- a/frontend/src/components/shared/GoogleLogin.tsx +++ b/frontend/src/components/shared/GoogleLogin.tsx @@ -2,7 +2,7 @@ import React from "react"; const GoogleLogin = () => { const handleGoogleLogin = async () => { - window.open("https://api-team-manager.onrender.com/google/login", "_self"); + window.open("http://api-team-manager.onrender.com/google/login", "_self"); }; return (
diff --git a/frontend/src/features/api/apiSlice.tsx b/frontend/src/features/api/apiSlice.tsx index aba7f6a..dd2abdc 100644 --- a/frontend/src/features/api/apiSlice.tsx +++ b/frontend/src/features/api/apiSlice.tsx @@ -1,14 +1,10 @@ import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react"; -import Cookies from "js-cookie"; const apiSlice = createApi({ reducerPath: "api", baseQuery: fetchBaseQuery({ baseUrl: "https://api-team-manager.onrender.com", - headers: { - "Content-type": "application/json", - authorization: Cookies.get("tmAccessToken") as string, - }, + credentials: "include", }), tagTypes: [ "user", diff --git a/frontend/src/features/user/index.ts b/frontend/src/features/user/index.ts index 0b65ec1..ffc51c9 100644 --- a/frontend/src/features/user/index.ts +++ b/frontend/src/features/user/index.ts @@ -12,8 +12,9 @@ const userApi = apiSlice.injectEndpoints({ loginUser: builder.mutation({ query: (data) => ({ method: "POST", - url: "/user/login", + url: "https://api-team-manager.onrender.com/user/login", body: data, + credentials: "include", }), invalidatesTags: ["user"] as any, }), @@ -25,7 +26,8 @@ const userApi = apiSlice.injectEndpoints({ }), loggedInUser: builder.query({ query: () => ({ - url: "/user/auth", + url: "https://api-team-manager.onrender.com/user/auth", + credentials: "include", }), providesTags: ["user"] as any, }), diff --git a/frontend/src/hooks/useGetLoggedInUser.ts b/frontend/src/hooks/useGetLoggedInUser.ts index d4638ff..e0dbb75 100644 --- a/frontend/src/hooks/useGetLoggedInUser.ts +++ b/frontend/src/hooks/useGetLoggedInUser.ts @@ -7,19 +7,15 @@ const useGetLoggedInUser = () => { useEffect(() => { const fetchUser = async () => { try { - const token = Cookies.get("tmAccessToken") as string; - if (token !== "undefined") { - const res = await fetch( - "https://api-team-manager.onrender.com/user/auth", - { - headers: { - authorization: token, - }, - } - ); - const data = await res.json(); - setUser(data?.data); - } + const res = await fetch( + "https://api-team-manager.onrender.com/user/auth", + { + method: "GET", + credentials: "include", + } + ); + const data = await res.json(); + setUser(data?.data); } catch (error) { console.log("Failed to fetch user"); }