Identity engine
The Identity engine knows who is signed in: the session, the profile, the workspaces and the permissions. Not everyone is signed in, so every part of this page also says what you get when no one is.
- What it does
- Keeps the session of the person, and holds the user, the workspaces, the preferences and the permissions that the backend gives.
- Exported from the package?
- Yes.
authState,identityState,whenAuthReady,hasPermission,getCurrentWorkspace,switchWorkspace,logoutand the others are exported from@davidnet-net/svelte-ui. AppShell starts the engine throughinit, so do not callinitIdentityEngineyourself.
The engine never fails when no one is signed in. After the first check
authState.isLoggedIn is false, identityState is empty, whenAuthReady() has resolved, hasPermission gives false and getCurrentWorkspace() gives nothing. Signed in, all of it is filled. Build every screen for both states. The examples below work signed out, and the indicator shows which state you are in.Which one do I use?
| What I need | Use |
|---|---|
| Is someone signed in? | authState |
| Wait until I know | whenAuthReady |
| Show the name or a preference | identityState |
| Hide a button someone may not use | hasPermission |
| The active workspace, or switch to another | getCurrentWorkspace, switchWorkspace |
| Sign out | logout |
authState
const authState: { loading: boolean; isBeating: boolean; isLoggedIn: boolean; isStale: boolean }authState is reactive. loading is true until the first check has finished, and during later checks. isBeating is true during a check. isLoggedIn says whether someone is signed in. The session is checked at the start, when the tab becomes visible again and shortly before the access token expires. isStale is true when the last checks gave no clear answer (the network is down, a check takes longer than 10 seconds, or the backend gave a server error): the previous state is kept and the check is retried after 5, 15 and then every 60 seconds. Only a clear answer that the session is invalid signs the person out.
When to use
Use loading to show a placeholder instead of a sign-in button while the first check runs, then isLoggedIn to choose between the signed-in and the signed-out screen.
When not to use, and what to use instead
Do not decide from isLoggedIn alone in the first moments: it is false until the first check is done. Do not write to authState.
Live example
| Call | Result |
|---|---|
| authState.loading | true |
| authState.isBeating | false |
| authState.isLoggedIn | false |
| authState.isStale | false |
authBeat(), a real request to the backend. Signed out, it ends in the same state, and the other examples keep working.{#if authState.loading}
<Skeleton />
{:else if authState.isLoggedIn}
<p>Hello {identityState.user?.displayName}</p>
{:else}
<LinkButton href={accountUrl}>Sign in</LinkButton>
{/if}identityState
const identityState: {
token: accessToken | undefined;
user: user | undefined;
workspaces: workspace[] | undefined;
preferences: preferences | undefined;
privacy: privacyPreferences | undefined;
}identityState holds user, workspaces, preferences, privacy and token. Each is undefined until a session was found and the profile loaded, and again after signing out. rbacState holds the permissions of the active workspace.
When to use
Use it to show the name, the avatar or a preference of the signed-in person.
When not to use, and what to use instead
Always handle undefined: signed out, there is no user. Do not write to it; call syncProfileData() to load it again. Never show the token: it is only for requests.
Live example
| Call | Result |
|---|---|
| identityState.user?.username | none |
| identityState.preferences?.language | none |
| identityState.workspaces?.length | none |
| identityState.token?.raw | none |
{#if identityState.user}
<p>{identityState.user.displayName} ({identityState.user.username})</p>
<p>{identityState.preferences?.language}</p>
{:else}
<p>Not signed in</p>
{/if}whenAuthReady
function whenAuthReady(): Promise<void>whenAuthReady() returns a promise that resolves after the first session check has finished, whether or not someone is signed in. It stays resolved, so awaiting it later returns at once. After it resolves, read authState.isLoggedIn or identityState.user to know which case it is.
When to use
Use it in code that must know whether someone is signed in before it starts: loading the data of the person, or choosing between a private and a public page.
When not to use, and what to use instead
It does not mean that someone is signed in: check identityState.user after it. It does not wait for later checks.
Live example
// Resolves after the first session check, also when nobody is signed in
await whenAuthReady();
if (identityState.user) loadMyBoards();
else showPublicPage();hasPermission
function hasPermission(permissionKey: string, teamId?: string, targetWorkspaceId?: string): booleanhasPermission(key, teamId?, workspaceId?) says whether the person has a permission in a workspace (the active one by default), or in one of its teams. The owner of a workspace has every permission. Signed out there is no workspace, so it gives false.
When to use
Use it to hide or disable what someone may not do.
When not to use, and what to use instead
Hiding a button is no protection: the backend must check the permission again. A false can also mean signed out, or not loaded yet, so do not show an error because of it.
Live example
| Call | Result |
|---|---|
| hasPermission("boards.create") | false |
{#if hasPermission("boards.create")}
<Button>New board</Button>
{/if}getCurrentWorkspace
function getCurrentWorkspace(): workspace | undefined
function switchWorkspace(workspaceID: string, name: string): Promise<void>
function syncWorkspaceAccess(workspaceId: string): Promise<void>getCurrentWorkspace() gives the active workspace, or the personal one when none is chosen, and undefined when no one is signed in. switchWorkspace(id, name) changes the active workspace and shows a toast. syncWorkspaceAccess(id) loads the permissions of another workspace, so hasPermission can answer for it.
When to use
Use getCurrentWorkspace for the name or logo of the workspace, and switchWorkspace for a workspace picker.
When not to use, and what to use instead
Do not call switchWorkspace signed out: it needs the session. Show the picker only when authState.isLoggedIn is true.
Live example
| Call | Result |
|---|---|
| getCurrentWorkspace()?.name | none |
const workspace = getCurrentWorkspace();
await switchWorkspace(otherId, otherName);logout
function logout(): Promise<void>
function clearIdentityData(): Promise<void>logout() empties the identity data in the browser, ends the session at the backend, checks the session again and reloads the page. clearIdentityData() only empties the state in the browser: the session stays. Called signed out, logout() still reloads the page, and there is nothing to end.
When to use
Use it for a sign-out button, and show that button only when someone is signed in.
When not to use, and what to use instead
Do not use clearIdentityData to sign out: it does not end the session. Do not show a sign-out button to someone who is signed out.
Live example
{#if authState.isLoggedIn}
<Button onclick={logout}>Sign out</Button>
{/if}authBeat
function authBeat(): Promise<void>
function syncProfileData(): Promise<void>
function initIdentityEngine(): Promise<void>authBeat() checks the session now. syncProfileData() loads the profile, the preferences and the permissions again. initIdentityEngine() starts the checks and listens for the tab becoming visible again: init calls it.
When to use
Use syncProfileData after you changed the profile of the person, so the app reads the new values. Use authBeat to check the session after something that may have ended it.
When not to use, and what to use instead
Do not call initIdentityEngine in an app with AppShell: it adds a second listener and a second timer. syncProfileData does nothing without a session.
Live example
await authBeat(); // check the session now
await syncProfileData(); // load the profile again