-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2f1f18c
commit d970b67
Showing
36 changed files
with
2,217 additions
and
2,034 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 |
---|---|---|
@@ -1,68 +1,65 @@ | ||
import { createContext, useContext, useEffect, useState } from "react" | ||
import { createContext, useContext, useEffect, useState } from "react"; | ||
|
||
import { GoogleAuthProvider, signInWithPopup } from "firebase/auth" | ||
import { GoogleAuthProvider, signInWithPopup } from "firebase/auth"; | ||
// @ts-ignore | ||
import { auth } from "@/back_end/utils" | ||
import { auth } from "@/back_end/utils"; | ||
|
||
// @ts-ignore | ||
import { checkAdmin } from "@/utils/api"; | ||
|
||
|
||
// rome-ignore lint/suspicious/noExplicitAny: <explanation> | ||
const AuthContext = createContext<any>({}) | ||
|
||
export const useAuth = () => useContext(AuthContext) | ||
export const AuthContextProvider = ({children}: {children:React.ReactNode}) => { | ||
|
||
const provider = new GoogleAuthProvider(); | ||
provider.setCustomParameters({ prompt: 'select_account' }); | ||
|
||
// rome-ignore lint/suspicious/noExplicitAny: <explanation> | ||
const [currentUser, setCurrentUser] = useState<any>(null) | ||
const [isAdmin, setAdmin] = useState(false) | ||
const [loading, setLoading] = useState(true) | ||
|
||
const signIn = async () => { | ||
const result = await signInWithPopup(auth, provider); | ||
|
||
|
||
return result; | ||
} | ||
const AuthContext = createContext<any>({}); | ||
|
||
const getUser = () => { | ||
return auth.currentUser; | ||
} | ||
|
||
const signOut = async () => { | ||
return auth.signOut(); | ||
} | ||
|
||
useEffect(() => { | ||
export const useAuth = () => useContext(AuthContext); | ||
export const AuthContextProvider = ({ | ||
children, | ||
}: { children: React.ReactNode }) => { | ||
const provider = new GoogleAuthProvider(); | ||
provider.setCustomParameters({ prompt: "select_account" }); | ||
|
||
// rome-ignore lint/suspicious/noExplicitAny: <explanation> | ||
const unsubscribe = auth.onAuthStateChanged(async (user: any) => { | ||
setCurrentUser(user) | ||
if (user) { | ||
setAdmin(await checkAdmin(user)) | ||
} | ||
setLoading(false) | ||
}) | ||
|
||
return unsubscribe | ||
}, []) | ||
|
||
|
||
const value = { | ||
currentUser, | ||
isAdmin, | ||
getUser, | ||
signIn, | ||
signOut | ||
} | ||
|
||
return ( | ||
<AuthContext.Provider value={value}> | ||
{loading ? null : children} | ||
</AuthContext.Provider> | ||
) | ||
} | ||
const [currentUser, setCurrentUser] = useState<any>(null); | ||
const [isAdmin, setAdmin] = useState(false); | ||
const [loading, setLoading] = useState(true); | ||
|
||
const signIn = async () => { | ||
const result = await signInWithPopup(auth, provider); | ||
|
||
return result; | ||
}; | ||
|
||
const getUser = () => { | ||
return auth.currentUser; | ||
}; | ||
|
||
const signOut = async () => { | ||
return auth.signOut(); | ||
}; | ||
|
||
useEffect(() => { | ||
// rome-ignore lint/suspicious/noExplicitAny: <explanation> | ||
const unsubscribe = auth.onAuthStateChanged(async (user: any) => { | ||
setCurrentUser(user); | ||
if (user) { | ||
setAdmin(await checkAdmin(user)); | ||
} | ||
setLoading(false); | ||
}); | ||
|
||
return unsubscribe; | ||
}, []); | ||
|
||
const value = { | ||
currentUser, | ||
isAdmin, | ||
getUser, | ||
signIn, | ||
signOut, | ||
}; | ||
|
||
return ( | ||
<AuthContext.Provider value={value}> | ||
{loading ? null : children} | ||
</AuthContext.Provider> | ||
); | ||
}; |
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 |
---|---|---|
@@ -1,89 +1,89 @@ | ||
import axios from 'axios'; | ||
import axios from "axios"; | ||
import { database } from "../utils/index.js"; | ||
import { | ||
ref, | ||
query, | ||
get, | ||
} from "firebase/database"; | ||
import { ref, query, get } from "firebase/database"; | ||
|
||
const orgName = "hcp-uw"; | ||
const HCP_ACCESS_TOKEN = process.env.NEXT_PUBLIC_HCP_ACCESS_TOKEN; | ||
const CLIENT_ID = process.env.NEXT_PUBLIC_CLIENT_ID; | ||
const CLIENT_SECRET = process.env.NEXT_PUBLIC_CLIENT_SECRET; | ||
|
||
export async function exchangeAuth(auth) { | ||
try { | ||
const response = await axios.post("https://github.com/login/oauth/access_token", null, { | ||
params: { | ||
client_id: CLIENT_ID, | ||
client_secret: CLIENT_SECRET, | ||
code: auth, | ||
}, | ||
headers: { | ||
Accept: 'application/json', | ||
}, | ||
}); | ||
try { | ||
const response = await axios.post( | ||
"https://github.com/login/oauth/access_token", | ||
null, | ||
{ | ||
params: { | ||
client_id: CLIENT_ID, | ||
client_secret: CLIENT_SECRET, | ||
code: auth, | ||
}, | ||
headers: { | ||
Accept: "application/json", | ||
}, | ||
}, | ||
); | ||
|
||
const github_access_token = response.data.access_token; | ||
return github_access_token; | ||
} catch (err) { | ||
console.error(err); | ||
return null; | ||
} | ||
const github_access_token = response.data.access_token; | ||
return github_access_token; | ||
} catch (err) { | ||
console.error(err); | ||
return null; | ||
} | ||
} | ||
|
||
export async function getGithubUser(token) { | ||
try { | ||
const response = await axios.get("https://api.github.com/user", { | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
}); | ||
try { | ||
const response = await axios.get("https://api.github.com/user", { | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
}); | ||
|
||
return response.data.login; | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
return response.data.login; | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
} | ||
|
||
export async function checkMembership(username) { | ||
try { | ||
await axios.get(`https://api.github.com/orgs/hcp-uw/memberships/${username}`, { | ||
headers: { | ||
Authorization: `Bearer ${HCP_ACCESS_TOKEN}`, | ||
}, | ||
}); | ||
try { | ||
await axios.get( | ||
`https://api.github.com/orgs/hcp-uw/memberships/${username}`, | ||
{ | ||
headers: { | ||
Authorization: `Bearer ${HCP_ACCESS_TOKEN}`, | ||
}, | ||
}, | ||
); | ||
|
||
return true; | ||
} catch (err) { | ||
return false | ||
} | ||
return true; | ||
} catch (err) { | ||
return false; | ||
} | ||
} | ||
|
||
export async function checkAdmin(auth) { | ||
if (auth == null) { | ||
return false; | ||
} | ||
|
||
if (auth == null) { | ||
return false; | ||
} | ||
|
||
let email = auth.email; | ||
let key = email.split("@")[0] | ||
let qRes; | ||
let data; | ||
try { | ||
let q = query(ref(database, "Update/Members")); | ||
qRes = await get(q); | ||
data = qRes.val(); | ||
let member = data[key] | ||
let email = auth.email; | ||
let key = email.split("@")[0]; | ||
let qRes; | ||
let data; | ||
try { | ||
let q = query(ref(database, "Update/Members")); | ||
qRes = await get(q); | ||
data = qRes.val(); | ||
let member = data[key]; | ||
|
||
if (member == null) { | ||
return false; | ||
} | ||
|
||
if (member == null) { | ||
return false; | ||
return member["Club_Lead"]; | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
|
||
|
||
return member["Club_Lead"]; | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
} | ||
} |
Oops, something went wrong.