From e7e49f92554d4b23a50a746fbec50e7099f5c1ae Mon Sep 17 00:00:00 2001 From: core Date: Sun, 19 Nov 2023 21:32:12 -0500 Subject: [PATCH] login req --- trifid-api/src/main.rs | 1 + trifid-api/src/routes/v1/auth/magic_link.rs | 55 +++++++++++++++++++++ trifid-api/src/routes/v1/auth/mod.rs | 3 +- 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 trifid-api/src/routes/v1/auth/magic_link.rs diff --git a/trifid-api/src/main.rs b/trifid-api/src/main.rs index 893db62..fb3bf3c 100644 --- a/trifid-api/src/main.rs +++ b/trifid-api/src/main.rs @@ -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()) diff --git a/trifid-api/src/routes/v1/auth/magic_link.rs b/trifid-api/src/routes/v1/auth/magic_link.rs new file mode 100644 index 0000000..a1c213e --- /dev/null +++ b/trifid-api/src/routes/v1/auth/magic_link.rs @@ -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, state: Data) -> JsonAPIResponse { + 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 {} }) +} \ No newline at end of file diff --git a/trifid-api/src/routes/v1/auth/mod.rs b/trifid-api/src/routes/v1/auth/mod.rs index 06675be..aa16fd9 100644 --- a/trifid-api/src/routes/v1/auth/mod.rs +++ b/trifid-api/src/routes/v1/auth/mod.rs @@ -1 +1,2 @@ -pub mod verify_magic_link; \ No newline at end of file +pub mod verify_magic_link; +pub mod magic_link; \ No newline at end of file