Skip to content

Commit b2b9b9a

Browse files
committed
add useffect for storing auth-token in localstorage
1 parent cbd46e9 commit b2b9b9a

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

src/app/dashboard/dashboard.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
} from "chart.js";
1818
import { DashboardService } from "@/services/streak-service";
1919
import MemberDetails from "./[memberId]/page";
20+
import { AuthService } from "@/services/authentication-service";
2021

2122
ChartJS.register(CategoryScale, LinearScale, BarElement, Title, Tooltip, Legend);
2223

@@ -144,12 +145,25 @@ const Dashboard = () => {
144145
},
145146
],
146147
};
148+
149+
// Saving auth token for authentication.
150+
useEffect(() => {
151+
const urlParams = new URLSearchParams(window.location.search);
152+
const token = urlParams.get("code");
153+
if (token) {
154+
AuthService.saveToken(token);
155+
console.log("Saved ", token)
156+
window.history.replaceState({}, document.title, "/dashboard"); // Clean URL
157+
}
158+
}, []);
147159

148160
useEffect(() => {
149161
fetchAttendanceCount();
150162
fetchMemberSummary();
151163
}, [selectedDate]);
152164

165+
166+
153167
const formatDate = (date: Date): string => {
154168
const options: Intl.DateTimeFormatOptions = { month: "short", day: "numeric", year: "numeric" };
155169
return date.toLocaleDateString("en-US", options);

src/app/page.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
"use client";
2+
13
import Image from "next/image";
24
import logo from "../../public/amfoss-logo-white.png"
35
import amfoss from "../../public/[email protected]"
46
import { Github } from 'lucide-react';
57
import LampContainer from "../components/lamp"
8+
import { AuthService } from "@/services/authentication-service";
9+
610

711
export default function Home() {
812
return (
@@ -18,7 +22,7 @@ export default function Home() {
1822
</div>
1923
<p className="absolute lg:top-[45vh] md:top-[35vh] sm:top-[42.5vh] left-[0vw] w-[100vw] animate-fadeInUp lg:text-[3vw] md:text-[6vw] sm:text-[7vw] text-white font-bold opacity-0" style={{ textAlign: "center" }}>India's Leading FOSS Club</p>
2024
<Image className="absolute animate-fadeInUp lg:max-w-[20vw] md:max-w-[40vw] sm:max-w-[43vw] lg:left-[40vw] md:left-[30vw] sm:left-[28vw] lg:top-[52.5vh] sm:top-[48.3vh] opacity-0" src={amfoss} alt="amfoss" />
21-
<button className="absolute animate-fadeInUp text-center pt-1 pb-1 font-semibold lg:text-[2.5vh] md:text-3xl sm:text-xl text-black lg:w-[12vw] sm:w-[38vw] md:w-[28vw] bg-yellow-400 lg:rounded-[0.7rem] md:rounded-[0.5rem] sm:rounded-[1rem] lg:top-[70vh] md:top-[75vh] sm:top-[60vh] lg:left-[44vw] md:left-[36vw] sm:left-[33vw] opacity-0 flex items-center justify-center gap-4"><Github />Sign in</button>
25+
<button onClick={() => AuthService.loginWithGitHub()} className="absolute animate-fadeInUp text-center pt-1 pb-1 font-semibold lg:text-[2.5vh] md:text-3xl sm:text-xl text-black lg:w-[12vw] sm:w-[38vw] md:w-[28vw] bg-yellow-400 lg:rounded-[0.7rem] md:rounded-[0.5rem] sm:rounded-[1rem] lg:top-[70vh] md:top-[75vh] sm:top-[60vh] lg:left-[44vw] md:left-[36vw] sm:left-[33vw] opacity-0 flex items-center justify-center gap-4"><Github />Sign in</button>
2226
</div>
2327
</div>
2428
</main>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"use client";
2+
3+
4+
const CLIENT_ID = process.env.NEXT_PUBLIC_GITHUB_CLIENT_ID;
5+
const REDIRECT_URI = process.env.NEXT_PUBLIC_GITHUB_REDIRECT_URI;
6+
7+
export const AuthService = {
8+
/**
9+
* Redirects user to GitHub login page for authentication.
10+
*/
11+
loginWithGitHub() {
12+
window.location.href = `http://github.com/login/oauth/authorize?client_id=${CLIENT_ID}&redirect_uri=${REDIRECT_URI}&scope=read:user`;
13+
},
14+
15+
16+
/**
17+
* Stores the authentication token in local storage.
18+
* @param {string} token - GitHub OAuth token.
19+
*/
20+
saveToken(token: string) {
21+
localStorage.setItem("github_token", token);
22+
},
23+
24+
/**
25+
* Retrieves the authentication token from local storage.
26+
* @returns {string | null} - The stored token or null if not found.
27+
*/
28+
getToken() {
29+
return localStorage.getItem("github_token");
30+
},
31+
32+
/**
33+
* Removes the authentication token from local storage and logs out the user.
34+
*/
35+
logout() {
36+
localStorage.removeItem("github_token");
37+
window.location.reload();
38+
},
39+
40+
/**
41+
* Checks if the user is authenticated by verifying if a token exists.
42+
* @returns {boolean} - True if authenticated, false otherwise.
43+
*/
44+
isAuthenticated() {
45+
return Boolean(localStorage.getItem("github_token"));
46+
},
47+
};

0 commit comments

Comments
 (0)