forked from Roflin/gamenight
Added a client (From generator) and a basic login page to test it.
This commit is contained in:
@@ -20,15 +20,6 @@ impl LoginResponse {
|
||||
jwt_token: Some(token)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn failure(message: String) -> Self {
|
||||
Self {
|
||||
login_result: false,
|
||||
message: Some(message),
|
||||
user_id: None,
|
||||
jwt_token: None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
|
||||
@@ -34,23 +34,22 @@ impl Into<schema::user::Register> for Register {
|
||||
pub async fn login(pool: web::Data<DbPool>, login_data: web::Json<Login>) -> Result<impl Responder, ApiError> {
|
||||
let data = login_data.into_inner();
|
||||
|
||||
let response = if let Some(user) = web::block(move || {
|
||||
if let Ok(Some(user)) = web::block(move || {
|
||||
let mut conn = pool.get_conn();
|
||||
schema::login(&mut conn, data.into())
|
||||
})
|
||||
.await??
|
||||
.await?
|
||||
{
|
||||
let token = get_token(&user)?;
|
||||
LoginResponse::success(user.id, token)
|
||||
let response = LoginResponse::success(user.id, token);
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type(ContentType::json())
|
||||
.body(serde_json::to_string(&response)?)
|
||||
)
|
||||
}
|
||||
else {
|
||||
LoginResponse::failure("User doesn't exist or password doesn't match".to_string())
|
||||
};
|
||||
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type(ContentType::json())
|
||||
.body(serde_json::to_string(&response)?)
|
||||
)
|
||||
Err(ApiError{status: 401, message: "User doesn't exist or password doesn't match".to_string()})
|
||||
}
|
||||
}
|
||||
|
||||
#[post("/user")]
|
||||
|
||||
Reference in New Issue
Block a user