-
-
Notifications
You must be signed in to change notification settings - Fork 560
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add upvote system * resolves_conflicts * resolves_conflicts * pnpm file commited * adds: upvote icon * changes: upvote logic * fix: upvote_icon * fix: decrease count for few resources * fix: upvote function * feat: add_firebase_github_auth * feat: Add firebase github auth * fix: text color light mode * fix: Sensitive token in .env * fix: resource visible before signin but can't upvote functionality * fix: dependencies * feat: add toast alerts * fix: toast multiple occurence * fix: toast multiple occurence * fix: toast double occurence * fixes: lint error warnings * removes: save-remove code * fixes: error in card.tsx * fix: eslint warnings * removes: firebase-tool dependency * removes: getUser.ts file * chore: temp commit * chore: test commit * chore: test commit 2 * chore: removes console log --------- Co-authored-by: Rupali Haldiya <[email protected]>
- Loading branch information
1 parent
ffa4d1c
commit 42a718b
Showing
19 changed files
with
2,335 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
"use client" | ||
import { signInWithPopup,GithubAuthProvider,signOut } from 'firebase/auth' | ||
import React, { useEffect, useState } from 'react' | ||
import {auth,provider} from '../../lib/firebase-config' | ||
import { useRouter } from 'next/router' | ||
import Image from 'next/image' | ||
import { toast,ToastContainer } from 'react-toastify' | ||
import 'react-toastify/dist/ReactToastify.css'; | ||
|
||
const SignInWithGithub=()=>{ | ||
const router = useRouter(); | ||
const [imageURL, setImageURL] = useState<string | null>(null); | ||
const [authenticated,setAuthenticated] = useState(false); | ||
const signIn = async () => { | ||
try { | ||
const result = await signInWithPopup(auth, provider); | ||
const credential = GithubAuthProvider.credentialFromResult(result); | ||
console.log("Credential",credential); | ||
console.log("Result: ",result); | ||
const token: string = await result.user.getIdToken(); | ||
const username = result.user.displayName; | ||
localStorage.setItem('accessToken', token); | ||
const currDate = new Date().getTime; | ||
document.cookie = `accessToken=${token};path=/; expires=${currDate}`; | ||
const imgURL = result.user.photoURL; | ||
localStorage.setItem('imageURL',imgURL as string); | ||
setImageURL(imgURL); | ||
setAuthenticated(true); | ||
|
||
fetch('/api/auth',{ | ||
method: 'POST', | ||
headers: { | ||
Authorization: `Bearer ${token}` | ||
} | ||
}).then((response)=>{ | ||
if(response.status===200) | ||
{ | ||
router.push('/'); | ||
toast.success(`${username} is authenticated successfully`); | ||
} | ||
}) | ||
} | ||
catch (error) { | ||
console.error('Error signing in:', error); | ||
} | ||
} | ||
|
||
const handleSignOut=async()=>{ | ||
try { | ||
await signOut(auth); | ||
router.push("/"); | ||
toast.success("You are successfully logged out!!"); | ||
const currDate = new Date().getTime; | ||
document.cookie = `accessToken=; expires=${currDate}; path=/;`; | ||
console.log(document.cookie); | ||
localStorage.removeItem('accessToken'); | ||
localStorage.removeItem('imageURL'); | ||
setAuthenticated(false); | ||
setImageURL(null); | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
} | ||
|
||
useEffect(()=>{ | ||
const storedToken = localStorage.getItem('accessToken'); | ||
const storedImageURL = localStorage.getItem('imageURL'); | ||
if (storedToken && storedImageURL) { | ||
setImageURL(storedImageURL); | ||
setAuthenticated(true); | ||
} | ||
},[]) | ||
return ( | ||
<> | ||
<div> | ||
{authenticated && ( | ||
<div> | ||
{imageURL && <Image height={100} width={100} className='rounded-lg' src={imageURL} alt='User Profile' />} | ||
</div> | ||
)} | ||
<ToastContainer /> | ||
</div> | ||
{authenticated ? ( | ||
<button style={{ background: '#4d0080',color:'white', padding: 10 }} onClick={handleSignOut}> | ||
Sign Out | ||
</button> | ||
) : ( | ||
<button style={{ background: '#4d0080',color:'white', padding: 10 }} onClick={signIn}> | ||
Sign In With Github | ||
</button> | ||
)} | ||
</> | ||
) | ||
|
||
} | ||
|
||
export default SignInWithGithub; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { auth } from "firebase-admin"; | ||
import { cookies } from "next/headers" | ||
import { NextRequest,NextResponse } from "next/server"; | ||
import { customInitApp } from "lib/firebase-admin-config"; | ||
|
||
customInitApp(); | ||
|
||
export async function POST(request: NextRequest){ | ||
const authorization = request.headers.get("Authorization"); | ||
if(authorization && authorization.startsWith('Bearer ')) | ||
{ | ||
const idToken = authorization.split("Bearer ")[1]; | ||
|
||
const decodedToken = await auth().verifyIdToken(idToken); | ||
if(decodedToken) | ||
{ | ||
const expiresIn = 60 * 60 * 24 * 5 * 1000; | ||
const sessionCookie = await auth().createSessionCookie(idToken,{ | ||
expiresIn | ||
}); | ||
|
||
const options = { | ||
name: "Session", | ||
value: sessionCookie, | ||
maxAge: expiresIn, | ||
httpOnly: true, | ||
secure: true, | ||
}; | ||
cookies().set(options); | ||
} | ||
} | ||
return NextResponse.json({}, { status: 200 }); | ||
} | ||
|
||
export async function GET() | ||
{ | ||
const session = cookies().get("accessToken")?.value || ""; | ||
if(!session) | ||
{ | ||
return NextResponse.json({isLogged: false},{status: 401}); | ||
} | ||
const decodedClaims = await auth().verifySessionCookie(session,true); | ||
if(!decodedClaims) | ||
{ | ||
return NextResponse.json({isLogged: false},{status: 401}); | ||
} | ||
return NextResponse.json({ isLogged: true }, { status: 200}); | ||
} | ||
|
||
export async function DELETE(request: NextRequest, response: NextResponse) { | ||
const session = request.cookies.get("accessToken"); | ||
|
||
if (!session) { | ||
return NextResponse.json({ isLogged: false }, { status: 401 }); | ||
} | ||
|
||
try { | ||
response.cookies.set('accessToken', '', { | ||
httpOnly: true, | ||
maxAge: 0, | ||
}) | ||
|
||
return NextResponse.json({}, { status: 200 }); | ||
} catch (error) { | ||
console.error('Error in handleLogout:', error); | ||
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export const metadata = { | ||
title: 'Next.js', | ||
description: 'Generated by Next.js', | ||
} | ||
|
||
export default function RootLayout({ | ||
children, | ||
}: { | ||
children: React.ReactNode | ||
}) { | ||
return ( | ||
<html lang="en"> | ||
<body>{children}</body> | ||
</html> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import admin from "firebase-admin" | ||
import { initializeApp,cert,getApps } from "firebase-admin/app" | ||
import {firebaseConfig} from "../service-account.ts"; | ||
|
||
const firebaseAdminConfig = { | ||
credential: cert(firebaseConfig as admin.ServiceAccount) | ||
} | ||
|
||
export function customInitApp() { | ||
if(!getApps().length) { | ||
initializeApp(firebaseAdminConfig) | ||
} | ||
} |
Oops, something went wrong.