Skip to main content

Registrar

Trait Registrar 

Source
pub trait Registrar:
    Send
    + Sync
    + 'static {
    type Input: FormInput;
    type User: PanelUser;

    // Required method
    fn register(
        &self,
        cx: &Cx,
        input: Self::Input,
        ex: &mut dyn Executor,
    ) -> impl Future<Output = Result<Self::User>> + Send;

    // Provided method
    fn validate(
        &self,
        _cx: &Cx,
        _input: &Self::Input,
        _ex: &mut dyn Executor,
    ) -> impl Future<Output = Result<FieldErrors>> + Send { ... }
}
Expand description

How a panel creates accounts from its sign-up page, installed with Auth::registration.

It mirrors an Action with input: Input is the form the page asks for, validate refuses a submission field by field, and register writes the account. Both run in one transaction the framework opens, so a uniqueness probe in validate sees what register writes against.

The shipped PasswordSignUp registers an AdminUser from a SignUp; an app with its own user table implements it beside its Authenticator, for the same user type:

struct StaffAuth;

impl Registrar for StaffAuth {
    type Input = SignUp;
    type User = Staff;

    async fn validate(
        &self,
        _cx: &Cx,
        sign_up: &SignUp,
        ex: &mut dyn toasty::Executor,
    ) -> topcoat::Result<FieldErrors> {
        let mut errors = sign_up.password_errors();
        let taken = Staff::filter_by_email(&sign_up.email)
            .first()
            .exec(ex)
            .await?;
        if taken.is_some() {
            errors.add_field("email", "Email has already been taken");
        }
        Ok(errors)
    }

    async fn register(
        &self,
        _cx: &Cx,
        sign_up: SignUp,
        ex: &mut dyn toasty::Executor,
    ) -> topcoat::Result<Staff> {
        Ok(toasty::create!(Staff {
            email: sign_up.email,
            password_hash: hash_password(&sign_up.password).await?,
            name: sign_up.name,
        })
        .exec(ex)
        .await?)
    }
}

A new account that may access the panel is signed in and lands on the panel; one that may not, such as an account awaiting approval, lands on the login page with a notification saying it was created.

Required Associated Types§

Source

type Input: FormInput

What the sign-up page asks for: SignUp, or the app’s own #[derive(FormInput)] struct. #[form(password)] masks a field, and #[form(email)] checks an address.

Source

type User: PanelUser

The account a sign-up creates: the type the panel’s Authenticator loads, which mounting checks.

Required Methods§

Source

fn register( &self, cx: &Cx, input: Self::Input, ex: &mut dyn Executor, ) -> impl Future<Output = Result<Self::User>> + Send

Create the account from a validated input through the transaction ex, which commits when it returns Ok.

Provided Methods§

Source

fn validate( &self, _cx: &Cx, _input: &Self::Input, _ex: &mut dyn Executor, ) -> impl Future<Output = Result<FieldErrors>> + Send

Refuse a submission register should not receive, such as an email another account holds: each error names an input field’s key and renders under its control. Defaults to none. A key no control renders is a declaration error: the page would show no message.

It runs inside the sign-up’s transaction ex, after the input parses and its controls’ own rules pass.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§