From 222616c0c55a600c46e435e6269e40318d1933ba Mon Sep 17 00:00:00 2001 From: H1ghBre4k3r Date: Tue, 5 Nov 2024 17:11:41 +0100 Subject: [PATCH] feat(auth): add user verification and improve session handling 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. --- src/components/navbar.tsx | 6 ++++-- src/contexts/appwrite.tsx | 4 +++- src/contexts/auth.tsx | 20 +++++++++++++++++++- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/components/navbar.tsx b/src/components/navbar.tsx index eec7008..cdb9874 100644 --- a/src/components/navbar.tsx +++ b/src/components/navbar.tsx @@ -32,17 +32,18 @@ export function NavBar() { - - + + Kneipolympics diff --git a/src/contexts/appwrite.tsx b/src/contexts/appwrite.tsx index e4069bb..a6c93f0 100644 --- a/src/contexts/appwrite.tsx +++ b/src/contexts/appwrite.tsx @@ -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, diff --git a/src/contexts/auth.tsx b/src/contexts/auth.tsx index 7bbf111..561881e 100644 --- a/src/contexts/auth.tsx +++ b/src/contexts/auth.tsx @@ -1,6 +1,7 @@ import { createContext, PropsWithChildren, + useCallback, useEffect, useMemo, useState, @@ -28,7 +29,21 @@ export function AuthContextProvider({ children }: PropsWithChildren) { Models.User | 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) => { @@ -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 { const session = await account.createEmailPasswordSession(email, password); @@ -76,6 +91,9 @@ export function AuthContextProvider({ children }: PropsWithChildren) { name: string, ): Promise { 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 = {