diff --git a/src/app/dashboard/dashboard.tsx b/src/app/dashboard/dashboard.tsx
index 7450f4c..c58e23c 100644
--- a/src/app/dashboard/dashboard.tsx
+++ b/src/app/dashboard/dashboard.tsx
@@ -17,6 +17,7 @@ import {
} from "chart.js";
import { DashboardService } from "@/services/streak-service";
import MemberDetails from "./[memberId]/page";
+import { AuthService } from "@/services/authentication-service";
ChartJS.register(CategoryScale, LinearScale, BarElement, Title, Tooltip, Legend);
@@ -144,12 +145,25 @@ const Dashboard = () => {
},
],
};
+
+ // Saving auth token for authentication.
+ useEffect(() => {
+ const urlParams = new URLSearchParams(window.location.search);
+ const token = urlParams.get("code");
+ if (token) {
+ AuthService.saveToken(token);
+ console.log("Saved ", token)
+ window.history.replaceState({}, document.title, "/dashboard"); // Clean URL
+ }
+ }, []);
useEffect(() => {
fetchAttendanceCount();
fetchMemberSummary();
}, [selectedDate]);
+
+
const formatDate = (date: Date): string => {
const options: Intl.DateTimeFormatOptions = { month: "short", day: "numeric", year: "numeric" };
return date.toLocaleDateString("en-US", options);
diff --git a/src/app/page.tsx b/src/app/page.tsx
index 0c72c8a..53e0963 100644
--- a/src/app/page.tsx
+++ b/src/app/page.tsx
@@ -1,8 +1,12 @@
+"use client";
+
import Image from "next/image";
import logo from "../../public/amfoss-logo-white.png"
import amfoss from "../../public/amfoss-footer-black@3x.png"
import { Github } from 'lucide-react';
import LampContainer from "../components/lamp"
+import { AuthService } from "@/services/authentication-service";
+
export default function Home() {
return (
@@ -18,7 +22,7 @@ export default function Home() {
India's Leading FOSS Club
-
+
diff --git a/src/services/authentication-service.ts b/src/services/authentication-service.ts
new file mode 100644
index 0000000..bdb4d13
--- /dev/null
+++ b/src/services/authentication-service.ts
@@ -0,0 +1,47 @@
+"use client";
+
+
+const CLIENT_ID = process.env.NEXT_PUBLIC_GITHUB_CLIENT_ID;
+const REDIRECT_URI = process.env.NEXT_PUBLIC_GITHUB_REDIRECT_URI;
+
+export const AuthService = {
+ /**
+ * Redirects user to GitHub login page for authentication.
+ */
+ loginWithGitHub() {
+ window.location.href = `http://github.com/login/oauth/authorize?client_id=${CLIENT_ID}&redirect_uri=${REDIRECT_URI}&scope=read:user`;
+ },
+
+
+ /**
+ * Stores the authentication token in local storage.
+ * @param {string} token - GitHub OAuth token.
+ */
+ saveToken(token: string) {
+ localStorage.setItem("github_token", token);
+ },
+
+ /**
+ * Retrieves the authentication token from local storage.
+ * @returns {string | null} - The stored token or null if not found.
+ */
+ getToken() {
+ return localStorage.getItem("github_token");
+ },
+
+ /**
+ * Removes the authentication token from local storage and logs out the user.
+ */
+ logout() {
+ localStorage.removeItem("github_token");
+ window.location.reload();
+ },
+
+ /**
+ * Checks if the user is authenticated by verifying if a token exists.
+ * @returns {boolean} - True if authenticated, false otherwise.
+ */
+ isAuthenticated() {
+ return Boolean(localStorage.getItem("github_token"));
+ },
+};