defaultOrganization()
defaultOrganization()
is a post-login middleware, it switches a users organization context to another suborganization.
This middleware allows for you to login at your root organization but then have the user land in a suborganization e.g. their personal organization, their teams organization, their preferred home, etc.
You can specify a known organization id, or provide a function to perform a look up (sync, or async). The lookup function is called with a list of the available organizations for a given user and a reference to the user object; if this doesn't suit your needs you can also write your own default organization resolution algorithm as you see fit.
The defaultOrganization()
middleware is compatible with <Form>
, and the logIn()
function of the useSlashID()
hook.
Usage
Using a static default organization
import { Form } from '@slashid/react'
import { defaultOrganization } from '@slashid/react'
function Component() {
return (
<Form
middleware={[
defaultOrganization("ORG_ID")
]}
/>
)
}
With lookup function
Searching for the organization from the users available organizations
import { Form } from '@slashid/react'
import { defaultOrganization } from '@slashid/react'
function Component() {
return (
<Form
middleware={[
defaultOrganization(({ organizations }) =>
orgs.find(({ org_name }) => org_name === "home") ?? "FALLBACK_ORG_ID"
)
]}
/>
)
}
With custom algorithm
For example: getting the default org from a user attribute you've set
import { Form } from '@slashid/react'
import { defaultOrganization } from '@slashid/react'
function Component() {
return (
<Form
middleware={[
defaultOrganization(async ({ user }) => {
const bucket = user.getBucket()
const { preferred_org_id } = await bucket.get<{ preferred_org_id }>("preferred_org_id")
return preferred_org_id
})
]}
/>
)
}
API
function defaultOrganization (oid): string | Promise<string> { ... }
Parameter | Type | Description |
---|---|---|
oid | string | (context: { organizations: OrganizationDetails[], user: User }) => string | Promise<string>) | The organization id of the users default organization - this can be a string or a function which resolves to a string. When a function is provided it's called with a list of all available organizations for that user, and the user object itself. |