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)
}
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?;
if !u.verify_pw(password) {
return None;
}
Some(u.session().await)
Some((u.session().await, u.role))
}
/// Change the password of a `User`

View file

@ -52,12 +52,13 @@ pub struct LoginData {
#[post("/login", data = "<login>")]
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
.ok_or_else(|| api_error("Login failed"))?;
Ok(json!({
"token": ses.token
"token": ses.token,
"role": role
}))
}