diff --git a/backend/src/controllers/authController.js b/backend/src/controllers/authController.js
index d610812..0eea2ef 100644
--- a/backend/src/controllers/authController.js
+++ b/backend/src/controllers/authController.js
@@ -49,8 +49,12 @@ exports.login = async (req, res) => {
const token = jwt.sign({ name: user.name, isVerified: user.isVerified }, process.env.SECURITY_KEY, { expiresIn: '5hour' });
- res.cookie("token", token);
-
+ const oneWeekInSeconds = 7 * 24 * 60 * 60; // 7 days * 24 hours * 60 minutes * 60 seconds
+ const expirationDate = new Date(Date.now() + oneWeekInSeconds * 1000); // Convert seconds to milliseconds
+ res.cookie('token', token, {
+ expires: expirationDate,
+
+ });
// Send the token in the response
res.json({ message: 'Login successful' });
} catch (error) {
@@ -202,4 +206,18 @@ exports.facebooklogin = async ( req,res) => {
exports.googleLoginApp = async (req,res) => {
const { token } = req.body
-}
\ No newline at end of file
+}
+
+
+exports.logout = async (req,res) =>{
+ try{
+ res.clearCookie('token')
+ res.json({msg:"Done"})
+ }
+ catch(err){
+ res.error(err)
+ }
+
+}
+
+
diff --git a/backend/src/routes/authRoutes.js b/backend/src/routes/authRoutes.js
index 7e5a749..0fe2b54 100644
--- a/backend/src/routes/authRoutes.js
+++ b/backend/src/routes/authRoutes.js
@@ -6,5 +6,8 @@ router.post('/login', authController.login);
router.post('/googleLogin',authController.googleLogin)
router.post('/loginApp', authController.loginApp);
router.post('/facebooklogin',authController.facebooklogin);
+router.post('/logout', authController.logout);
+
+
module.exports = router;
diff --git a/webapp/src/components/UserInfo.js b/webapp/src/components/UserInfo.js
index dd0fc33..6be82a0 100644
--- a/webapp/src/components/UserInfo.js
+++ b/webapp/src/components/UserInfo.js
@@ -1,5 +1,6 @@
import React from 'react'
import { Link } from 'react-router-dom'
+import { logout } from '../utils/logout'
function UserInfo({showProfile}) {
return (
-
-
+
Log out
diff --git a/webapp/src/context/AuthContext.js b/webapp/src/context/AuthContext.js
index 65f98c2..e78b26f 100644
--- a/webapp/src/context/AuthContext.js
+++ b/webapp/src/context/AuthContext.js
@@ -10,11 +10,7 @@ export const useAuthContext = () => useContext(AuthContext)
//create provider
export const AuthProvider = ({children})=>{
//Todo
- const [auth,setAuth] = useState({
- name:'UserName',
- isVerified:true,
- token:"dummy"
- })
+ const [auth,setAuth] = useState(null)
return (
diff --git a/webapp/src/routes/PrivateRoutes.js b/webapp/src/routes/PrivateRoutes.js
index a81085d..546d5cf 100644
--- a/webapp/src/routes/PrivateRoutes.js
+++ b/webapp/src/routes/PrivateRoutes.js
@@ -2,11 +2,20 @@ import { Outlet, Navigate} from "react-router-dom";
import React from 'react'
import { useAuthContext } from "../context/AuthContext";
+import { getSessionCookie } from "../utils/cookie";
+
function PrivateRoutes() {
- const {auth}= useAuthContext()
+
+
+ let allow = false
+
+ allow = getSessionCookie("token")
+
+
+
return (
- auth.token ? :
+ allow ? :
)
}
diff --git a/webapp/src/utils/logout.js b/webapp/src/utils/logout.js
new file mode 100644
index 0000000..fb653c1
--- /dev/null
+++ b/webapp/src/utils/logout.js
@@ -0,0 +1,17 @@
+import React from 'react'
+import axios from 'axios';
+
+ export const logout = () => {
+ axios.defaults.withCredentials = true
+ console.log("clciked")
+ // Add logout functionality here
+ axios.post("http://localhost:5000/api/auth/logout")
+ .then((res)=>{
+ window.location.reload(true);
+ window.location.href = "/";
+ }).catch((err)=>console.log(err))
+ };
+
+
+
+
\ No newline at end of file
diff --git a/webapp/src/views/Login.js b/webapp/src/views/Login.js
index 181b5f6..719b6c3 100644
--- a/webapp/src/views/Login.js
+++ b/webapp/src/views/Login.js
@@ -1,4 +1,4 @@
-import React from "react";
+import React, { useState } from "react";
import FloatingLabel from 'react-bootstrap/FloatingLabel';
import Form from 'react-bootstrap/Form';
import '../assets/CSS/signUp.css'
@@ -11,21 +11,43 @@ import jwt_decode from 'jwt-decode'
export default function Login() {
const navigate = useNavigate()
+ const {setAuth,auth} = useAuthContext()
+
+ const [email,setEmail] = useState("")
+ const [password,setPassword] = useState("")
+
+
- const handleSubmit = ()=>{
-
+ const handleSubmit = (e)=>{
+ e.preventDefault()
+
axios.defaults.withCredentials = true
- axios.post('http://localhost:5000/api/auth/login', {email:"someone@gmail.com", password:"pwd123"})
+ axios.post('http://localhost:5000/api/auth/login', {email:email, password:password})
.then(response => {
// Handle the successful response here
+
console.log(jwt_decode(getSessionCookie("token")))
- console.log('Registration successful:', response.data);
+ setAuth(true)
+
+ if(jwt_decode(getSessionCookie("token")).isVerified){
+ navigate("/dashboard",{replace:true})
+ }
+ else{
+ navigate("/dashboard",{replace:true})
+ }
+
+
+
+
})
.catch(error => {
// Handle any errors that occur during the request
- console.error('Registration failed:', error.response.data);
- });
+ console.log('Login failed!')
+ }).finally(()=>{
+ setEmail("")
+ setPassword("")
+ })
// if(auth.isVerified){
@@ -40,7 +62,7 @@ export default function Login() {
LOGIN
-
-
+
{/*
OR
*/}
OR
diff --git a/webapp/src/views/SignUp.js b/webapp/src/views/SignUp.js
index d5e060c..69427f7 100644
--- a/webapp/src/views/SignUp.js
+++ b/webapp/src/views/SignUp.js
@@ -16,7 +16,8 @@ export default function SignUp() {
const [conpassword, setConPassword] = useState("")
- const handleRegister = ()=>{
+ const handleRegister = (e)=>{
+ e.preventDefault()
if(password != conpassword){
return
}
@@ -24,7 +25,7 @@ export default function SignUp() {
axios.post('http://localhost:5000/api/auth/signup', {email:email, password:password, name:userName})
.then(response => {
// Handle the successful response here
- console.log('Registration successful:', response.data);
+ console.log('Registration successful!', response.data);
// navigate("/passwordverify", { replace: true });
@@ -33,7 +34,12 @@ export default function SignUp() {
})
.catch(error => {
// Handle any errors that occur during the request
- console.error('Registration failed:', error.response.data);
+ console.log('Registration failed!');
+ }).finally(()=>{
+ setUserName("")
+ setEmail("")
+ setPassword("")
+ setConPassword("")
})
}
@@ -41,7 +47,7 @@ export default function SignUp() {