pub struct Ctx<D, A = Public> { /* private fields */ }Expand description
The per-request context. Cheap to clone; hooks and event handlers get their own copy.
Implementations§
Source§impl<D: Adapter, A> Ctx<D, A>
impl<D: Adapter, A> Ctx<D, A>
Sourcepub fn db_on<'t, Q: Queries>(&'t self, queries: &'t Q) -> Db<'t, Q>
pub fn db_on<'t, Q: Queries>(&'t self, queries: &'t Q) -> Db<'t, Q>
Typed queries inside a transaction: let tx = ctx.adapter().begin().await?; ctx.db_on(&tx).
Sourcepub fn adapter(&self) -> &D
pub fn adapter(&self) -> &D
The adapter this call runs on: the configured one, or the one a server API call
passed to Api::within (for sqlx, one bound to the app’s transaction).
pub fn schema(&self) -> &Schema
pub fn keys(&self) -> &Keys
pub fn config(&self) -> &CoreConfig
pub fn passwords(&self) -> &Passwords
pub fn now(&self) -> OffsetDateTime
Sourcepub fn new_id(&self) -> Result<Id, AuthError>
pub fn new_id(&self) -> Result<Id, AuthError>
A new id from the configured IdStrategy. Fails only
when a custom generator returns an invalid id: a 500 whose detail is logged.
pub fn request(&self) -> &RequestInfo
pub fn headers(&self) -> &HeaderMap
pub fn ip(&self) -> Option<&str>
pub fn user_agent(&self) -> Option<&str>
Sourcepub fn param(&self, name: &str) -> Option<&str>
pub fn param(&self, name: &str) -> Option<&str>
A path parameter, e.g. {token} in /reset-password/{token}.
Sourcepub fn plugin<P: Plugin>(&self) -> &P
pub fn plugin<P: Plugin>(&self) -> &P
A plugin’s config. Panics if it isn’t registered, which Plugin::Requires rules out
for dependencies. Use try_plugin for optional ones.
pub fn try_plugin<P: Plugin>(&self) -> Option<&P>
Sourcepub fn emit<E: Event>(&self, event: E)
pub fn emit<E: Event>(&self, event: E)
Queues an event; handlers run after the endpoint succeeds.
Sets {prefix}.{name}, signed with the cookie key.
Reads {prefix}.{name} and checks its signature.
Sourcepub fn redirect(&self, url: impl Into<String>)
pub fn redirect(&self, url: impl Into<String>)
Answer with a 302 to url instead of JSON. url must pass
CoreConfig::is_safe_redirect; Location is the URL it resolves to, and an unsafe
url answers 403 INVALID_CALLBACK_URL instead.
pub fn set_header(&self, name: HeaderName, value: HeaderValue)
Sourcepub fn internal(&self) -> Internal<'_, D, A>
pub fn internal(&self) -> Internal<'_, D, A>
Core operations plugins build on: users, sessions, sign-in, verification values, email.
Sourcepub async fn sensitive_session(&self) -> Result<SessionView, AuthError>
pub async fn sensitive_session(&self) -> Result<SessionView, AuthError>
The request’s session read again from the database, for endpoints that create or
change a credential (API keys, passkeys, linked accounts, two-factor). Skips the
cookie cache, so a revoked session is refused at once, and runs the session gates
with SessionUse::Sensitive, where plugins refuse sessions that shouldn’t mint
credentials (admin: impersonation sessions). 401 without a stored session, including
ephemeral ones such as an API key’s.