feat: candidate logout endpoint

This commit is contained in:
Sebastian Pravda 2022-11-24 20:09:14 +01:00
parent 8f282febc8
commit 35f591fb67
No known key found for this signature in database
GPG key ID: F3BC84F08EFA3F57
3 changed files with 19 additions and 0 deletions

View file

@ -40,6 +40,7 @@ pub fn rocket() -> Rocket<Build>{
"/candidate/",
routes![
routes::candidate::login,
routes::candidate::logout,
routes::candidate::whoami,
routes::candidate::get_details,
],

View file

@ -51,6 +51,19 @@ pub async fn login(
return Ok(response);
}
#[post("/logout")]
pub async fn logout(conn: Connection<'_, Db>, session: CandidateAuth, cookies: &CookieJar<'_>,) -> Result<(), Custom<String>> {
let db = conn.into_inner();
let candidate: entity::candidate::Model = session.into();
cookies.remove_private(Cookie::named("id"));
cookies.remove_private(Cookie::named("key"));
CandidateService::logout(db, candidate.application)
.await
.map_err(|e| Custom(Status::from_code(e.code()).unwrap_or(Status::InternalServerError), e.to_string()))
}
#[get("/whoami")]
pub async fn whoami(session: CandidateAuth) -> Result<String, Custom<String>> {
let candidate: entity::candidate::Model = session.into();

View file

@ -131,6 +131,11 @@ impl CandidateService {
Ok(new_password_plain)
}
pub async fn logout(db: &DbConn, id: i32) -> Result<(), ServiceError> {
SessionService::revoke_all_sessions(db, Some(id), None).await?;
Ok(())
}
pub(in crate::services) async fn add_candidate_details(
db: &DbConn,
candidate: candidate::Model,