user role

This commit is contained in:
JMARyA 2024-08-11 03:38:27 +02:00
parent e3a0d4075f
commit 74b32003d0
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
2 changed files with 5 additions and 4 deletions

View file

@ -60,14 +60,14 @@ impl User {
Some(u) Some(u)
} }
pub async fn login(username: &str, password: &str) -> Option<Session> { pub async fn login(username: &str, password: &str) -> Option<(Session, UserRole)> {
let u = User::find_one(doc! { "username": username }).await?; let u = User::find_one(doc! { "username": username }).await?;
if !u.verify_pw(password) { if !u.verify_pw(password) {
return None; return None;
} }
Some(u.session().await) Some((u.session().await, u.role))
} }
/// Change the password of a `User` /// Change the password of a `User`

View file

@ -52,12 +52,13 @@ pub struct LoginData {
#[post("/login", data = "<login>")] #[post("/login", data = "<login>")]
pub async fn login_route(login: Json<LoginData>) -> FallibleApiResponse { pub async fn login_route(login: Json<LoginData>) -> FallibleApiResponse {
let ses = User::login(&login.username, &login.password) let (ses, role) = User::login(&login.username, &login.password)
.await .await
.ok_or_else(|| api_error("Login failed"))?; .ok_or_else(|| api_error("Login failed"))?;
Ok(json!({ Ok(json!({
"token": ses.token "token": ses.token,
"role": role
})) }))
} }