what2watch/src/login.rs

68 lines
1.7 KiB
Rust
Raw Normal View History

2023-05-28 20:44:50 +00:00
use argon2::PasswordVerifier;
use axum::{
extract::State,
http::StatusCode,
response::{IntoResponse, Response},
};
use axum_login::{secrecy::SecretVec, AuthUser, SqliteStore};
use sqlx::SqlitePool;
use uuid::Uuid;
use crate::{templates::LoginGet, User};
pub type AuthContext = axum_login::extractors::AuthContext<Uuid, User, SqliteStore<User>>;
impl AuthUser<Uuid> for User {
fn get_id(&self) -> Uuid {
self.id
}
fn get_password_hash(&self) -> SecretVec<u8> {
SecretVec::new(self.pwhash.as_bytes().to_vec())
}
}
//-************************************************************************
// Login error and success types
//-************************************************************************
#[Error]
pub struct LoginError(#[from] LoginErrorKind);
#[Error]
#[non_exhaustive]
pub enum LoginErrorKind {
BadPassword,
Unknown,
}
impl IntoResponse for LoginError {
fn into_response(self) -> Response {
match self.0 {
LoginErrorKind::Unknown => (
StatusCode::INTERNAL_SERVER_ERROR,
"An unknown error occurred; you cursed, brah?",
)
.into_response(),
_ => (StatusCode::BAD_REQUEST, format!("{self}")).into_response(),
}
}
}
//-************************************************************************
// Login handlers
//-************************************************************************
/// Handle login queries
#[axum::debug_handler]
pub async fn post_login(
mut auth: AuthContext,
State(pool): State<SqlitePool>,
) -> Result<(), LoginError> {
Err(LoginErrorKind::Unknown.into())
}
pub async fn get_login() -> impl IntoResponse {
LoginGet::default()
}