User Models

A user model is a regular model, with the only difference being that it represents a real-world user role in the application. User models have the following features:

Authentication

User models have credential fields that are used for authentication.

A field marked as @publicCredential can be any user identifier that can be used for authenticating the user (usernames, emails, or phone numbers). Public credentials can be used to identify a user publicly among other users.

A field marked with @secretCredential on the other hand, should not be shared with other users, and it is used only in the authentication process, along with a public credential.

note

Pragma encrypts the values of fields annotated with @secretCredential using the application secret before storing them in the database.

Access Control

You can define roles for user models to specify what each kind of user is allowed to perform. See Permissions for more details.

To create a user model, simply annotate a model with the @user directive. For example:

@user @1
model User {
@1 username: String @publicCredential
@2 password: String @secretCredential
}

This tells Pragma to setup authentication flows for the User user model, where the username and password are the user's credentials.

note

You can mark multiple fields with the @publicCredential directive. However, there can only be one @secretCredential field on a model. This allows for functionality such as allowing users to either log in using their email, or their username. For example:

@user @1
model User {
@1 username: String @publicCredential
@2 email: String @publicCredential
@3 password: String @secretCredential
}

See the Generated API section for more details on how to use login queries.