First JWT login implementation
This commit is contained in:
parent
7a97b99bd6
commit
9309ec77fb
6 changed files with 59 additions and 22 deletions
|
|
@ -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
|
||||
|
|
|
|||
Reference in a new issue