capy-auth 0.10.0

Optional database-backed browser sessions and explicit authorization for Capy.
Documentation
//! Conventional, reversible SeaORM identity schema shared with the installer.
use sea_orm_migration::prelude::*;

/// Creates users, sessions (including short-lived anonymous CSRF sessions), and reset tokens.
/// Applications normally register the migration generated by `capy make:auth`.
#[derive(DeriveMigrationName)]
pub struct IdentityMigration;

#[async_trait::async_trait]
impl MigrationTrait for IdentityMigration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .create_table(
                Table::create()
                    .table(Alias::new("users"))
                    .col(
                        ColumnDef::new(Alias::new("id"))
                            .big_integer()
                            .not_null()
                            .auto_increment()
                            .primary_key(),
                    )
                    .col(
                        ColumnDef::new(Alias::new("name"))
                            .string_len(100)
                            .not_null(),
                    )
                    .col(
                        ColumnDef::new(Alias::new("email"))
                            .string_len(254)
                            .not_null(),
                    )
                    .col(
                        ColumnDef::new(Alias::new("password_hash"))
                            .string()
                            .not_null(),
                    )
                    .col(ColumnDef::new(Alias::new("email_verified_at")).big_integer())
                    .col(
                        ColumnDef::new(Alias::new("created_at"))
                            .big_integer()
                            .not_null(),
                    )
                    .col(
                        ColumnDef::new(Alias::new("updated_at"))
                            .big_integer()
                            .not_null(),
                    )
                    .to_owned(),
            )
            .await?;
        manager
            .create_index(
                Index::create()
                    .name("users_email_unique")
                    .table(Alias::new("users"))
                    .col(Alias::new("email"))
                    .unique()
                    .to_owned(),
            )
            .await?;
        for table in ["sessions", "password_reset_tokens"] {
            let mut statement = Table::create();
            statement.table(Alias::new(table)).col(
                ColumnDef::new(Alias::new("token_hash"))
                    .string_len(64)
                    .not_null()
                    .primary_key(),
            );
            let mut user = ColumnDef::new(Alias::new("user_id"));
            user.big_integer();
            if table == "password_reset_tokens" {
                user.not_null();
            }
            statement
                .col(&mut user)
                .col(
                    ColumnDef::new(Alias::new("created_at"))
                        .big_integer()
                        .not_null(),
                )
                .col(
                    ColumnDef::new(Alias::new("expires_at"))
                        .big_integer()
                        .not_null(),
                )
                .foreign_key(
                    ForeignKey::create()
                        .name(format!("{table}_user_fk"))
                        .from(Alias::new(table), Alias::new("user_id"))
                        .to(Alias::new("users"), Alias::new("id"))
                        .on_delete(ForeignKeyAction::Cascade),
                );
            if table == "sessions" {
                statement
                    .col(
                        ColumnDef::new(Alias::new("csrf_token"))
                            .string_len(64)
                            .not_null(),
                    )
                    .col(
                        ColumnDef::new(Alias::new("last_seen_at"))
                            .big_integer()
                            .not_null(),
                    );
            }
            manager.create_table(statement.to_owned()).await?;
            for column in ["user_id", "expires_at"] {
                manager
                    .create_index(
                        Index::create()
                            .name(format!("{table}_{column}_idx"))
                            .table(Alias::new(table))
                            .col(Alias::new(column))
                            .to_owned(),
                    )
                    .await?;
            }
        }
        Ok(())
    }
    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        for table in ["password_reset_tokens", "sessions", "users"] {
            manager
                .drop_table(Table::drop().table(Alias::new(table)).to_owned())
                .await?;
        }
        Ok(())
    }
}