-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented Github OAuth feature (#158)
* implemented github oauth feature * ran make format and rebased from master branch * removed unused dependency fastapi-sessions * CONTRIBUTING.md documentation for Github OAuth configuration and replacing Button with TCButton in Login and RegistrationEmail components * Multi-line for condition expression in add_user function * decrease line width for api calls in test * Add some helpful conditioning/tips for OAuth exclusive users * fixed most of static checks * fixed all static check errors --------- Co-authored-by: shaunchua <[email protected]> Co-authored-by: Dennis Chen <[email protected]>
- Loading branch information
1 parent
1276119
commit a7f467c
Showing
19 changed files
with
351 additions
and
59 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
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 |
---|---|---|
|
@@ -2,7 +2,7 @@ import TCButton from "components/files/TCButton"; | |
import { useAlertQueue } from "hooks/alerts"; | ||
import { api } from "hooks/api"; | ||
import { useAuthentication } from "hooks/auth"; | ||
import { FormEvent, useEffect, useState } from "react"; | ||
import { FormEvent, useState } from "react"; | ||
import { Col, Form, Offcanvas, Row } from "react-bootstrap"; | ||
import { Link } from "react-router-dom"; | ||
|
||
|
@@ -14,8 +14,6 @@ interface Props { | |
const Sidebar = ({ show, onHide }: Props) => { | ||
const { addAlert } = useAlertQueue(); | ||
|
||
const [needToCall, setNeedToCall] = useState<boolean>(true); | ||
const [email, setEmail] = useState<string>(""); | ||
const auth = useAuthentication(); | ||
const auth_api = new api(auth.api); | ||
|
||
|
@@ -54,19 +52,6 @@ const Sidebar = ({ show, onHide }: Props) => { | |
} | ||
}; | ||
|
||
useEffect(() => { | ||
(async () => { | ||
if (needToCall) { | ||
setNeedToCall(false); | ||
try { | ||
const res = await auth_api.me(); | ||
setEmail(res.email); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
} | ||
})(); | ||
}, []); | ||
return ( | ||
<Offcanvas show={show} onHide={onHide} placement="end"> | ||
<Offcanvas.Header closeButton> | ||
|
@@ -84,7 +69,14 @@ const Sidebar = ({ show, onHide }: Props) => { | |
<p> | ||
<strong>Change Email</strong> | ||
</p> | ||
<p>Current email: {email}</p> | ||
{auth.email == "[email protected]" ? ( | ||
<p> | ||
No email address associated with this account. (This is because | ||
you registered via OAuth.) | ||
</p> | ||
) : ( | ||
<p>Current email: {auth.email}</p> | ||
)} | ||
{changeEmailSuccess ? ( | ||
<p>An email has been sent to your new email address.</p> | ||
) : ( | ||
|
@@ -109,6 +101,10 @@ const Sidebar = ({ show, onHide }: Props) => { | |
<p> | ||
<strong>Change Password</strong> | ||
</p> | ||
<p> | ||
You may only change your password if you have a previous password. | ||
If not, log out and reset your password. | ||
</p> | ||
{changePasswordSuccess ? ( | ||
<p>Your password has been changed.</p> | ||
) : ( | ||
|
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 |
---|---|---|
@@ -1,6 +1,12 @@ | ||
import axios, { AxiosInstance } from "axios"; | ||
import { BACKEND_URL } from "constants/backend"; | ||
import { createContext, ReactNode, useCallback, useContext } from "react"; | ||
import { | ||
createContext, | ||
ReactNode, | ||
useCallback, | ||
useContext, | ||
useState, | ||
} from "react"; | ||
import { useNavigate } from "react-router-dom"; | ||
|
||
const AUTH_KEY_ID = "AUTH"; | ||
|
@@ -9,8 +15,9 @@ const getLocalStorageAuth = (): string | null => { | |
return localStorage.getItem(AUTH_KEY_ID); | ||
}; | ||
|
||
export const setLocalStorageAuth = (email: string) => { | ||
localStorage.setItem(AUTH_KEY_ID, email); | ||
// changed from email to id to accommodate oauth logins that don't use email | ||
export const setLocalStorageAuth = (id: string) => { | ||
localStorage.setItem(AUTH_KEY_ID, id); | ||
}; | ||
|
||
export const deleteLocalStorageAuth = () => { | ||
|
@@ -20,8 +27,11 @@ export const deleteLocalStorageAuth = () => { | |
interface AuthenticationContextProps { | ||
logout: () => void; | ||
isAuthenticated: boolean; | ||
email: string | null; | ||
setIsAuthenticated: React.Dispatch<React.SetStateAction<boolean>>; | ||
id: string | null; | ||
api: AxiosInstance; | ||
email: string; | ||
setEmail: React.Dispatch<React.SetStateAction<string>>; | ||
} | ||
|
||
const AuthenticationContext = createContext< | ||
|
@@ -37,8 +47,11 @@ export const AuthenticationProvider = (props: AuthenticationProviderProps) => { | |
|
||
const navigate = useNavigate(); | ||
|
||
const isAuthenticated = getLocalStorageAuth() !== null; | ||
const email = getLocalStorageAuth(); | ||
const [isAuthenticated, setIsAuthenticated] = useState<boolean>( | ||
getLocalStorageAuth() !== null, | ||
); | ||
const [email, setEmail] = useState<string>("[email protected]"); | ||
const id = getLocalStorageAuth(); | ||
|
||
const api = axios.create({ | ||
baseURL: BACKEND_URL, | ||
|
@@ -62,8 +75,11 @@ export const AuthenticationProvider = (props: AuthenticationProviderProps) => { | |
value={{ | ||
logout, | ||
isAuthenticated, | ||
email, | ||
setIsAuthenticated, | ||
id, | ||
api, | ||
email, | ||
setEmail, | ||
}} | ||
> | ||
{children} | ||
|
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
Oops, something went wrong.