mirror of
https://github.com/n08i40k/schedule-parser-rusted.git
synced 2025-12-06 17:57:47 +03:00
Полностью рабочая авторизация
This commit is contained in:
@@ -1,26 +1,34 @@
|
||||
use crate::database::driver;
|
||||
use crate::database::models::User;
|
||||
use crate::routes::auth::schema::SignInErrCode::IncorrectCredentials;
|
||||
use crate::routes::auth::schema::{SignInDto, SignInResult};
|
||||
use crate::AppState;
|
||||
use crate::{AppState, utility};
|
||||
use actix_web::{post, web};
|
||||
use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl, SelectableHelper};
|
||||
use diesel::SaveChangesDsl;
|
||||
use std::ops::DerefMut;
|
||||
use web::Json;
|
||||
|
||||
#[post("/sign-in")]
|
||||
pub async fn sign_in(data: Json<SignInDto>, app_state: web::Data<AppState>) -> Json<SignInResult> {
|
||||
use crate::database::schema::users::dsl::*;
|
||||
let result = match driver::users::get_by_username(&app_state.database, data.username.clone()) {
|
||||
Ok(mut user) => match bcrypt::verify(&data.password, &user.password) {
|
||||
Ok(true) => {
|
||||
let mut lock = app_state.connection();
|
||||
let conn = lock.deref_mut();
|
||||
|
||||
match {
|
||||
let mut lock = app_state.database.lock().unwrap();
|
||||
let connection = lock.deref_mut();
|
||||
user.access_token =
|
||||
utility::jwt::encode(&user.id).expect("Failed to generate jet token");
|
||||
|
||||
users
|
||||
.filter(username.eq(data.username.clone()))
|
||||
.select(User::as_select())
|
||||
.first(connection)
|
||||
} {
|
||||
Ok(user) => Json(SignInResult::ok(&user)),
|
||||
Err(_) => Json(SignInResult::err(IncorrectCredentials)),
|
||||
}
|
||||
user.save_changes::<User>(conn)
|
||||
.expect("Failed to update user");
|
||||
|
||||
SignInResult::ok(&user)
|
||||
}
|
||||
Ok(false) | Err(_) => SignInResult::err(IncorrectCredentials),
|
||||
},
|
||||
|
||||
Err(_) => SignInResult::err(IncorrectCredentials),
|
||||
};
|
||||
|
||||
Json(result)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user