Skip to content

Commit

Permalink
add changes as suggested by code rabbit
Browse files Browse the repository at this point in the history
  • Loading branch information
Amnyadav committed Oct 11, 2024
1 parent b40129b commit 2fca884
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"dotenv": "^16.4.5",
"framer-motion": "^11.5.6",
"gsap": "^3.12.5",
"js-cookie": "^3.0.5",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-icons": "^5.2.1",
Expand Down
8 changes: 5 additions & 3 deletions frontend/src/components/Pages/Login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Link, useNavigate } from 'react-router-dom';
import photo from '../../assets/login.png';
import React, { useState } from 'react';
import { message } from 'antd';

import Cookies from 'js-cookie'
const Login = () => {
const API_URL = import.meta.env.VITE_BACKEND_URL || 'http://localhost:3000';
const [data, setData] = useState({
Expand Down Expand Up @@ -34,8 +34,10 @@ const Login = () => {
throw new Error(result.message || 'Login failed');
}
// Handle successful login (e.g., store token, redirect)
const token=result.token;
localStorage.setItem("token",JSON.stringify(token));
Cookies.set('authToken',result.token,{
expire:'1h',
secure:true
});
message.success('Login successful');
navigate('/');
} catch (err) {
Expand Down
11 changes: 8 additions & 3 deletions frontend/src/components/Shared/Navbar.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { useState, useEffect } from 'react';
import Logo from '../../assets/Logo/playcafe.png';
import { Link, useLocation, useNavigate } from 'react-router-dom';
import Cookies from 'js-cookie'

const Navbar = () => {
const [isloggedIn, setisloggedIn] = useState(false);
const [isModalOpen, setIsModalOpen] = useState(false);
const [isScrolled, setIsScrolled] = useState(false);
const [isMenuOpen, setIsMenuOpen] = useState(false);
const token=localStorage.getItem("token");
const [token,setToken] = useState(Cookies.get('authToken'));
const location = useLocation();
const navigate = useNavigate(); // Correctly initialize useNavigate

Expand All @@ -18,7 +19,9 @@ const Navbar = () => {
{ name: 'RESERVATION', path: '/reservation' },
{ name: 'BOARDGAMES', path: '/boardgame' },
];

useEffect(() => {
setToken(Cookies.get('authToken'));
});
useEffect(() => {
const handleScroll = () => {
const scrollPosition = window.scrollY;
Expand All @@ -30,6 +33,7 @@ const Navbar = () => {
return () => {
window.removeEventListener('scroll', handleScroll);
};

}, []);

const toggleMenu = () => {
Expand All @@ -39,7 +43,8 @@ const Navbar = () => {
const handleLogout = () => {
// setisloggedIn(false); // Set isLoggedIn to false on confirmation
//managing log in , logout using jwt tokens
localStorage.removeItem("token");
Cookies.remove("authToken");
setToken(null);
setIsModalOpen(false); // Close the modal
setIsMenuOpen(false) // after getting logged out close the menu if it is open
navigate("/login");//navigate to login after get logged out
Expand Down

0 comments on commit 2fca884

Please sign in to comment.