feat(auth): implement user registration and login with JWT authentication
- Added `bcrypt`, `jsonwebtoken`, and `once_cell` dependencies to manage password hashing and JWT handling. - Created `Claims` struct for JWT claims and implemented token generation in the login route. - Implemented user registration and authentication logic in the `auth` module. - Updated error handling to include validation errors. - Created new routes for user registration and login, and integrated them into the main router. - Added tests for the authentication flow, including registration and login scenarios. - Updated user model to include a password hash field. - Refactored user creation logic to include password validation. - Adjusted feed and user routes to utilize JWT for authentication.
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
use api::setup_router;
|
||||
use app::persistence::user::create_user;
|
||||
use axum::Router;
|
||||
use models::params::user::CreateUserParams;
|
||||
use http_body_util::BodyExt;
|
||||
use models::params::{auth::RegisterParams, user::CreateUserParams};
|
||||
use sea_orm::DatabaseConnection;
|
||||
use utils::testing::setup_test_db;
|
||||
use serde_json::{json, Value};
|
||||
use utils::testing::{make_post_request, setup_test_db};
|
||||
|
||||
pub struct TestApp {
|
||||
pub router: Router,
|
||||
@@ -22,8 +24,27 @@ pub async fn setup() -> TestApp {
|
||||
pub async fn create_test_user(db: &DatabaseConnection, username: &str) {
|
||||
let params = CreateUserParams {
|
||||
username: username.to_string(),
|
||||
password: "password".to_string(),
|
||||
};
|
||||
create_user(db, params)
|
||||
.await
|
||||
.expect("Failed to create test user");
|
||||
}
|
||||
|
||||
pub async fn create_user_with_password(db: &DatabaseConnection, username: &str, password: &str) {
|
||||
let params = RegisterParams {
|
||||
username: username.to_string(),
|
||||
password: password.to_string(),
|
||||
};
|
||||
app::persistence::auth::register_user(db, params)
|
||||
.await
|
||||
.expect("Failed to create test user with password");
|
||||
}
|
||||
|
||||
pub async fn login_user(router: Router, username: &str, password: &str) -> String {
|
||||
let login_body = json!({ "username": username, "password": password }).to_string();
|
||||
let response = make_post_request(router, "/auth/login", login_body, None).await;
|
||||
let body = response.into_body().collect().await.unwrap().to_bytes();
|
||||
let v: Value = serde_json::from_slice(&body).unwrap();
|
||||
v["token"].as_str().unwrap().to_string()
|
||||
}
|
||||
|
Reference in New Issue
Block a user