Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikhilgholap1304 committed May 29, 2024
1 parent 2fd7efc commit 4f42453
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 12 deletions.
23 changes: 22 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-icons": "^5.2.1",
"react-router-dom": "^6.23.1"
"react-router-dom": "^6.23.1",
"react-toastify": "^10.0.5"
},
"devDependencies": {
"@types/react": "^18.2.66",
Expand Down
17 changes: 16 additions & 1 deletion src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
import { AuthContextProvider, useAuth } from "./context/AuthContext";
import Login from "./components/Login";
import Dashboard from "./components/Dashboard";
import { ToastContainer } from "react-toastify";
import "react-toastify/dist/ReactToastify.css";

const AuthRedirect = () => {
const { currentUid } = useAuth();
Expand Down Expand Up @@ -43,8 +45,21 @@ const App = () => {
</PrivateRoute>
}
/>
<Route path="/" element={<AuthRedirect/>} />
<Route path="/" element={<AuthRedirect />} />
</Routes>
<ToastContainer
position="bottom-right"
autoClose={3000}
hideProgressBar={false}
newestOnTop={true}
closeOnClick
rtl={false}
pauseOnFocusLoss
draggable
pauseOnHover
theme="dark"
transition:Bounce
/>
</AuthContextProvider>
</Router>
);
Expand Down
1 change: 1 addition & 0 deletions src/components/Dashboard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const Dashboard = () => {
try {
await signOut(auth);
localStorage.removeItem("uid");
localStorage.removeItem("loginTimestamp");
Navigate("/login");
} catch (err) {
console.error("Error logging out:", err);
Expand Down
3 changes: 3 additions & 0 deletions src/components/Login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { auth, googleProvider, githubProvider } from "../lib/firebase";
import { signInWithPopup } from "firebase/auth";
import { useNavigate } from "react-router-dom";
import { useAuth } from "../context/AuthContext";
import { toast } from "react-toastify";

const Login = () => {
const Navigate = useNavigate();
Expand All @@ -18,6 +19,7 @@ const Login = () => {
try {
const data = await signInWithPopup(auth, provider);
const user = data.user;
localStorage.setItem("loginTimestamp", new Date().getTime());
localStorage.setItem("uid", user.uid);
setLoading(null);
Navigate(`/${user.uid}`);
Expand All @@ -27,6 +29,7 @@ const Login = () => {
err
);
setLoading(null);
toast.error("try again");
}
};
handleLogin();
Expand Down
35 changes: 26 additions & 9 deletions src/context/AuthContext.jsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,45 @@
import { createContext, useContext, useEffect, useState } from "react";
import { auth } from "../lib/firebase";
import { onAuthStateChanged } from "firebase/auth";
import { onAuthStateChanged, signOut } from "firebase/auth";

export const AuthContext = createContext();

export const AuthContextProvider = ({ children }) => {
const [currentUid, setCurrentUid] = useState(null);

useEffect(() => {
const unsub = onAuthStateChanged(auth, (user) => {
setCurrentUid(user ? user.uid : null);
})
const checkSession = () => {
const loginTimestamp = localStorage.getItem("loginTimestamp");
const currentTime = new Date().getTime();
const tenDaysInMillis = 10 * 24 * 60 * 60 * 1000;

return () => {
unsub();
if (loginTimestamp && currentTime - loginTimestamp > tenDaysInMillis) {
localStorage.removeItem('uid')
localStorage.removeItem('loginTimestamp');
signOut(auth);
setCurrentUid(null);
}
};

const unsubscribe = onAuthStateChanged(auth, (user) => {
if (user) {
setCurrentUid(user.uid);
localStorage.setItem("loginTimestamp", new Date().getTime());
} else {
checkSession();
}
});

checkSession();

return () => unsubscribe();
}, []);

return (
<AuthContext.Provider value={{ currentUid, setCurrentUid }}>
{children}
</AuthContext.Provider>
)
}
);
};

export const useAuth = () => useContext(AuthContext);

0 comments on commit 4f42453

Please sign in to comment.