Skip to content

Commit

Permalink
Update README and formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
sidlak-c137 committed Oct 2, 2023
1 parent 2f1f18c commit d970b67
Show file tree
Hide file tree
Showing 36 changed files with 2,217 additions and 2,034 deletions.
113 changes: 55 additions & 58 deletions Context/AuthContext.tsx
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>
);
};
34 changes: 8 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

# Husky Coding Project Official Website

The first iteration of [the Husky Coding Project Official Website](https://hcp-uw.web.app/home) built with [React](https://reactjs.org/) leveraging [Material UI as our components library](https://mui.com/). Hosted with [Firebase](https://firebase.google.com/).
The [the Husky Coding Project Official Website](https://hcp-uw.vercel.app/home) built with [NextJS](https://nextjs.org/) leveraging [ChakraUI](https://chakra-ui.com/) as our components library. Hosted with [Firebase](https://vercel.com/).

<a href="https://github.com/hcp-uw/club-website"><img alt="Latest Release" src="https://img.shields.io/badge/latest-v1.0.0-brightgreen"></a>
<a href="https://hcp-uw.web.app/home"><img alt="Successful Production" src="https://img.shields.io/badge/production-success-brightgreen"></a>
<a href="https://github.com/hcp-uw/club-website"><img alt="Latest Release" src="https://img.shields.io/badge/latest-v2.0.0-brightgreen"></a>
<a href="https://hcp-uw.vercel.app/home"><img alt="Successful Production" src="https://img.shields.io/badge/production-success-brightgreen"></a>

</div>

Expand All @@ -25,27 +25,19 @@ git clone https://github.com/hcp-uw/club-website.git
```sh
cd {HOME}/club-website/
```
4) Install all dependencies using `yarn install`
4) Install all dependencies using `yarn deps`
```sh
yarn install
yarn deps
```
<!-- 5) Run the application using `yarn start`
<!-- 5) Run the application locally using `yarn run dev`
```sh
yarn start
yarn run dev
``` -->
5) Go to club-website/src/back-end/
```sh
cd club-website/src/back-end/
```
6) Install back-end dependencies
```sh
npm install
```

## Running the App Locally

```sh
yarn start
yarn run dev
```

Runs the app in the development mode.\
Expand Down Expand Up @@ -75,32 +67,24 @@ Your app is ready to be deployed!

```sh
yarn lint
# or
npm run lint
```

To apply fixes:

```shell
yarn lint --apply
# or
npm run lint -- --apply
```

To apply suggested fixes:

```shell
yarn lint --apply-suggested
# or
npm run lint -- --apply-suggested
```

### Formatter

```sh
yarn fmt
# or
npm run fmt
```

For more information about Linter and Formatter, please visit https://docs.rome.tools/.
Expand Down Expand Up @@ -135,5 +119,3 @@ $ git push origin v2.0.0
<a href="https://github.com/hcp-uw/club-website/graphs/contributors">
<img src="https://contrib.rocks/image?repo=hcp-uw/club-website" />
</a>

This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
130 changes: 65 additions & 65 deletions back_end/api/auth.js
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);
}
}
}
Loading

0 comments on commit d970b67

Please sign in to comment.