Users - Usage Examples

Last Updated: 2026-07-08

Overview

Working examples for creating users and roles, authenticating, and querying the Users domain.

Creating a User with a Password

from nextango.apps.users.models import User

user = User.objects.create_user(
    email='[email protected]',
    name='Jane Doe',
    password='a-strong-password',
)
# user.has_password is True, user.password_set_at is set,
# user.status defaults to 'active'

Assigning a Role

Roles are assigned by writing to user_roles_data, not by creating a role model directly. The role model row is created automatically on save.

from nextango.apps.users.models import User

user = User.objects.create_user(email='[email protected]', name='Sam Lee')
user.user_roles_data = [
    {
        '_type': 'roleEmployee',
        'employeeId': 'EMP-001',
        'pinCode': '1234',
        'hourlyRate': 18.50,
        'storeAssignments': [{'_ref': 'store-sanity-id', '_type': 'reference'}],
    }
]
user.save()

user.employee_roles.first().employee_id  # 'EMP-001'
user.get_all_roles()                     # ['employee']

Removing a role from the array and saving again deletes the corresponding role row.

PIN Login (POS Terminal)

The users app URLconf is mounted at users/auth/, so the absolute path is:

curl -X POST http://localhost:8000/users/auth/pin-login/ \
  -H "Content-Type: application/json" \
  -d '{
    "employee_id": "EMP-001",
    "pin_code": "1234",
    "store_id": "store-uuid-or-sanity-id"
  }'

Response includes an auth token and employee_uuid, the identifier POS transaction creation links against. See Authentication.

Calling PIN Login from a Service (POS Gateway pattern)

from nextango.apps.users.services import UserAuthenticationService

result = UserAuthenticationService.authenticate_pin_login(
    employee_id='EMP-001',
    pin_code='1234',
    store_id='store-uuid-or-sanity-id',
)

if result['success']:
    employee_uuid = result['user']['employee_uuid']
    store = result['store']
else:
    # result['message'] carries the validation error
    pass

Querying Employees by Store

Use the ?store= filter on the root-router user list, which matches employee and manager store_assignments correctly:

curl -H "Authorization: Token <token>" \
  "http://localhost:8000/users/?store=store-uuid-or-sanity-id"

Do not use RoleService.get_employees_by_store() for this: RoleService is currently broken (its queries filter camelCase names that do not exist as model fields, raising FieldError on first call) and has no callers. See Services.

Checking Store Access in a View

from nextango.apps.users.permissions import user_store_gate

def get(self, request, store_id):
    store = Store.objects.get(pk=store_id)
    if not user_store_gate(request.user, store):
        return Response(status=403)
    ...

Creating a Wishlist Collection

from nextango.apps.users.models import UserProductCollection

collection = UserProductCollection.objects.create(
    title='Summer Essentials',
    slug='summer-essentials-jane',
    owner=user,
    collection_type='wishlist',
)
collection.items.add(inventory_level)  # triggers a statistics refresh

Was this page helpful?