login req
This commit is contained in:
parent
51b6d3a85a
commit
e7e49f9255
|
@ -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())
|
||||
|
|
|
@ -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 {} })
|
||||
}
|
|
@ -1 +1,2 @@
|
|||
pub mod verify_magic_link;
|
||||
pub mod verify_magic_link;
|
||||
pub mod magic_link;
|
Loading…
Reference in New Issue