user-auth store test2
This commit is contained in:
@@ -14,14 +14,13 @@ use std::str::FromStr;
|
||||
use std::io;
|
||||
use tower::ServiceBuilder;
|
||||
use tower_http::services::ServeDir;
|
||||
use tokio::sync::RwLock;
|
||||
use tower_http::cors::{Any,CorsLayer};
|
||||
use tower_http::trace::TraceLayer;
|
||||
use booksman_search::BookMeili;
|
||||
use booksman_search;
|
||||
use booksman_orm::{
|
||||
sea_orm::{Database, DatabaseConnection},
|
||||
Mutation as MutationCore, Query as QueryCore,
|
||||
Mutation as MutationCore, Query as QueryCore
|
||||
// BookAndMeta,
|
||||
};
|
||||
use meilisearch_sdk::client::Client;
|
||||
@@ -32,29 +31,9 @@ use migration::{Migrator, MigratorTrait};
|
||||
use chrono::Local;
|
||||
use axum_login::{
|
||||
axum_sessions::{async_session::MemoryStore as SessionMemoryStore, SessionLayer},
|
||||
memory_store::MemoryStore as AuthMemoryStore,
|
||||
secrecy::SecretVec,
|
||||
AuthLayer, AuthUser, RequireAuthorizationLayer,
|
||||
};
|
||||
use rand::Rng;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Debug, Default, Clone, Deserialize, Serialize)]
|
||||
struct User {
|
||||
id: i32,
|
||||
password_hash: String,
|
||||
name: String,
|
||||
}
|
||||
|
||||
impl AuthUser for User {
|
||||
fn get_id(&self) -> String {
|
||||
format!("{}", self.id)
|
||||
}
|
||||
|
||||
fn get_password_hash(&self) -> SecretVec<u8> {
|
||||
SecretVec::new(self.password_hash.clone().into())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug, Clone)]
|
||||
struct BookUI {
|
||||
@@ -230,7 +209,7 @@ struct Opt {
|
||||
static_dir: String,
|
||||
}
|
||||
|
||||
type AuthContext = axum_login::extractors::AuthContext<User, AuthMemoryStore<User>>;
|
||||
type AuthContext = axum_login::extractors::AuthContext<booksman_orm::AxumUser, booksman_orm::AxumUserStore>;
|
||||
|
||||
#[tokio::main]
|
||||
pub async fn main() {
|
||||
@@ -253,9 +232,6 @@ pub async fn main() {
|
||||
|
||||
let secret = rand::thread_rng().gen::<[u8; 64]>();
|
||||
|
||||
let session_store = SessionMemoryStore::new();
|
||||
let session_layer = SessionLayer::new(session_store, &secret).with_secure(false);
|
||||
|
||||
let conn = Database::connect(db_url)
|
||||
.await
|
||||
.expect("Database connection failed");
|
||||
@@ -263,21 +239,11 @@ pub async fn main() {
|
||||
Migrator::up(&conn, None).await.unwrap();
|
||||
|
||||
|
||||
let session_store = SessionMemoryStore::new();
|
||||
let session_layer = SessionLayer::new(session_store, &secret).with_secure(false);
|
||||
|
||||
let store = Arc::new(RwLock::new(HashMap::default()));
|
||||
let usersmodels : Vec<user::Model> = QueryCore::list_all_users(&conn).await.unwrap_or(Vec::new());
|
||||
//let users : Vec<User> = get_users_seaorm(conn);
|
||||
//let user = User::get_rusty_user();
|
||||
for usermodel in usersmodels.iter() {
|
||||
let user = User{
|
||||
id: usermodel.id,
|
||||
name: usermodel.user_name.clone().unwrap(),
|
||||
password_hash : usermodel.password_hash.clone(),
|
||||
};
|
||||
store.write().await.insert(user.get_id(), user);
|
||||
}
|
||||
|
||||
let user_store = AuthMemoryStore::new(&store);
|
||||
let user_store = booksman_orm::AxumUserStore::new(&conn);
|
||||
let auth_layer = AuthLayer::new(user_store, &secret);
|
||||
|
||||
let meili_client = Client::new(meili_url, meili_key);
|
||||
@@ -300,7 +266,6 @@ pub async fn main() {
|
||||
// .merge(SpaRouter::new("/assets", opt.static_dir))
|
||||
.layer(auth_layer)
|
||||
.layer(session_layer)
|
||||
.layer(Extension(store))
|
||||
.layer(ServiceBuilder::new().layer(TraceLayer::new_for_http()))
|
||||
.layer(Extension(conn))
|
||||
.layer(Extension(meili_client))
|
||||
@@ -332,9 +297,8 @@ pub async fn main() {
|
||||
|
||||
|
||||
#[axum_macros::debug_handler]
|
||||
async fn register_handler(mut auth: AuthContext, Extension(ref conn): Extension<DatabaseConnection>,
|
||||
Extension(ref store): Extension<RwLock<HashMap<i32, User>>>,
|
||||
Json(user_sent): Json<User>) -> impl IntoResponse {
|
||||
async fn register_handler(Extension(ref conn): Extension<DatabaseConnection>,
|
||||
Json(user_sent): Json<booksman_orm::AxumUser>) -> impl IntoResponse {
|
||||
// add to db
|
||||
let user: user::Model = user::Model{
|
||||
id: user_sent.id,
|
||||
@@ -342,8 +306,8 @@ Json(user_sent): Json<User>) -> impl IntoResponse {
|
||||
password_hash: user_sent.clone().password_hash,
|
||||
};
|
||||
let created_user = MutationCore::create_user(conn, user).await.expect("Failed to create user");
|
||||
store.write().await.insert(created_user.last_insert_id, user_sent.clone());
|
||||
auth.login(&user_sent).await.unwrap();
|
||||
|
||||
//auth.login(&user_sent_updated).await.unwrap();
|
||||
return "success";
|
||||
}
|
||||
|
||||
@@ -364,7 +328,7 @@ async fn list_users(
|
||||
}
|
||||
return Json(users);
|
||||
}
|
||||
async fn login_handler(mut auth: AuthContext, Json(user_sent): Json<User>) -> impl IntoResponse {
|
||||
async fn login_handler(mut auth: AuthContext, Json(user_sent): Json<booksman_orm::AxumUser>) -> impl IntoResponse {
|
||||
auth.login(&user_sent).await.unwrap();
|
||||
return "success";
|
||||
}
|
||||
@@ -382,7 +346,7 @@ async fn list_users(
|
||||
async fn create_by_isbn(
|
||||
Extension(ref conn): Extension<DatabaseConnection>,
|
||||
Extension(ref meili_client): Extension<Client>,
|
||||
Extension(user): Extension<User>,
|
||||
Extension(user): Extension<booksman_orm::AxumUser>,
|
||||
axum::extract::Query(params): axum::extract::Query<HashMap<String, String>>,
|
||||
) -> impl IntoResponse {
|
||||
|
||||
@@ -636,7 +600,7 @@ let mut vec = Vec::with_capacity(12);
|
||||
async fn delete_book(
|
||||
Extension(ref conn): Extension<DatabaseConnection>,
|
||||
Extension(ref meili_client): Extension<Client>,
|
||||
Extension(user): Extension<User>,
|
||||
Extension(user): Extension<booksman_orm::AxumUser>,
|
||||
Path(id): Path<i32>,
|
||||
) -> impl IntoResponse {
|
||||
MutationCore::delete_book(conn, id)
|
||||
@@ -766,7 +730,7 @@ return Json(res);
|
||||
async fn create_book(
|
||||
Extension(ref conn): Extension<DatabaseConnection>,
|
||||
Extension(ref meili_client): Extension<Client>,
|
||||
Extension(user): Extension<User>,
|
||||
Extension(user): Extension<booksman_orm::AxumUser>,
|
||||
Json(doc_sent_orig): Json<BookUI>,
|
||||
) -> impl IntoResponse {
|
||||
println!("Creating book");
|
||||
@@ -912,7 +876,7 @@ async fn create_book(
|
||||
async fn update_book(
|
||||
Extension(ref conn): Extension<DatabaseConnection>,
|
||||
Extension(ref meili_client): Extension<Client>,
|
||||
Extension(user): Extension<User>,
|
||||
Extension(user): Extension<booksman_orm::AxumUser>,
|
||||
Json(doc_sent): Json<BookUI>,
|
||||
) -> impl IntoResponse {
|
||||
println!("Updating book");
|
||||
|
||||
Reference in New Issue
Block a user