peer-group-grading/crates/backend/src/controller/auth.rs
2025-04-04 11:32:19 +02:00

36 lines
750 B
Rust

use actix_session::Session;
use actix_web::{
post,
web::{self, ServiceConfig},
HttpResponse, Responder,
};
use serde::Deserialize;
use crate::{error::ApiError, Database};
#[derive(Deserialize)]
struct LoginRequest {
username: String,
password: String,
}
pub fn setup(cfg: &mut ServiceConfig) {
cfg.service(login);
}
#[post("/login")]
async fn login(
db: web::Data<Database>,
login_request: web::Json<LoginRequest>,
session: Session,
) -> Result<impl Responder, ApiError> {
let login_request = login_request.into_inner();
let user_id = db
.verify_local_user(&login_request.username, &login_request.password)
.await?;
session.insert("user", user_id)?;
Ok(HttpResponse::Ok())
}