Sqlite.Auth.User

Add and get users from the database.

User types

type alias User = User

A user that exists in the database. This does not mean the user has been verified via credentials. It is simply a user that exists.

This value is produced by functions that create or look up a user, such as new, get, or Sqlite.Auth.Username.user.

This value is consumed by functions that read a user's id, attach a username to a user, or check a password for a given user, such as id, Sqlite.Auth.Username.list, or Sqlite.Auth.Password.validate.

This value wraps an internal type that cannot be constructed by functions outside of this package. If you want to get access to the underlying user id this type holds, use the id function.

type alias Verified = Verified

A user that exists in the database and has been proven, via a credential check or a live session, that they are the user they say they are. Unlike the User type, which only proves a user exists, this proves the user has been authenticated.

This value is produced by a credential check or a live-session verification, such as Sqlite.Auth.Password.validate, Sqlite.Auth.Password.reset, or Sqlite.Auth.Session.verify.

This value is consumed by privileged account operations, such as delete, Sqlite.Auth.Password.change, or Sqlite.Auth.Session.issue.

This value wraps an internal type that cannot be constructed by functions outside of this package. If you want to get access to the underlying user id this type holds, use the verifiedId function.

Migrations

All migrations are meant to be plug-and-play with the gren-sqlite-migrate package:

Sqlite.Migrate.migrate database
    { secureContext = secureContext }
    (Sqlite.Auth.User.migrationV1 ++ yourOtherMigrations)

If you are using a different method of migration, the individual SQL statements can be extracted from each migration's statements field.

migrationV1 : { name : String, statements : Array String }

Initial migration required for functions in this module to operate.

CREATE TABLE IF NOT EXISTS users (
    id TEXT PRIMARY KEY,
    created_at INTEGER NOT NULL,
    updated_at INTEGER NOT NULL
);

The users table is used as a foreign key reference for all other modules in this package. As such, it is required for most, if not all, of the functions in this package to operate successfully.

Working with users

new : Database -> SecureContext -> Task Error User

Create a new user with a randomly generated UUID v4 identifier.

It's highly recommended that you immediately create a username for the new user in the same SQLite transaction. After a user is created, the only method of retrieving it from the database is by its id (via the get function) or by the user's created username. If you plan on using this package for user-facing authentication, user ids are not the most friendly way to remember how to log into an account!

get : Database -> { id : String } -> Task Error User

Look up a user by id, returning the User for the row. The task fails when there's no user with the given id.

id : User -> String

Get the id of the given User.

verifiedId : Verified -> String

Get the id of a given Verified user.

delete : Database -> Verified -> Task Error {}

Delete a user. This also deletes everything that references them including usernames, passwords, password reset tokens, and sessions, assuming the given database has foreign keys enabled and you've used the migrations in this package which contain ON DELETE CASCADE statements.

This operation requires a Verified user. A user can be verified by validating their password (Sqlite.Auth.Password.validate) or their current session (Sqlite.Auth.Session.verify).

Deleting a user who does not exist succeeds with no changes to the underlying database.