-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add context for current user state, with redirection
- Loading branch information
1 parent
7f2644b
commit 234ccee
Showing
20 changed files
with
287 additions
and
176 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
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,17 @@ | ||
import { APIResult, get, handleAPIError } from "@/api/requests"; | ||
|
||
export interface User { | ||
_id: string; | ||
uid: string; | ||
role: string; | ||
} | ||
|
||
export const getWhoAmI = async (firebaseToken: string): Promise<APIResult<User>> => { | ||
try { | ||
const response = await get("/api/user/whoami", { Authorization: `Bearer ${firebaseToken}` }); | ||
const json = (await response.json()) as User; | ||
return { success: true, data: json }; | ||
} catch (error) { | ||
return handleAPIError(error); | ||
} | ||
}; |
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 |
---|---|---|
|
@@ -7,38 +7,25 @@ import Image from "next/image"; | |
import styles from "@/app/login/page.module.css"; | ||
import { signInWithEmailAndPassword } from "firebase/auth"; | ||
import { initFirebase } from "@/firebase/firebase"; | ||
import { useRouter } from "next/navigation"; | ||
import { useScreenSizes } from "@/util/useScreenSizes"; | ||
import { useScreenSizes } from "@/hooks/useScreenSizes"; | ||
import { useRedirectToHomeIfSignedIn } from "@/hooks/useRedirection"; | ||
import { getWhoAmI } from "@/api/Users"; | ||
|
||
const Login = () => { | ||
const [email, setEmail] = useState(""); | ||
const [password, setPassword] = useState(""); | ||
|
||
const { auth } = initFirebase(); | ||
useRedirectToHomeIfSignedIn(); | ||
|
||
const router = useRouter(); | ||
const { auth } = initFirebase(); | ||
|
||
const { isMobile } = useScreenSizes(); | ||
|
||
const sendTokenToBackend = async (token: string) => { | ||
try { | ||
const response = await fetch(`http://localhost:3001/api/user/whoami`, { | ||
method: "GET", | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
|
||
if (response.ok) { | ||
const userInfo = await response.json(); | ||
console.log(userInfo); | ||
router.push("/dummyPage"); | ||
} else { | ||
console.error("Failed to get user info from JWT Token"); | ||
} | ||
} catch (error) { | ||
console.error("error sending JWT token to backend", error); | ||
const res = await getWhoAmI(token); | ||
if (!res.success) { | ||
// TODO: better error handling (e.g. no network, user not found) | ||
console.error(`Error retrieving current user: ${res.error}`); | ||
} | ||
}; | ||
|
||
|
@@ -47,11 +34,13 @@ const Login = () => { | |
const userCredential = await signInWithEmailAndPassword(auth, email, password); | ||
const token = await userCredential.user?.getIdToken(); | ||
if (!token) { | ||
// TODO: better error handling | ||
console.error("JWT token not retrieved."); | ||
} else { | ||
await sendTokenToBackend(token); | ||
} | ||
} catch (error) { | ||
// TODO: better error handling (e.g. no network, invalid credentials) | ||
console.error("login failed: ", error); | ||
} | ||
}; | ||
|
@@ -62,75 +51,73 @@ const Login = () => { | |
}; | ||
|
||
return ( | ||
<body> | ||
<div className={styles.loginContainer}> | ||
<Image | ||
src="/Images/login_bg.png" | ||
alt="" | ||
layout="fill" | ||
objectFit="cover" | ||
priority | ||
/* Inline styling due to using Image Component*/ | ||
style={{ | ||
position: "absolute", | ||
top: "0", | ||
left: "0", | ||
right: "30", | ||
bottom: "0", | ||
}} | ||
className={styles.backgroundImage} | ||
/> | ||
<div | ||
style={{ | ||
position: "absolute", | ||
top: "0", | ||
left: "0", | ||
right: "0", | ||
bottom: "0", | ||
background: "#232220D9", | ||
}} | ||
/> | ||
<div className={styles.loginBox}> | ||
<div className={styles.logoContainer}> | ||
<div className={styles.logoImage}> | ||
<Image | ||
src="/Images/LoginImage.png" | ||
alt="Logo" | ||
className={styles.image} | ||
width={isMobile ? 130 : 190} | ||
height={isMobile ? 60 : 90} | ||
/> | ||
</div> | ||
<div className={styles.loginContainer}> | ||
<Image | ||
src="/Images/login_bg.png" | ||
alt="" | ||
layout="fill" | ||
objectFit="cover" | ||
priority | ||
/* Inline styling due to using Image Component*/ | ||
style={{ | ||
position: "absolute", | ||
top: "0", | ||
left: "0", | ||
right: "30", | ||
bottom: "0", | ||
}} | ||
className={styles.backgroundImage} | ||
/> | ||
<div | ||
style={{ | ||
position: "absolute", | ||
top: "0", | ||
left: "0", | ||
right: "0", | ||
bottom: "0", | ||
background: "#232220D9", | ||
}} | ||
/> | ||
<div className={styles.loginBox}> | ||
<div className={styles.logoContainer}> | ||
<div className={styles.logoImage}> | ||
<Image | ||
src="/Images/LoginImage.png" | ||
alt="Logo" | ||
className={styles.image} | ||
width={isMobile ? 130 : 190} | ||
height={isMobile ? 60 : 90} | ||
/> | ||
</div> | ||
<div className={styles.welcomeText}>Welcome!</div> | ||
<form onSubmit={handleLogin} className={styles.loginForm}> | ||
<div className={styles.inputGroup}> | ||
<InputField | ||
label="Email" | ||
id="email" | ||
placeholder="e.g. [email protected]" | ||
value={email} | ||
onChange={(e) => setEmail(e.target.value)} | ||
/> | ||
</div> | ||
<div className={styles.inputGroup}> | ||
<InputField | ||
label="Password" | ||
id="password" | ||
placeholder="" | ||
value={password} | ||
onChange={(e) => setPassword(e.target.value)} | ||
type="password" | ||
/> | ||
</div> | ||
<div className={styles.forgotPassword}>Forgot Password?</div> | ||
<button type="submit" className={styles.loginButton}> | ||
Log In | ||
</button> | ||
</form> | ||
</div> | ||
<div className={styles.welcomeText}>Welcome!</div> | ||
<form onSubmit={handleLogin} className={styles.loginForm}> | ||
<div className={styles.inputGroup}> | ||
<InputField | ||
label="Email" | ||
id="email" | ||
placeholder="e.g. [email protected]" | ||
value={email} | ||
onChange={(e) => setEmail(e.target.value)} | ||
/> | ||
</div> | ||
<div className={styles.inputGroup}> | ||
<InputField | ||
label="Password" | ||
id="password" | ||
placeholder="" | ||
value={password} | ||
onChange={(e) => setPassword(e.target.value)} | ||
type="password" | ||
/> | ||
</div> | ||
<div className={styles.forgotPassword}>Forgot Password?</div> | ||
<button type="submit" className={styles.loginButton}> | ||
Log In | ||
</button> | ||
</form> | ||
</div> | ||
</body> | ||
</div> | ||
); | ||
}; | ||
|
||
|
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 was deleted.
Oops, something went wrong.
32 changes: 0 additions & 32 deletions
32
frontend/src/components/VSRIndividual/HeaderBar/styles.module.css
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.