feat: signin api (OS-2)

This commit is contained in:
Artemy 2022-07-26 20:30:45 +03:00
parent 8baf0576e8
commit b2379f2a43
4 changed files with 76 additions and 0 deletions

View file

@ -37,6 +37,64 @@ impl Core {
self.get_collection(&self.apps).await self.get_collection(&self.apps).await
} }
pub async fn signin(&self, name: &String, password: &String) -> serde_json::Value {
let response = self.users.find_one(doc! {"name":name}, None).await;
match response {
Ok(user) => {
match user {
Some(user) => {
let pass_hash = self.hash(name.clone() + &password);
if user.get_str("password").unwrap() == pass_hash {
let jwt_info = JwtInfo {
name: name.clone(),
role: user.get_str("role").unwrap().to_string(),
exp: Utc::now().timestamp() + 604800, //week
};
let token = encode(
&Header::default(),
&jwt_info,
&EncodingKey::from_secret(self.jwt_secret.as_ref()),
);
match token {
Ok(token) => {
json! ({
"code":"ok",
"token":token
})
}
Err(_) => {
json! ({
"code":"err",
"msg":"Some problem with jwt generation"
})
}
}
} else {
json! ({
"code":"denied",
"msg":"Wrong password"
})
}
}
None => {
json! ({
"code":"err",
"msg":"User does not exist"
})
}
}
}
Err(_) => {
json! ({
"code":"err",
"msg":"User does not exist"
})
}
}
}
pub async fn signup(&self, user: &Json<User>) -> serde_json::Value { pub async fn signup(&self, user: &Json<User>) -> serde_json::Value {
let jwt_info = JwtInfo { let jwt_info = JwtInfo {
name: user.name.clone(), name: user.name.clone(),

View file

@ -45,6 +45,7 @@ async fn main() -> std::io::Result<()> {
.wrap(cors) .wrap(cors)
.service(routes::apps) .service(routes::apps)
.service(routes::signup) .service(routes::signup)
.service(routes::signin),
}) })
.bind(("0.0.0.0", port)) .bind(("0.0.0.0", port))
.expect("Can not bind to port") .expect("Can not bind to port")

View file

@ -5,6 +5,15 @@ use actix_web_grants::proc_macro::{has_any_permission, has_permissions};
pub async fn signup(app_data: web::Data<crate::AppState>, user: web::Json<User>) -> impl Responder { pub async fn signup(app_data: web::Data<crate::AppState>, user: web::Json<User>) -> impl Responder {
response(app_data.core.signup(&user).await) response(app_data.core.signup(&user).await)
} }
#[post("/signin")]
pub async fn signin(
app_data: web::Data<crate::AppState>,
user: web::Json<UserAuth>,
) -> impl Responder {
response(app_data.core.signin(&user.name, &user.password).await)
}
#[get("/apps")] #[get("/apps")]
pub async fn apps(app_data: web::Data<crate::AppState>) -> impl Responder { pub async fn apps(app_data: web::Data<crate::AppState>) -> impl Responder {
HttpResponse::Ok().json(app_data.core.get_apps().await) HttpResponse::Ok().json(app_data.core.get_apps().await)

View file

@ -15,6 +15,14 @@ pub struct User {
pub password: String, pub password: String,
pub email: String, pub email: String,
} }
#[derive(Debug, Serialize, Deserialize)]
pub struct UserAuth {
pub name: String,
pub password: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct JwtInfo { pub struct JwtInfo {
pub name: String, pub name: String,
pub role: String, pub role: String,