Skip to content

Commit

Permalink
Implement Auth (#45)
Browse files Browse the repository at this point in the history
* created documentation for createNewEvent

* implemented createNewEvent function

* wrote docs for delete event function

* implemented deleteEvent

* wrote documentation for updateEvent

* fixed required param section

* added updateEvent implementation

* added documentation for createNewLead

* implemented createNewLead

* added documentation for deleteClubLead

* deleteClubLead implemented

* added testing dependency injection

* ran formatter

* wrote documentation for updateClubLead

* implemented updateClubLead

* imported set and remove

* added createNewProject documentation

* implemented createNewProject

* added test dep inj to functions I forgot

* added deleteProject documentation

* implemented deleteEvent

* implemented deleteProject

* New db structure proposal

* wrote test and passed for createEvent

* wrote test for createNewLead

* wrote test for createNewProject

* wrote getEventByName function

* wrote test for updateEvent

* implemented getLeadByName

* wrote test for updateLead

* wrote tests for update functions

* started implementing AdminForm

* Simple Github Auth

* Github Auth Updated

* fetching real data in form and added image uploading and retrieval functions

* started doing image upload/download functions;

* Added Protected Routes

* added pages

* added current image

* hooked up back end and got image upload to work

* Working Auth with member/admin levels

* Working Auth with member/admin privileges

---------

Co-authored-by: elimelt <[email protected]>
Co-authored-by: seunguk <[email protected]>
  • Loading branch information
3 people authored Sep 6, 2023
1 parent e0e7690 commit 0e4f227
Show file tree
Hide file tree
Showing 30 changed files with 2,525 additions and 175 deletions.
59 changes: 59 additions & 0 deletions frontend-v2/Context/AuthContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { createContext, useContext, useEffect, useState } from "react"
import { GithubAuthProvider, GoogleAuthProvider, signInWithPopup } from "firebase/auth"
import { auth } from "back_end/utils"

// @ts-ignore
import { checkAdmin } from "@/utils/api";

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' });

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(() => {
const unsubscribe = auth.onAuthStateChanged(async user => {
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>
)
}
86 changes: 86 additions & 0 deletions frontend-v2/back_end/api/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import axios from 'axios';
import { database } from "../utils/index.js";
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',
},
});

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}`,
},
});

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}`,
},
});

return true;
} catch (err) {
return false
}
}

export async function checkAdmin(auth) {
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]

if (member == null) {
return false;
}

return member["Club_Lead"];
} catch (err) {
console.error(err);
}
}
Loading

0 comments on commit 0e4f227

Please sign in to comment.