Skip to content

Commit

Permalink
feat: add basic login and logout functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
H1ghBre4k3r committed Nov 3, 2024
1 parent 0fd1da7 commit 2fdf5b3
Show file tree
Hide file tree
Showing 11 changed files with 618 additions and 4 deletions.
453 changes: 453 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"eslint-plugin-react-hooks": "^5.0.0",
"eslint-plugin-react-refresh": "^0.4.14",
"globals": "^15.11.0",
"sass-embedded": "^1.80.6",
"typescript": "~5.6.2",
"typescript-eslint": "^8.11.0",
"vite": "^5.4.10"
Expand Down
11 changes: 11 additions & 0 deletions src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
body {
padding: 0;
margin: 0;
}

main {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
5 changes: 4 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ import "./App.css";

import { AppwriteContextProvider } from "./contexts/appwrite";
import { AuthContextProvider } from "./contexts/auth";
import { Page } from "./navigation/page";

export function App() {
return (
<>
<AppwriteContextProvider>
<AuthContextProvider></AuthContextProvider>
<AuthContextProvider>
<Page></Page>
</AuthContextProvider>
</AppwriteContextProvider>
</>
);
Expand Down
18 changes: 18 additions & 0 deletions src/components/navbar.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
nav {
/* border: 1px solid black; */

display: flex;
justify-content: space-between;
margin: 5px;
padding: 10px;
box-shadow: 0px 0px 32px 0px #999;
border-radius: 5px;

ul {
display: flex;
list-style: none;
gap: 10px;
padding: 0;
margin: 0;
}
}
16 changes: 16 additions & 0 deletions src/components/navbar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { useAuth } from "../hooks/useAuth";
import "./navbar.css";

export function NavBar() {
const { user, logout } = useAuth();
return (
<nav>
<ul>
<li>Home</li>
<li>Page</li>
</ul>
<span>{user?.name ?? "FUCK"}</span>
<button onClick={() => logout()}>Logout</button>
</nav>
);
}
42 changes: 39 additions & 3 deletions src/contexts/auth.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import { createContext, PropsWithChildren, useState } from "react";
import {
createContext,
PropsWithChildren,
useEffect,
useMemo,
useState,
} from "react";
import { useAppwrite } from "../hooks/useAppwrite";
import { Account, Models } from "appwrite";

export type AuthContextValue = {
session?: Models.Session;
user?: Models.User<Models.Preferences>;
loggedIn: boolean;
login(username: string, password: string): Promise<void>;
logout(): Promise<void>;
};
Expand All @@ -12,9 +20,27 @@ export const AuthContext = createContext({} as AuthContextValue);

export function AuthContextProvider({ children }: PropsWithChildren) {
const { client } = useAppwrite();
const account = new Account(client);
const account = useMemo(() => new Account(client), [client]);

const [session, setSession] = useState<Models.Session | undefined>();
const [user, setUser] = useState<
Models.User<Models.Preferences> | undefined
>();

useEffect(() => {
account
.getSession("current")
.then((session) => {
account
.get()
.then((user) => {
setSession(session);
setUser(user);
})
.catch(() => account.deleteSession("current"));
})
.catch(console.error);
}, [account]);

async function login(username: string, password: string): Promise<void> {
try {
Expand All @@ -23,7 +49,13 @@ export function AuthContextProvider({ children }: PropsWithChildren) {
password,
);

setSession(session);
account
.get()
.then((user) => {
setSession(session);
setUser(user);
})
.catch(() => account.deleteSession("current"));
} catch (e) {
console.error(e);
}
Expand All @@ -32,6 +64,8 @@ export function AuthContextProvider({ children }: PropsWithChildren) {
async function logout(): Promise<void> {
try {
await account.deleteSession("current");
setSession(undefined);
setUser(undefined);
} catch (e) {
console.error(e);
}
Expand All @@ -41,6 +75,8 @@ export function AuthContextProvider({ children }: PropsWithChildren) {
login,
session,
logout,
user,
loggedIn: !!(session && user),
};

return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
Expand Down
6 changes: 6 additions & 0 deletions src/hooks/useAuth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { useContext } from "react";
import { AuthContext } from "../contexts/auth";

export function useAuth() {
return useContext(AuthContext);
}
11 changes: 11 additions & 0 deletions src/navigation/login.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.login-form {
display: flex;
flex-direction: column;
gap: 20px;

input {
padding: 5px;
border: 1px solid #42423d;
border-radius: 5px;
}
}
40 changes: 40 additions & 0 deletions src/navigation/login.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { FormEvent, useState } from "react";

import "./login.css";
import { useAuth } from "../hooks/useAuth";

export function Login() {
const auth = useAuth();

const [username, setUsername] = useState("");
const [password, setPassword] = useState("");

function onSubmit(e: FormEvent<HTMLFormElement>) {
e.preventDefault();

auth.login(username, password).then(console.log).catch(console.error);
}

return (
<>
<h2>Login</h2>
<form onSubmit={onSubmit} className="login-form">
<input
type="email"
name="username"
placeholder="Username"
value={username}
onChange={(e) => setUsername(e.target.value)}
/>
<input
type="password"
name="password"
placeholder="Password"
value={password}
onChange={(e) => setPassword(e.target.value)}
/>
<button>Login</button>
</form>
</>
);
}
19 changes: 19 additions & 0 deletions src/navigation/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { NavBar } from "../components/navbar";
import { useAuth } from "../hooks/useAuth";
import { Login } from "./login";

export function Page() {
const { loggedIn } = useAuth();
return (
<main>
<h1>Kneipolympics</h1>
{loggedIn ? (
<>
<NavBar></NavBar>
</>
) : (
<Login></Login>
)}
</main>
);
}

0 comments on commit 2fdf5b3

Please sign in to comment.