Przeglądaj źródła

Create route for retrieving user password_salt with email.

Lee Morgan 11 miesięcy temu
rodzic
commit
35918689f3

+ 17 - 0
bruno/Suma/User/Password Salt.bru

@@ -0,0 +1,17 @@
+meta {
+  name: Password Salt
+  type: http
+  seq: 5
+}
+
+post {
+  url: http://localhost:8000/api/user/salt
+  body: json
+  auth: inherit
+}
+
+body:json {
+  {
+    "email": "lee@leemorgan.dev"
+  }
+}

+ 11 - 1
src/controllers/user.rs

@@ -5,10 +5,20 @@ use serde_json::json;
 
 use crate::models::user::User;
 use crate::models::account::{Account};
-use crate::dto::user::{CreateInput, LoginInput};
+use crate::dto::user::{CreateInput, LoginInput, GetPasswordSaltInput};
 use crate::app_error::AppError;
 use crate::auth::user_auth;
 
+#[post("/api/user/salt")]
+pub async fn get_password_salt_route(
+    db: web::Data<Database>,
+    body: web::Json<GetPasswordSaltInput>
+) -> Result<HttpResponse, AppError> {
+    let user_collection = db.collection::<User>("users");
+    let user = User::find_by_email(&user_collection, &body.email.to_lowercase()).await?;
+    Ok(HttpResponse::Ok().json(json!({"password_salt": user.password_salt})))
+}
+
 #[post("/api/user")]
 pub async fn create_route(
     db: web::Data<Database>,

+ 5 - 0
src/dto/user.rs

@@ -15,3 +15,8 @@ pub struct CreateInput {
     pub password_salt: String,
     pub encryption_salt: String
 }
+
+#[derive(Deserialize)]
+pub struct GetPasswordSaltInput{
+    pub email: String
+}

+ 2 - 0
src/routes/user.rs

@@ -1,5 +1,6 @@
 use actix_web::web;
 use crate::controllers::user::{
+    get_password_salt_route,
     create_route,
     login_route,
     logout_route,
@@ -7,6 +8,7 @@ use crate::controllers::user::{
 };
 
 pub fn config(cfg: &mut web::ServiceConfig) {
+    cfg.service(get_password_salt_route);
     cfg.service(create_route);
     cfg.service(login_route);
     cfg.service(logout_route);

+ 1 - 1
src/views/js/data/User.js

@@ -15,7 +15,7 @@ export default class User{
         return {
             name: name,
             email: email,
-            password_hash: await EncryptionHandler.hashPassword(password, salt),
+            password_hash: await EncryptionHandler.hashPassword(password, passwordSalt),
             password_salt: passwordSalt,
             encryption_salt: EncryptionHandler.generateSalt()
         };

+ 1 - 4
src/views/js/pages/Register.js

@@ -17,15 +17,12 @@ export default class Register extends Page{
         const password = this.container.querySelector(".password").value;
         const confirmPassword = this.container.querySelector(".confirmPassword").value;
 
-        const user = User.create(name, email, password);
-        console.log(user);
-
         fetch("/api/user", {
             method: "POST",
             headers: {
                 "Content-Type": "application/json"
             },
-            body: JSON.stringify(user)
+            body: JSON.stringify(User.create(name, email, password))
         })
             .then(r=>r.json())
             .then((response)=>{