backend: working auth
This commit is contained in:
parent
8c3becf843
commit
ab22d6d830
9 changed files with 172 additions and 10 deletions
|
@ -3,10 +3,14 @@ pub use sea_orm_migration::prelude::*;
|
|||
pub struct Migrator;
|
||||
|
||||
mod m20240705_100914_create_user;
|
||||
mod m20240708_085852_create_admin_user;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl MigratorTrait for Migrator {
|
||||
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
|
||||
vec![Box::new(m20240705_100914_create_user::Migration)]
|
||||
vec![
|
||||
Box::new(m20240705_100914_create_user::Migration),
|
||||
Box::new(m20240708_085852_create_admin_user::Migration),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
39
crates/migration/src/m20240708_085852_create_admin_user.rs
Normal file
39
crates/migration/src/m20240708_085852_create_admin_user.rs
Normal file
|
@ -0,0 +1,39 @@
|
|||
use argon2::password_hash::rand_core::OsRng;
|
||||
use argon2::password_hash::SaltString;
|
||||
use argon2::Argon2;
|
||||
use argon2::PasswordHasher;
|
||||
use sea_orm_migration::prelude::*;
|
||||
|
||||
#[derive(DeriveMigrationName)]
|
||||
pub struct Migration;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl MigrationTrait for Migration {
|
||||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
let argon2 = Argon2::default();
|
||||
let salt = SaltString::generate(OsRng);
|
||||
let hash = argon2
|
||||
.hash_password("admin".as_bytes(), &salt)
|
||||
.expect("Generating Admin Password Hash failed");
|
||||
let insert = Query::insert()
|
||||
.into_table(User::Table)
|
||||
.columns([User::Name, User::Email, User::Hash])
|
||||
.values_panic(["admin".into(), "admin".into(), hash.to_string().into()])
|
||||
.to_owned();
|
||||
|
||||
manager.exec_stmt(insert).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn down(&self, _manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(DeriveIden)]
|
||||
enum User {
|
||||
Table,
|
||||
Name,
|
||||
Email,
|
||||
Hash,
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue