Skip to content

Commit

Permalink
🔐 login context done
Browse files Browse the repository at this point in the history
  • Loading branch information
lachlanshoesmith committed Dec 13, 2024
1 parent 53aa72f commit 0f4268b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
32 changes: 26 additions & 6 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import { DiscordPage } from "./Settings/SettingsPage/DiscordPage/DiscordPage";
import { Unauthenticated } from "./Unauthenticated/Unauthenticated";
import { ProtectedRoute } from "./ProtectedRoute/ProtectedRoute";
import { useEffect, useState } from "react";
import { UserContext } from "./UserContext/UserContext";
import { User, UserContext } from "./UserContext/UserContext";

function App() {
const [user, setUser] = useState({});
const [user, setUser] = useState<User | null>(null);

useEffect(() => {
fetch("http://localhost:5180/user", {
Expand All @@ -37,20 +37,40 @@ function App() {

return (
<BrowserRouter>
<UserContext.Provider value={user}>
<UserContext.Provider value={{ user, setUser }}>
<div className="page">
<Routes>
<Route path="/" element={<HomePage />} />
<Route path="/about" element={<AboutPage />} />
<Route path="/timeline" element={<Calendar />} /> //
<Route path="/login" element={<LoginPage />} />
<Route path="/register" element={<RegisterPage />} />
<Route
path="/login"
element={
<ProtectedRoute
isAuthenticated={user === null}
fallback={<Navigate to="/settings/profile" />}
>
<LoginPage />
</ProtectedRoute>
}
/>
<Route
path="/register"
element={
<ProtectedRoute
isAuthenticated={user === null}
fallback={<Navigate to="/settings/profile" />}
>
<RegisterPage />
</ProtectedRoute>
}
/>
<Route
path="/settings"
element={
// this propagates to all child routes
<ProtectedRoute
isAuthenticated={user.id !== undefined}
isAuthenticated={user !== null && user.id !== undefined}
fallback={<Navigate to="/login" />}
>
<Settings />
Expand Down
10 changes: 8 additions & 2 deletions frontend/src/Login/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@ import { AuthScreen } from "../AuthScreen/AuthScreen";
import { TextInput, TextOptions } from "../TextInput/TextInput";
import { UserCircleIcon } from "@heroicons/react/24/outline";
import { LockClosedIcon } from "@heroicons/react/24/outline";
import { useState, FormEvent } from "react";
import { useState, FormEvent, useContext } from "react";
import { Link, useNavigate } from "react-router";
import { errorHandler, AuthError } from "../errorHandler";
import { UserContext, User } from "../UserContext/UserContext";

export default function LoginPage() {
const [username, setUsername] = useState("");
const [password, setPassword] = useState("");
const [error, setError] = useState<AuthError | undefined>(undefined);
const [success, setSuccess] = useState<string | undefined>(undefined);
const navigate = useNavigate();
const { setUser } = useContext(UserContext);

async function handleSubmit(event: FormEvent<HTMLFormElement>) {
event.preventDefault();
const res = await fetch("http://localhost:5180/auth/login", {
Expand All @@ -30,12 +33,15 @@ export default function LoginPage() {

if (!res.ok) {
setError(errorHandler(json.error));
} else {
} else if (setUser) {
setError(undefined);
setUser(json as User);
setSuccess("Logged in successfully! Redirecting...");
setTimeout(() => {
navigate("/timeline");
}, 1000);
} else {
setError(errorHandler("Couldn't update user object."));
}
}

Expand Down
20 changes: 18 additions & 2 deletions frontend/src/UserContext/UserContext.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
import { createContext } from "react";
import { createContext, Dispatch, SetStateAction } from "react";

export const UserContext = createContext({});
export interface User {
id: number;
username: string;
email: string;
dateJoined: string;
profilePicture: string | undefined;
}

export interface UserContextType {
user: User | null;
setUser: Dispatch<SetStateAction<User | null>> | undefined;
}

export const UserContext = createContext<UserContextType>({
user: null,
setUser: undefined,
});

0 comments on commit 0f4268b

Please sign in to comment.