First JWT login implementation

This commit is contained in:
Jef Roosens 2021-08-21 16:45:41 +02:00
parent 7a97b99bd6
commit 9309ec77fb
Signed by: Jef Roosens
GPG key ID: 955C0660072F691F
6 changed files with 59 additions and 22 deletions

View file

@ -1,5 +1,5 @@
use crate::RbDbConn;
use rb::auth::verify_user;
use rb::auth::{verify_user, JWTResponse, generate_jwt_token};
use rocket::serde::json::Json;
use serde::Deserialize;
@ -9,14 +9,18 @@ struct Credentials {
password: String,
}
// TODO add catch for when user immediately requests new JWT token (they could totally spam this)
#[post("/login", data = "<credentials>")]
async fn login(conn: RbDbConn, credentials: Json<Credentials>) {
async fn login(conn: RbDbConn, credentials: Json<Credentials>) -> rb::Result<Json<JWTResponse>> {
let credentials = credentials.into_inner();
// Get the user, if credentials are valid
let user = conn
.run(move |c| verify_user(c, &credentials.username, &credentials.password))
.await;
user
.await?;
Ok(Json(conn.run(move |c| generate_jwt_token(c, &user)).await?))
}
// /refresh