Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Your own tables in the same transaction

use auth_chad::core::CommitError;
use auth_chad::signup::Created;
use auth_chad::sqlite::SqliteStore;
use auth_chad::axum::{Hooks, Refusal};
use auth_chad::{Code, Write};

#[derive(Clone)]
struct App;

impl Hooks<SqliteStore> for App {
    async fn commit_signup(
        &self,
        store: &SqliteStore,
        created: &Created,
        app: &serde_json::Value,
        writes: Vec<Write>,
    ) -> Result<(), Refusal> {
        let user = created.user.id.0;
        let color = app["color"].as_str().unwrap_or("blue").to_owned();   // the signup's own "app" field
        if color.len() > 20 {
            return Err(Code::InvalidRequest.into());
        }
        store
            .commit_with(writes, move |tx| {
                tx.execute("INSERT INTO profiles (owner, color) VALUES (?1, ?2)", (user, color))
                    .map(|_| ())
                    .map_err(|_| CommitError::Backend)
            })
            .await?;
        Ok(())
    }
}

profiles.owner can be BLOB REFERENCES auth_users (id). If your check or your insert fails, the user is not created. The signup’s JSON can carry your own data in "app". To refuse with your own code, return Refusal::App(StatusCode::CONFLICT, "name_taken"): the client gets that status and {"error": "name_taken"}.

An application with its own connections calls auth_chad::sqlite::apply(&tx, &sql, writes) or auth_chad::postgres::apply(&tx, &sql, writes).await inside its own transaction. get and find read with any connection.