Skip to content

Commit

Permalink
Added cookies to signup
Browse files Browse the repository at this point in the history
  • Loading branch information
harikrishnan-git committed Jan 29, 2025
1 parent 0f36715 commit 3d8a399
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 8 deletions.
9 changes: 5 additions & 4 deletions frontend/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ import Signup from "./Pages/Signup/Signup";
import Home from "./Pages/Home/Home";
import { Route, Routes } from "react-router-dom";
import { Toaster } from "react-hot-toast";
import { useAuthContext } from "./Context/AuthContext";

function App() {
const [count, setCount] = useState(0);

const { authUser } = useAuthContext();
return (
<div className="p-4 h-screen flex items-center justify-center">
<Routes>
<Route path="/" element={<Home />} />
<Route path="/login" element={<Login />} />
<Route path="/signup" element={<Signup />} />
<Route path="/" element={authUser ? <Home /> : <Login />} />
<Route path="/login" element={authUser ? <Home /> : <Login />} />
<Route path="/signup" element={authUser ? <Home /> : <Signup />} />
</Routes>
<Toaster />
</div>
Expand Down
10 changes: 8 additions & 2 deletions frontend/src/Components/Sidebar/Logout.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import React from "react";
import { BiLogOut } from "react-icons/bi";
import useLogout from "../../Hooks/useLogout";

export default function Logout() {
const { loading, logout } = useLogout();
return (
<div className="mt-auto ">
<button>
<BiLogOut className="h-7 w-7 text-white cursor-pointer" />
<button onClick={logout}>
{!loading ? (
<BiLogOut className="h-7 w-7 text-white cursor-pointer" />
) : (
<span className="loading loading-spinner"></span>
)}
</button>
</div>
);
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/Context/AuthContext.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Children, createContext, useContext, useState } from "react";

export const authContext = createContext();
export const AuthContext = createContext();

export const useAuthContext = () => {
return useContext(AuthContext);
Expand Down
29 changes: 29 additions & 0 deletions frontend/src/Hooks/useLogout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { useAuthContext } from "../Context/AuthContext";
import { useState } from "react";
import toast from "react-hot-toast";

const useLogout = () => {
const [loading, setLoading] = useState(false);
const { setAuthUser } = useAuthContext();
const logout = async () => {
setLoading(true);
try {
const res = await fetch("/api/auth/logout", {
method: "POST",
headers: { "Content-Type": "application/json" },
});
const data = await res.json();
if (data.error) throw new Error(data.error);
await localStorage.removeItem("chat-user");
await setAuthUser(null);
} catch (error) {
toast.error(error.message);
} finally {
setLoading(false);
}
};

return { loading, logout };
};

export default useLogout;
5 changes: 4 additions & 1 deletion frontend/src/Hooks/useSignup.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import toast from "react-hot-toast";
import { useState } from "react";
import { useAuthContext } from "../Context/AuthContext";

const useSignup = () => {
const [loading, setLoading] = useState(false);
const { setAuthUser } = useAuthContext();
const signup = async ({
fullName,
UserName,
Expand Down Expand Up @@ -38,7 +40,8 @@ const useSignup = () => {
if (data.error) {
throw new Error(data.error);
}
console.log(data);
localStorage.setItem("chat-user", JSON.stringify(data));
setAuthUser(data);
} catch (error) {
toast.error(error.message);
setLoading(false);
Expand Down

0 comments on commit 3d8a399

Please sign in to comment.