summaryrefslogtreecommitdiff
path: root/server/src/routes/auth.rs
blob: 629ed33cbb547ca38fccdfbabf207dae3bf68256 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
use crate::errors::AppError;
use crate::models::{
    auth::{AuthBody, Claims},
    user::{User, UserCreate},
};
use axum::{routing::post, Json, Router};

pub fn create_route() -> Router {
    Router::new().route("/login", post(make_login))
}

async fn make_login(Json(payload): Json<UserCreate>) -> Result<Json<AuthBody>, AppError> {
    let user = User::new(payload.email, payload.password);
    match User::find(user).await {
        Ok(user) => {
            let claims = Claims::new(user.id);
            let token = claims.get_token()?;
            Ok(Json(AuthBody::new(token)))
        }
        Err(_) => Err(AppError::NotFound),
    }
}