Token

final class Token : Model, @unchecked Sendable
extension Token: ModelTokenAuthenticatable

A Token model associates a randomly generated string with a User.

A Token is generated upon each login, and revoked by deletion upon each logout. The .token value is used in between those events in an HTTP Bearer Authorization header field to authenticate the user sending API requests. There is no identifying user info in a .token; the association to a specific User is done internally on the API server through this model.

Properties

  • id

    The Token’s ID, provisioned automatically.

    Declaration

    Swift

    @Token
    .ID var id: UUID? { get set }
  • The generated token value.

    Declaration

    Swift

    @Token
    .Field var token: String { get set }
  • The User associated to the Token.

    Declaration

    Swift

    @Token
    .Parent var user: User { get set }
  • Timestamp of the model’s creation, set automatically.

    Declaration

    Swift

    @Token
    .Timestamp var createdAt: Date? { get set }

Initializaton

Methods

  • Creates a new random Token.

    Declaration

    Swift

    static func generate(for user: User, length: Int = 16) throws -> Token

    Parameters

    user

    The User to be associated with this Token.

    length

    Desired length of token data, defaults to 16.

    Return Value

    A Token object.