Users

Manage your product's end users with Spica, from sign-up and login to email and phone verification, password reset, passwordless login and bans.

Users are the end users of your product. They log in with a username and password, a one-time code or magic link, or an OAuth provider, and they reach your data through policies and bucket rules.

The users list in the panel's Access Management section

Create users

POST /passport/user requires an identity or an API key with the passport:user:create action. Users can't call it anonymously, so sign-up goes through your backend, typically an HTTP function:

Terminal
curl -X POST https://api.example.com/api/passport/user \
  -H "Authorization: APIKEY <key>" -H "Content-Type: application/json" \
  -d '{"username": "reader1", "password": "readerPass1"}'
JSON
{"username": "reader1", "policies": [], "_id": "6b0b1adea5611972c29c2631"}
FieldDescription
usernameRequired. At least 3 characters, unique. Often an email address.
passwordRequired. Checked against the password policy.
bannedUntilOptional date until which the user can't log in.
deactivateJwtsBeforeOptional Unix timestamp in seconds. Tokens issued before it are rejected.

Then attach the policies users need, for example a policy that allows reading and writing your app's buckets:

Terminal
curl -X PUT https://api.example.com/api/passport/user/<userId>/policy/<policyId> \
  -H "Authorization: APIKEY <key>"

The number of users can be limited with --passport-user-limit. Account creation is rate limited by the createUser rate limit.

Log in

POST/passport/login
Terminal
curl -X POST https://api.example.com/api/passport/login \
  -H "Content-Type: application/json" \
  -d '{"username": "reader1", "password": "readerPass1"}'
JSON
{"scheme": "USER", "token": "eyJhbGciOiJIUzI1NiIs..."}

Use the token as Authorization: USER <token>. Its payload contains the user's _id, username and policies, which rules can read through auth. The response also sets a refresh token cookie, see Tokens & sessions.

What users can do on their own account

Without any extra policy, a logged-in user can:

EndpointPurpose
GET /passport/user/:idRead their own record.
PUT /passport/user/:id/selfChange their password: {"password": "..."}.
POST /passport/user/:id/start-factor-verification, .../complete-factor-verification, DELETE .../factorsManage two-factor authentication.

Everything else needs a policy. Attaching an email address or phone number to an account requires passport:user:update, so run it from a function with an API key after you have verified the user's token.

Email and phone verification

Users can prove ownership of an email address or a phone number, which then enables password reset and passwordless login through that channel.

Emails are sent through the mailer and SMS through Twilio. Enable the provider and strategy in the user configuration:

JSON
{
  "providerVerificationConfig": [
    {"provider": "email", "strategy": "Otp"},
    {"provider": "phone", "strategy": "Otp"}
  ]
}
StrategyWhat the user receives
OtpA 6-digit code, by default Your verification code is: 123456.
MagicLinkA link to <public-url>/passport/user/verify-magic-link?token=....

Start and complete the verification:

Terminal
curl -X POST https://api.example.com/api/passport/user/<userId>/start-provider-verification \
  -H "Authorization: APIKEY <key>" -H "Content-Type: application/json" \
  -d '{"value": "ada@example.com", "provider": "email", "strategy": "Otp", "purpose": "verification"}'

curl -X POST https://api.example.com/api/passport/user/<userId>/verify-provider \
  -H "Authorization: APIKEY <key>" -H "Content-Type: application/json" \
  -d '{"code": "123456", "provider": "email", "strategy": "Otp", "purpose": "verification"}'

The number of attempts is limited by verificationProcessMaxAttempt, and codes expire after --passport-user-verification-code-expires-in seconds (5 minutes by default).

Password reset

Enable a provider in the user configuration:

JSON
{"resetPasswordProvider": [{"provider": "email", "strategy": "Otp"}]}

Then let the user request a code and set a new password. Neither call needs a token:

Terminal
curl -X POST https://api.example.com/api/passport/user/forgot-password/start \
  -H "Content-Type: application/json" \
  -d '{"username": "reader1", "provider": "email"}'

curl -X POST https://api.example.com/api/passport/user/forgot-password/verify \
  -H "Content-Type: application/json" \
  -d '{"username": "reader1", "code": "123456", "newPassword": "aNewPassword1", "provider": "email"}'

Without the configuration the API responds with Provider or Reset password is not configured properly.

Passwordless login

Enable it in the user configuration:

JSON
{"passwordlessLogin": {"passwordlessLoginProvider": [{"provider": "email", "strategy": "Otp"}]}}
Terminal
curl -X POST https://api.example.com/api/passport/user/passwordless-login/start \
  -H "Content-Type: application/json" \
  -d '{"username": "reader1", "provider": "email"}'

curl -X POST https://api.example.com/api/passport/user/passwordless-login/verify \
  -H "Content-Type: application/json" \
  -d '{"username": "reader1", "code": "123456", "provider": "email"}'

The verify call returns a user token like a normal login. Both endpoints are rate limited by the login rate limit.

Ban users and revoke sessions

GoalHow
Block logins until a datePUT /passport/user/:id with bannedUntil.
Invalidate every token issued so farPUT /passport/user/:id with deactivateJwtsBefore set to the current Unix time.
Remove the accountDELETE /passport/user/:id.

The auth devkit offers ban and deactivateUserTokens helpers.

Manage users

EndpointAction
GET /passport/userpassport:user:index
GET /passport/user/:idpassport:user:show
POST /passport/userpassport:user:create
PUT /passport/user/:idpassport:user:update
DELETE /passport/user/:idpassport:user:delete
PUT /passport/user/:id/policy/:policyIdpassport:user:policy:add
DELETE /passport/user/:id/policy/:policyIdpassport:user:policy:remove

Identities or users?

Use users for anyone who signs up to your product. Use identities only for people who operate the backend. Users never see the panel, and identities never pass through bucket rules.