From 8ee703f79677696068273192d8d5cc8994f3a6af Mon Sep 17 00:00:00 2001 From: pratyushsingha Date: Wed, 13 Mar 2024 16:25:14 +0530 Subject: [PATCH] fix: fixed authorization with localstorage --- package.json | 1 + src/App.jsx | 7 +------ src/components/AuthLayout.jsx | 21 +++++++++++++++++++++ src/components/Index.jsx | 2 ++ src/components/ui/checkbox.jsx | 24 ++++++++++++++++++++++++ src/main.jsx | 27 +++++++++++++++------------ src/pages/auth/Login.jsx | 19 +++++++++++++++++++ yarn.lock | 15 +++++++++++++++ 8 files changed, 98 insertions(+), 18 deletions(-) create mode 100644 src/components/AuthLayout.jsx create mode 100644 src/components/ui/checkbox.jsx diff --git a/package.json b/package.json index 4e86173..a1f8839 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ }, "dependencies": { "@hookform/resolvers": "^3.3.4", + "@radix-ui/react-checkbox": "^1.0.4", "@radix-ui/react-dialog": "^1.0.5", "@radix-ui/react-dropdown-menu": "^2.0.6", "@radix-ui/react-icons": "^1.3.0", diff --git a/src/App.jsx b/src/App.jsx index e03abc8..684f1a3 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -3,12 +3,7 @@ import { Outlet } from "react-router-dom"; import LoadingBar from "react-top-loading-bar"; import "./index.css"; -import { - AppContext, - Container, - Sidebar, - Spinner, -} from "@/components/Index"; +import { AppContext, Container, Sidebar, Spinner } from "@/components/Index"; function App() { const { progress, setProgress, loading } = useContext(AppContext); diff --git a/src/components/AuthLayout.jsx b/src/components/AuthLayout.jsx new file mode 100644 index 0000000..710afbc --- /dev/null +++ b/src/components/AuthLayout.jsx @@ -0,0 +1,21 @@ +import React, { useEffect, useState } from "react"; +import { App, Spinner } from "./Index"; +import { useNavigate } from "react-router-dom"; + +const AuthLayout = () => { + const [loading, setLoading] = useState(true); + const navigate = useNavigate(); + useEffect(() => { + if (!localStorage.getItem("accessToken")) { + navigate("/login"); + setLoading(false); + } else { + navigate("/"); + setLoading(false); + } + }, [navigate, setLoading]); + + return loading ? : ; +}; + +export default AuthLayout; diff --git a/src/components/Index.jsx b/src/components/Index.jsx index 6f91591..bfd4fd1 100644 --- a/src/components/Index.jsx +++ b/src/components/Index.jsx @@ -26,6 +26,7 @@ import { CardHeader, CardTitle, } from "@/components/ui/card"; +import { Checkbox } from "@/components/ui/checkbox" import Login from "@/pages/auth/Login.jsx"; import App from "@/App"; @@ -90,4 +91,5 @@ export { PassStrengthBar, Tweets, BookmarkedTweets, + Checkbox }; diff --git a/src/components/ui/checkbox.jsx b/src/components/ui/checkbox.jsx new file mode 100644 index 0000000..5713a94 --- /dev/null +++ b/src/components/ui/checkbox.jsx @@ -0,0 +1,24 @@ +"use client" + +import * as React from "react" +import * as CheckboxPrimitive from "@radix-ui/react-checkbox" +import { CheckIcon } from "@radix-ui/react-icons" + +import { cn } from "@/lib/utils" + +const Checkbox = React.forwardRef(({ className, ...props }, ref) => ( + + + + + +)) +Checkbox.displayName = CheckboxPrimitive.Root.displayName + +export { Checkbox } diff --git a/src/main.jsx b/src/main.jsx index c61ad49..1c516e0 100644 --- a/src/main.jsx +++ b/src/main.jsx @@ -11,16 +11,16 @@ import { TweetDetails, Profile, UserDetails, - Tweets, - BookmarkedTweets + Tweets,BookmarkedTweets, } from "@/components/Index"; import "./index.css"; import LikedTweets from "./pages/LikedTweets"; +import AuthLayout from "./components/AuthLayout"; const router = createBrowserRouter([ { path: "/", - element: , + element: , children: [ { path: "/", element: }, { path: "/tweet/:id", element: }, @@ -29,21 +29,24 @@ const router = createBrowserRouter([ element: , children: [ { path: "/profile/:username/", element: }, - { path: "/profile/:username/bookmarkedTweets", element: }, + { + path: "/profile/:username/bookmarkedTweets", + element: , + }, { path: "/profile/:username/likedTweets", element: }, { path: "/profile/:username/edit", element: }, ], }, - { - path: "/register", - element: , - }, - { - path: "/login", - element: , - }, ], }, + { + path: "/login", + element: , + }, + { + path: "/register", + element: , + }, ]); ReactDOM.createRoot(document.getElementById("root")).render( diff --git a/src/pages/auth/Login.jsx b/src/pages/auth/Login.jsx index 07119ea..71bae6d 100644 --- a/src/pages/auth/Login.jsx +++ b/src/pages/auth/Login.jsx @@ -23,6 +23,7 @@ import { AppContext } from "@/context/AppContext"; import { Separator } from "@/components/ui/separator"; import Container from "@/components/Container"; import InputDiv from "@/components/InputDiv"; +import { Checkbox } from "@/components/Index"; const loginSchema = z.object({ username: z @@ -40,6 +41,7 @@ const Login = () => { const navigate = useNavigate(); const [showPassword, setShowPassword] = useState(null); const [loader, setLoader] = useState(false); + const [rememberMe, setRememberMe] = useState(true); const { progress, setProgress } = useContext(AppContext); const { @@ -66,6 +68,10 @@ const Login = () => { }, { withCredentials: true } ); + if (rememberMe === true) { + localStorage.setItem("accessToken", response.data.data.accessToken); + console.log(rememberMe); + } toast({ title: "success", description: `welcome back ${response.data.data.user.username}`, @@ -133,6 +139,19 @@ const Login = () => { Forget password? +
+ setRememberMe(value)} + id="rememberMe" + /> + +