Auth Stores

We provided a few helpers to make it easier to work with users and roles. Below is the comprehensive documentation for the stores available in our application, designed to facilitate the management and access of user and team data.

User

import { user } from "$lib/stores/auth";
import { team } from "$lib/stores/auth";
import { role } from "$lib/stores/auth";
import { personalTeam } from "$lib/stores/auth";
import { isPersonalTeam } from "$lib/stores/auth";
import { memberships } from "$lib/stores/auth";
import { isSuperAdmin } from "$lib/stores/auth";
import { subscription } from "$lib/stores/auth";
import { is } from "$lib/stores/auth";
import { isSelf } from "$lib/stores/auth";
  • user - The current user.
  • team - The current team.
  • role - The current user’s role.
  • personalTeam - The current user’s personal team.
  • isPersonalTeam - A boolean value indicating whether the current team is the user’s personal team.
  • memberships - The current user’s team memberships.
  • isSuperAdmin - A boolean value indicating whether the current user is a super admin.
  • subscription - The current user’s subscription.
  • is - A function to check if the current user has the required role.
  • isSelf - A function to check if the current user is the same as the provided user ID.

Examples

Show component only if user is admin

component.svelte
import { isSuperAdmin } from "$lib/stores/auth";

{#if $isSuperAdmin}
  <AdminComponent />
{/if}

Show component only if user is a member of the team

component.svelte
import { is } from "$lib/stores/auth";

{#if $is(["MEMBER", "BILLING"])}
  <MemberComponent />
{/if}

Show component only if user is the same as the provided user ID

component.svelte
import { isSelf } from "$lib/stores/auth";

{#if $isSelf(user.id)}
  <SelfComponent />
{/if}

Additional Middlewares

While those stores help you shape your frontend, you can also use the following middlewares to protect your routes.

trpc/routes/file.ts
import { auth } from '../middleware/auth'
import { role } from '../middleware/role'

// Example of ROLE middleware
delete: t.procedure.use(role('OWNER')).mutation(async ({ ctx }) => {})

// Example of AUTH middleware
delete: t.procedure.use(auth).mutation(async ({ ctx }) => {})

For more information on how to use the middleware, please refer to the tRPC middlewares documentation.