-
Notifications
You must be signed in to change notification settings - Fork 0
Authentication
HelgaZhizhka edited this page Nov 11, 2025
·
1 revision
-
Session State: Stored in the viewer store (
useViewerStore), which tracks:-
status: initial, loading, authenticated, guest, error -
session: Supabase session object or null -
error: last error
-
-
Persistence: Store is persisted in local storage (
enablePersist: true). -
Initialization: On app start,
useInitViewer:- Loads the current session from Supabase (
getSession) - Subscribes to session changes (
onAuthStateChange) - Updates the viewer store accordingly
- Loads the current session from Supabase (
- Form: Email and password (validated via zod).
-
API Call:
login(callssupabase.auth.signInWithPassword)- On success, session is stored in viewer store.
- On error, error is shown via toast.
- UI: On success, redirect to home.
- Form: Email, password, confirm password (validated via zod).
-
API Call:
signUp(callssupabase.auth.signUp)- On success, user receives a confirmation email.
-
emailRedirectTois set to onboarding step. - If user already exists, error is shown.
- UI: On success, redirect to registration success page.
- The user receives an email with a reference to the confirmation of registration. When you click on a link, it is redirected to the onboarding form.
- Multi-step form: First step - Profile info, Second Step - shipping/billing address
-
API Call:
- All data is validated and on submit send request
updateCustomer(in this case - for create new customer) and requestcreateAddressfor shipping address and for billing if it is exists. - On success, user is redirected to home.
- All data is validated and on submit send request
- Validation: Each step is validated via zod; all data is validated before submission.
- Form: Email (validated via zod).
-
API Call:
resetPassword(callssupabase.auth.resetPasswordForEmail)- On success, user is prompted to check email.
-
redirectTois set to the reset password page.
- Form: Current Password, New password, confirm password (validated via zod).
-
API Call:
changePassword: 1 - check current password; 2- if success -updateUserwith new password- On success, user is redirected to home.
- On error, error is shown via toast.
-
API Call:
logout(callssupabase.auth.signOut) - State Update: Clears session from viewer store and react-query cache.
-
Viewer Status: All protected routes use
authGuardto check viewer status.- If
requireAuthis true and user is not authenticated, redirect to login. - If
requireAuthis false and user is authenticated, redirect to home/profile.
- If
-
Hook:
useAuthRedirectcan be used in components to enforce redirect logic.
- All API calls catch and handle errors, updating the viewer store and showing user-friendly messages via toast.
- Errors are strictly typed and surfaced in the UI.
-
Hooks: All forms use custom hooks (
useLoginForm,useRegistrationForm,useFormStep, etc.) that encapsulate API calls, validation, and navigation. - TanStack Query: Mutations are used for all async actions, with onSuccess/onError handlers.
- Viewer Store: Central point for session and status, used across the app for access control and personalization.
- User submits registration form →
signUp→ Supabase creates user, sends confirmation email. - User confirms email, redirected to onboarding.
- User completes onboarding steps →
updateCustomerandcreateAddress→ Supabase stores customer profile and address data in databases.
- User submits login form →
login→ Supabase authenticates, returns session. - Session is stored in viewer store, user is redirected to home.
- User submits email →
resetPassword→ Supabase sends reset email. - User follows link, submits new password →
updateUser→ Supabase updates password
- All sensitive operations (registration, login, password reset) are handled via Supabase secure endpoints.
- Session tokens are managed by Supabase and never exposed to the frontend directly.
- All user data is validated both client-side (zod) and server-side (Supabase). Supabase Auth Docs