Skip to content

Commit

Permalink
feat(auth): add user verification and improve session handling
Browse files Browse the repository at this point in the history
Implement user verification in the AuthContextProvider using a new 
verifyUser function. This function extracts userId and secret from 
URL parameters and calls the account's updateVerification method. 
Update the useEffect dependency to include verifyUser. 

Enhance the Appwrite client setup by specifying the endpoint. 
Add a LocalBar icon to the NavBar for improved UI. 
Ensure the login function creates a verification after user 
registration for better session management.
  • Loading branch information
H1ghBre4k3r committed Nov 5, 2024
1 parent fa6ac0a commit 222616c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/components/navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,18 @@ export function NavBar() {
<AppBar>
<Container maxWidth="lg">
<Toolbar disableGutters>
<Box sx={{ flexGrow: 1, display: "flex", alignItems: "center" }}>
<LocalBar sx={{ mr: 1 }} />
<Box sx={{ flexGrow: 1, display: "flex" }}>
<Typography
variant="h6"
noWrap
component="a"
href="/"
sx={{
display: {
xs: "none",
md: "inline-flex",
},
alignItems: "center",
mr: 2,
fontFamily: "monospace",
fontWeight: 700,
Expand All @@ -51,6 +52,7 @@ export function NavBar() {
textDecoration: "none",
}}
>
<LocalBar sx={{ mr: 1 }} />
Kneipolympics
</Typography>
</Box>
Expand Down
4 changes: 3 additions & 1 deletion src/contexts/appwrite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ type Props = PropsWithChildren;

export function AppwriteContextProvider({ children }: Props) {
const client = new Client();
client.setProject("67257b8d001f6f36d0be");
client
.setEndpoint("https://cloud.appwrite.io/v1")
.setProject("67257b8d001f6f36d0be");

const value = {
client,
Expand Down
20 changes: 19 additions & 1 deletion src/contexts/auth.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
createContext,
PropsWithChildren,
useCallback,
useEffect,
useMemo,
useState,
Expand Down Expand Up @@ -28,7 +29,21 @@ export function AuthContextProvider({ children }: PropsWithChildren) {
Models.User<Models.Preferences> | undefined
>();

const verifyUser = useCallback(async () => {
const params = new URLSearchParams(window.location.search);
const userId = params.get("userId");
const secret = params.get("secret");

if (!(userId && secret)) {
return;
}

await account.updateVerification(userId, secret).catch(() => {});
}, [account]);

useEffect(() => {
verifyUser();

account
.getSession("current")
.then((session) => {
Expand All @@ -41,7 +56,7 @@ export function AuthContextProvider({ children }: PropsWithChildren) {
.catch(() => account.deleteSession("current"));
})
.catch(() => {});
}, [account]);
}, [account, verifyUser]);

async function login(email: string, password: string): Promise<void> {
const session = await account.createEmailPasswordSession(email, password);
Expand Down Expand Up @@ -76,6 +91,9 @@ export function AuthContextProvider({ children }: PropsWithChildren) {
name: string,
): Promise<void> {
await account.create(ID.unique(), email, password, name);
await account.createEmailPasswordSession(email, password);
await account.createVerification(window.location.origin.toString());
await account.deleteSession("current");
}

const value = {
Expand Down

0 comments on commit 222616c

Please sign in to comment.