Auth.Session

Auth

Signed cookie sessions and opaque session-id cookie helpers.

**Signed claims** (default): mint an HS256 JWT into a cookie with purpose = "session". **Opaque**: set/read a raw session secret (todo-app style); the app owns DB lookup and RLS.

Types

Config

type Config = Config { secret : String
, cookieName : String
, ttlSeconds : Int
, attrs : { httpOnly : Bool
, secure : Bool
, path : String
, sameSite : String
, maxAge : Maybe Int
}
}

Session cookie configuration.

Examples

Auth.Session.config
    { secret = "…", cookieName = "_session", ttlSeconds = 86400 }

Values

config

config : { secret : String , cookieName : String , ttlSeconds : Int } -> Config

Build a session config. Uses Cookies.defaults for cookie attributes (HttpOnly, Path=/, SameSite=Lax).

Examples

session =
    Auth.Session.config
        { secret = env.secret
        , cookieName = "_session"
        , ttlSeconds = 86400
        }

withAttrs

withAttrs : { httpOnly : Bool , secure : Bool , path : String , sameSite : String , maxAge : Maybe Int } -> Config -> Config

Override Set-Cookie attributes (for example Secure on HTTPS).

Examples

Auth.Session.withAttrs Cookies.secureDefaults session

sign

sign : Config -> claims -> Header

Mint a signed session cookie header for claims.

Examples

Auth.Session.sign session { userId = "42" }

verify

verify : Config -> Request -> Maybe claims

Read and verify a signed session cookie. Returns claims when the JWT is valid and purpose is session.

Examples

Auth.Session.verify session req

clear

clear : Config -> Header

Clear the session cookie.

Examples

Auth.Session.clear session

putOpaque

putOpaque : Config -> String -> Header

Set an opaque session id / secret as the cookie value.

Examples

Auth.Session.putOpaque session sessionId

getOpaque

getOpaque : Config -> Request -> Maybe String

Read the opaque cookie value (no crypto).

Examples

Auth.Session.getOpaque session req