pub struct Api<'a, D> { /* private fields */ }Expand description
Server-side endpoint calls. Pass the request’s headers (or a session token) for endpoints that need a session.
Implementations§
Source§impl<D: Adapter> Api<'_, D>
impl<D: Adapter> Api<'_, D>
Sourcepub fn db(&self) -> Db<'_, D>
pub fn db(&self) -> Db<'_, D>
Typed queries on the configured adapter, for code outside an endpoint that has an
AuthInstance but no Ctx (an axum extractor reading a plugin’s table).
Sourcepub fn within(self, db: D, events: &DeferredEvents) -> Self
pub fn within(self, db: D, events: &DeferredEvents) -> Self
Runs every call on db instead of the configured adapter (its queries, session
lookup, hooks and gates), and queues the calls’ events on events instead of firing
them.
Each call runs inside its own db.begin(), committed when the call succeeds and
rolled back when it fails, so a failed call leaves db as it was. That needs an
adapter whose begin nests inside its own queries: with sqlx, use .in_tx(&tx) from
fastauth_sqlx::InTx, which binds the adapter to a transaction and makes begin a
savepoint.
Sourcepub fn headers(self, headers: &HeaderMap) -> Self
pub fn headers(self, headers: &HeaderMap) -> Self
Uses these request headers: cookies for the session, user agent and so on.
Sourcepub fn session_token(self, token: &str) -> Self
pub fn session_token(self, token: &str) -> Self
Acts as the session with this raw token.
Sourcepub async fn call<E: Endpoint<D>>(
&self,
endpoint: &E,
input: E::Input,
) -> Result<ApiResponse<E::Output>, AuthError>
pub async fn call<E: Endpoint<D>>( &self, endpoint: &E, input: E::Input, ) -> Result<ApiResponse<E::Output>, AuthError>
Runs any endpoint, with its access check, hooks and events.
Sourcepub async fn create_user(
&self,
input: CreateUserInput,
) -> Result<UserView, AuthError>
pub async fn create_user( &self, input: CreateUserInput, ) -> Result<UserView, AuthError>
Creates a user, plus a credential account when password is set. Server-only: there
is no HTTP route. Validates like sign-up and answers
USER_ALREADY_EXISTS_USE_ANOTHER_EMAIL (422) when the email is taken.