Skip to content

Commit

Permalink
added user cookie auth
Browse files Browse the repository at this point in the history
  • Loading branch information
ayush7801 committed May 9, 2024
1 parent 62f6c63 commit 37f0c9b
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 5 deletions.
39 changes: 38 additions & 1 deletion backend/src/controller/userController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,41 @@ const userLogin = async (req: Request, res: Response, next: NextFunction) => {
}
}

export { getAllUsers, userSignup, userLogin };
const userAuthStatus = async (req: Request, res: Response, next: NextFunction) => {
try {
// User Authentication Status
console.log(res.locals.jwtUser);
const user = await User.findById(res.locals.jwtUser.id);
console.log(user);
if(!user) {
res.status(401).json({
status: 'fail',
message: 'Token is valid but user not found'
});
}
else if(user._id.toString() !== res.locals.jwtUser.id) {
res.status(403).json({
status: 'fail',
message: `Permission didn't match`
});
}
else{
// set final response
res.status(200).json({
status: 'success',
message: 'User logged in successfully',
userId: user._id.toString(),
name: user.name,
email: user.email
});
}
}catch (err) {
console.log(err);
res.status(500).json({
status: 'fail',
message: err.message
});
}
}

export { getAllUsers, userSignup, userLogin, userAuthStatus}
4 changes: 3 additions & 1 deletion backend/src/routes/userRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Router } from 'express';
import { getAllUsers, userLogin, userSignup } from '../controller/userController.js';
import { getAllUsers, userAuthStatus, userLogin, userSignup } from '../controller/userController.js';
import { logInValidator, signUpValidator, validateRequest } from '../utils/validator.js';
import { verifyToken } from '../utils/token-manager.js';

let userRouter = Router();

userRouter.get('/', getAllUsers);
userRouter.post('/signup', validateRequest(signUpValidator), userSignup);
userRouter.post('/login', validateRequest(logInValidator), userLogin);
userRouter.get('/auth-status', verifyToken, userAuthStatus);


export default userRouter;
16 changes: 16 additions & 0 deletions backend/src/utils/token-manager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import { Request, Response, NextFunction } from 'express';
import jwt from 'jsonwebtoken';
import { Constants } from './constants.js';

export const createToken = (payload: Object, expiresIn: string) => {
return jwt.sign(payload, process.env.JWT_SECRET as string, { expiresIn });
}

export const verifyToken = (req: Request, res: Response, next: NextFunction) => {
const token = req.signedCookies[Constants.AUTH_COOKIE_NAME];
if (!token || token.trim() === '') {
return res.status(401).json({ message: 'Invalid Token' });
}
jwt.verify(token, process.env.JWT_SECRET as string, (err, decoded) => {
if (err) {
return res.status(401).json({ message: 'Unauthorized' });
}
res.locals.jwtUser = decoded;
return next();
});
}
17 changes: 16 additions & 1 deletion frontend/src/apiCalls/userApiCalls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,19 @@ const userLoginAPI = async (email: string, password: string) => {
}
}

export { userLoginAPI }
const userTokenAuthentication = async () => {
try {
const res = await axios.get(`/users/auth-status`);
console.log(res);
if (res.status === 200) {
console.log("User authenticated successfully");
return await res.data;
}
} catch (err) {
console.error(`Some error occurred while authenticating user with status code ${(err as any)?.response?.status}: and message: ${(err as any)?.response?.data?.message}`);
console.error("Some error occurred while authenticating user: ", err);
return err;
}
}

export { userLoginAPI, userTokenAuthentication }
28 changes: 26 additions & 2 deletions frontend/src/context/AuthContext.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createContext, useContext, useEffect, useState } from "react";
import { userLoginAPI } from "../apiCalls/userApiCalls";
import { userLoginAPI, userTokenAuthentication } from "../apiCalls/userApiCalls";
import toast from "react-hot-toast";

type User = {
Expand All @@ -20,10 +20,34 @@ export const AuthProvider = ({children}: {children: React.ReactNode}) => {
const [user, setUser] = useState<User | null>(null);
const [isLoggedIn, setIsLoggedIn] = useState<boolean>(false);

useEffect(() => {}, []);
useEffect(() => {
// make a request to the server to check if the user is logged in
// if the user is logged in, set the user and isLoggedIn to true
// else set the user and isLoggedIn to false
async function checkUserAuthStatus() {
toast.loading('Authenticating User...', {id: 'login'});

return new Promise<void>( async (resolve, reject) => {
// make a request to the server to login
const res: any = await userTokenAuthentication();
if (res instanceof Error) {
console.log("Some error occurred while Authenticating user: ", res);
toast.error('Authentication failed, log in again...', {id: 'login'});
return reject();
}
// if successful, set the user and isLoggedIn to true
setUser({name: res.name, email: res.email});
setIsLoggedIn(true);
toast.success('Successfully Authenticated...', {id: 'login'});
return resolve();
});
}
checkUserAuthStatus();
}, []);

const login = async (email: string, password: string) => {
toast.loading('Logging in...', {id: 'login'});

return new Promise<void>( async (resolve, reject) => {
// make a request to the server to login
const res: any = await userLoginAPI(email, password);
Expand Down

0 comments on commit 37f0c9b

Please sign in to comment.