Back to Spree

Users

docs/developer/core-concepts/users.mdx

5.4.28.0 KB
Original Source

Overview

Spree has two separate user types:

  • Customers — users who browse your store, manage their account, and purchase products via the Store API
  • Admins — users who manage the store via the Admin Panel
User typeClassDefault
CustomersSpree.user_classSpree::User
AdminsSpree.admin_user_classSpree::AdminUser
<Info> You can use your own user classes. See the [Customize Authentication guide](/developer/customization/authentication) for details. </Info>

Customers

Customers interact with your store through the Store API. They can register, log in, manage their profile, and view order history.

mermaid
erDiagram
    Customer ||--o{ Order : "places"
    Customer ||--o{ Address : "has many"
    Customer ||--o{ Wishlist : "has many"
    Customer ||--o{ StoreCredit : "has many"
    Customer ||--o{ PaymentSource : "has many"

    Customer {
        string id
        string email
        string first_name
        string last_name
    }

    Order {
        string number
        string state
    }

    Address {
        string firstname
        string lastname
        string address1
        string city
    }

    Wishlist {
        string name
        boolean is_default
    }

Registration

<CodeGroup>
typescript
const { token, user } = await client.customers.create({
  email: '[email protected]',
  password: 'password123',
  password_confirmation: 'password123',
  first_name: 'John',
  last_name: 'Doe',
})
// token => JWT token for subsequent authenticated requests
// user => { id: "usr_xxx", email: "[email protected]", first_name: "John", ... }
bash
curl -X POST 'https://api.mystore.com/api/v3/store/customers' \
  -H 'X-Spree-Api-Key: pk_xxx' \
  -H 'Content-Type: application/json' \
  -d '{
    "email": "[email protected]",
    "password": "password123",
    "password_confirmation": "password123",
    "first_name": "John",
    "last_name": "Doe"
  }'
</CodeGroup>

Login

<CodeGroup>
typescript
const { token, user } = await client.auth.login({
  email: '[email protected]',
  password: 'password123',
})
// Use the token for authenticated requests
bash
curl -X POST 'https://api.mystore.com/api/v3/store/auth/login' \
  -H 'Authorization: Bearer pk_xxx' \
  -H 'Content-Type: application/json' \
  -d '{
    "email": "[email protected]",
    "password": "password123"
  }'
</CodeGroup>

The response includes a JWT token and a user object. Pass the token in subsequent requests via the Authorization: Bearer <token> header.

Token Refresh

Refresh an expiring token to keep the session alive:

<CodeGroup>
typescript
const { token } = await client.auth.refresh({
  token: existingToken,
})
bash
curl -X POST 'https://api.mystore.com/api/v3/store/auth/refresh' \
  -H 'Authorization: Bearer <jwt_token>'
</CodeGroup>

Customer Profile

<CodeGroup>
typescript
// Get current customer
const customer = await client.customer.get()
// {
//   id: "usr_xxx",
//   email: "[email protected]",
//   first_name: "John",
//   last_name: "Doe",
//   default_shipping_address: { ... },
//   default_billing_address: { ... },
//   addresses: [{ ... }, { ... }],
// }

// Update profile
const updated = await client.customer.update({
  first_name: 'Jonathan',
  accepts_email_marketing: true,
})
bash
# Get current customer
curl 'https://api.mystore.com/api/v3/store/customer' \
  -H 'Authorization: Bearer <jwt_token>'

# Update profile
curl -X PATCH 'https://api.mystore.com/api/v3/store/customer' \
  -H 'Authorization: Bearer <jwt_token>' \
  -H 'Content-Type: application/json' \
  -d '{ "first_name": "Jonathan", "accepts_email_marketing": true }'
</CodeGroup>

Customer Resources

Authenticated customers have access to these resources:

ResourceDescription
AddressesBilling and shipping addresses with default selection
OrdersPast order history
Credit CardsSaved credit cards for checkout
Payment SourcesOther saved payment methods (PayPal, Klarna, etc.)
Store CreditsBalance assigned by the store, usable at checkout
Gift CardsGift cards owned by or assigned to the customer
WishlistsSaved product lists

Guest Checkout

Customers don't need to register to purchase. Guest checkout uses an order token (X-Spree-Token) to identify the cart. See Orders — Cart for details.

Admin Users

Admin users manage the store via the Admin Panel. They have roles that control what they can access.

mermaid
erDiagram
    AdminUser ||--o{ RoleUser : "has many"
    RoleUser }o--|| Role : "belongs to"
    RoleUser }o--|| Store : "scoped to"
    AdminUser ||--o{ Invitation : "invites"

    AdminUser {
        string id
        string email
    }

    RoleUser {
        string role_id
        string resource_type
        string resource_id
    }

    Role {
        string name
    }

    Invitation {
        string email
        string status
        string token
        datetime expires_at
    }

Roles

Admin users can have different roles that control their permissions:

RoleDescription
adminFull access to all Admin Panel features
<Info> You can create custom roles with specific permissions. See the [Customize Permissions guide](/developer/customization/permissions) for details. </Info>

Creating Admin Users

Use the Spree CLI to create admin users:

bash
spree user create

The CLI will prompt you for the email and password. You can also pass them directly:

bash
spree user create --email [email protected] --password secret123

The created user gets the admin role on the default store.

Inviting Admin Users

You can invite new admins through the Admin Panel or programmatically.

Via Admin Panel:

  1. Navigate to Settings → Users
  2. Click Invite User
  3. Enter the email address and select a role
  4. Click Send Invitation

The invitee receives an email with an invitation link. If they already have an account, they log in to accept. Otherwise, they create an account first.

mermaid
flowchart TB
    A[Admin creates invitation] --> B[Invitation email sent]
    B --> C[Invitee clicks link]
    C --> D{Has account?}
    D -->|Yes| E[Log in]
    D -->|No| F[Create account]
    E --> G[Accept invitation]
    F --> G
    G --> H[Role assigned to store]

Invitation Details

AttributeDescription
emailInvitee's email address
tokenSecure token for the invitation link
statuspending or accepted
expires_atExpiration date (default: 2 weeks)
resourceThe store being granted access to
roleThe role to assign upon acceptance

Invitation Events

The invitation system publishes events you can subscribe to:

EventDescription
invitation.createdInvitation was created (triggers email)
invitation.acceptedInvitation was accepted and role assigned
invitation.resentInvitation was resent to the invitee

Permissions

Spree uses CanCanCan for authorization. Permissions apply to both customers (Store API access) and admins (Admin Panel access).

See the Customize Permissions guide for details on creating custom roles and permission sets.