Authentication state in the app is handled using the pocketbase sdk.

WIP

authUtils

The utils/auth.ts contains the authUtils class, which has all of the related functions for handling authentication processing.

login, signup, oauth and logout are the main ones.

pb

The pb export is used everywhere throughout the app for any pocketbase sdk functions.
This is exported from utils/pb.ts, and imported with import { pb } from '#imports

For the authentication state, we check:
pb.authStore.isValid which returns a boolean for if the user is logged in or not.

To get user information from the users table, for example the user id, use: pb.authStore.record?.id

Since this is a store, it updates in real time with the auth state automatically.

middleware

in middleware/auth.ts there is a route guarding middleware places to control app access via routes.

It checks the current app page and a variety of parameters in the user state to either allow the page to load or redirect to the correct page. This middleware runs on every page in the app.

Examples:

  • A user that is unauthenticated will be redirected to the login page at /auth if they try to access any other page.
  • A user that is authenticated, but not yet onboarded, will be redirected to the /auth/onboard page.
  • A user that is authenticated and approved to use the app will be redirected to their homepage if they navigate the /auth login page. Theres a few more cases it handles, so check the file to see the routing checks.