Skip to content

add useffect for storing auth-token in localstorage #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/app/dashboard/dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
"use client";

import Image from "next/image";
import logo from "../../public/amfoss-logo-white.png"
import amfoss from "../../public/[email protected]"
import { Github } from 'lucide-react';
import LampContainer from "../components/lamp"
import { AuthService } from "@/services/authentication-service";


export default function Home() {
return (
Expand All @@ -18,7 +22,7 @@ export default function Home() {
</div>
<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>
<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" />
<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>
<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>
</div>
</div>
</main>
Expand Down
47 changes: 47 additions & 0 deletions src/services/authentication-service.ts
Original file line number Diff line number Diff line change
@@ -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"));
},
};