Add login+sign_up routes and auth guard

This commit is contained in:
D. Scott Boggs 2023-06-26 10:59:56 -04:00
parent 792779a36d
commit 2485740291
3 changed files with 82 additions and 4 deletions

View file

@ -3,19 +3,22 @@
use std::default::default;
use bcrypt::*;
// TODO Add option for argon2 https://docs.rs/argon2/latest/argon2/
use either::Either::{self, Left, Right};
use rocket::response::status::Unauthorized;
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
use crate::{
api::ErrorResponder,
error::{self, Error},
};
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
#[sea_orm(table_name = "user")]
pub struct Model {
#[sea_orm(primary_key)]
#[serde(skip_deserializing)]
pub id: i32,
pub name: String,
pub password_hash: String,
@ -27,10 +30,10 @@ pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}
impl ActiveModel {
pub fn new(name: String, password: String) -> error::Result<Self> {
pub fn new(name: impl AsRef<str>, password: impl AsRef<str>) -> error::Result<Self> {
use sea_orm::ActiveValue::Set;
let name = Set(name);
let password_hash = Set(hash(password, DEFAULT_COST + 2)?);
let name = Set(name.as_ref().to_string());
let password_hash = Set(hash(password.as_ref(), DEFAULT_COST + 2)?);
Ok(Self {
name,
password_hash,