login req
/ build (push) Successful in 47s Details
/ build_x64 (push) Successful in 2m6s Details
/ build_arm64 (push) Successful in 2m33s Details
/ build_win64 (push) Successful in 2m31s Details

This commit is contained in:
core 2023-11-19 21:32:12 -05:00
parent 51b6d3a85a
commit e7e49f9255
Signed by: core
GPG Key ID: FDBF740DADDCEECF
3 changed files with 58 additions and 1 deletions

View File

@ -124,6 +124,7 @@ async fn main() {
}))
.service(routes::v1::signup::signup_req)
.service(routes::v1::auth::verify_magic_link::verify_link_req)
.service(routes::v1::auth::magic_link::login_req)
.wrap(Logger::default())
.wrap(actix_cors::Cors::permissive())
.app_data(app_state.clone())

View File

@ -0,0 +1,55 @@
use std::time::{Duration, SystemTime};
use actix_web::post;
use actix_web::http::StatusCode;
use actix_web::web::{Data, Json};
use serde::{Deserialize, Serialize};
use crate::{AppState, randid};
use crate::models::{MagicLink, User};
use crate::response::JsonAPIResponse;
use diesel::QueryDsl;
use diesel_async::RunQueryDsl;
use crate::email::send_email;
use diesel::SelectableHelper;
use crate::schema::users;
use crate::schema::magic_links;
use diesel::ExpressionMethods;
#[derive(Deserialize)]
pub struct LoginRequest {
pub email: String
}
#[derive(Serialize, Debug)]
pub struct LoginResponse {
pub data: Option<()>,
pub metadata: LoginResponseMetadata
}
#[derive(Serialize, Debug)]
pub struct LoginResponseMetadata {}
#[post("/v1/auth/magic-link")]
pub async fn login_req(req: Json<LoginRequest>, state: Data<AppState>) -> JsonAPIResponse<LoginResponse> {
let mut conn = handle_error!(state.pool.get().await);
let user_vec = handle_error!(users::dsl::users.filter(users::dsl::email.eq(&req.email)).select(User::as_select()).load(&mut conn).await);
// Difference from DN functionality: Trifid API will not implicitly create accounts
let user = match user_vec.get(0) {
Some(user) => user,
None => {
err!(StatusCode::BAD_REQUEST, make_err!("ERR_INVALID_EMAIL", "does not exist", "email"))
}
};
let new_magic_link = MagicLink {
id: randid!(token "ml"),
user_id: user.id.clone(),
expires: SystemTime::now() + Duration::from_secs(state.config.tokens.magic_link_expiry_seconds)
};
handle_error!(diesel::insert_into(magic_links::table).values(&new_magic_link).execute(&mut conn).await);
handle_error!(send_email(&new_magic_link.id, &req.email, &state.config).await);
ok!(LoginResponse { data: None, metadata: LoginResponseMetadata {} })
}

View File

@ -1 +1,2 @@
pub mod verify_magic_link;
pub mod magic_link;