mirror of
https://github.com/OSMA-D/osma-server.git
synced 2024-12-24 19:53:46 +03:00
feat: signin api (OS-2)
This commit is contained in:
parent
8baf0576e8
commit
b2379f2a43
4 changed files with 76 additions and 0 deletions
58
src/core.rs
58
src/core.rs
|
@ -37,6 +37,64 @@ impl Core {
|
|||
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 {
|
||||
let jwt_info = JwtInfo {
|
||||
name: user.name.clone(),
|
||||
|
|
|
@ -45,6 +45,7 @@ async fn main() -> std::io::Result<()> {
|
|||
.wrap(cors)
|
||||
.service(routes::apps)
|
||||
.service(routes::signup)
|
||||
.service(routes::signin),
|
||||
})
|
||||
.bind(("0.0.0.0", port))
|
||||
.expect("Can not bind to port")
|
||||
|
|
|
@ -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 {
|
||||
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")]
|
||||
pub async fn apps(app_data: web::Data<crate::AppState>) -> impl Responder {
|
||||
HttpResponse::Ok().json(app_data.core.get_apps().await)
|
||||
|
|
|
@ -15,6 +15,14 @@ pub struct User {
|
|||
pub password: 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 name: String,
|
||||
pub role: String,
|
||||
|
|
Loading…
Add table
Reference in a new issue