user-auth store test2
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
mod mutation;
|
||||
mod query;
|
||||
mod userstore;
|
||||
|
||||
pub use mutation::*;
|
||||
pub use query::*;
|
||||
pub use userstore::*;
|
||||
|
||||
pub use sea_orm;
|
||||
|
||||
@@ -26,6 +26,11 @@ impl Query {
|
||||
Book::find_by_id(id).one(db).await
|
||||
}
|
||||
|
||||
|
||||
pub async fn find_user_by_id(db: &DbConn, id: i32) -> Result<Option<user::Model>, DbErr> {
|
||||
User::find_by_id(id).one(db).await
|
||||
}
|
||||
|
||||
pub async fn list_all_users(db: &DbConn) -> Result<Vec<user::Model>, DbErr> {
|
||||
User::find().all(db).await
|
||||
}
|
||||
|
||||
62
backend/orm/src/userstore.rs
Normal file
62
backend/orm/src/userstore.rs
Normal file
@@ -0,0 +1,62 @@
|
||||
use sea_orm::*;
|
||||
use axum::async_trait;
|
||||
use axum_login::{secrecy::SecretVec, AuthUser, UserStore};
|
||||
use crate::Query;
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct AxumUser {
|
||||
pub id: i32,
|
||||
pub password_hash: String,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
type Result<T = ()> = std::result::Result<T, eyre::Error>;
|
||||
|
||||
impl<Role> AuthUser<Role> for AxumUser
|
||||
where
|
||||
Role: PartialOrd + PartialEq + Clone + Send + Sync + 'static,
|
||||
{
|
||||
fn get_id(&self) -> String {
|
||||
format!("{}", self.id)
|
||||
}
|
||||
|
||||
fn get_password_hash(&self) -> axum_login::secrecy::SecretVec<u8> {
|
||||
SecretVec::new(self.password_hash.clone().into())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct AxumUserStore {
|
||||
conn: DatabaseConnection,
|
||||
}
|
||||
|
||||
impl AxumUserStore {
|
||||
pub fn new(conn: &DatabaseConnection) -> Self {
|
||||
Self { conn: conn.clone() }
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<Role> UserStore<Role> for AxumUserStore
|
||||
where
|
||||
Role: PartialOrd + PartialEq + Clone + Send + Sync + 'static,
|
||||
{
|
||||
type User = AxumUser;
|
||||
|
||||
async fn load_user(&self, user_id: &str) -> Result<Option<Self::User>> {
|
||||
// my user id is a Vec<u8>, so it's stored base64 encoded
|
||||
let id: i32 = user_id.parse().unwrap();
|
||||
let user = Query::find_user_by_id(&self.conn, id).await?;
|
||||
match user {
|
||||
Some(u) => Ok(Some(AxumUser {
|
||||
id: u.id,
|
||||
password_hash: u.password_hash,
|
||||
name: u.user_name.unwrap(),
|
||||
})),
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user