Skip to content

Commit

Permalink
compiling now
Browse files Browse the repository at this point in the history
  • Loading branch information
amitu committed Feb 14, 2024
1 parent 3c8f7ea commit 0b8d321
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 31 deletions.
36 changes: 23 additions & 13 deletions fastn-core/src/auth/email_password/confirm_email.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
use crate::auth::email_password::{email_confirmation_sent_ftd, key_expired};

pub(crate) async fn confirm_email(
req: &fastn_core::http::Request,
ds: &fastn_ds::DocumentStore,
req_config: &mut fastn_core::RequestConfig,
db_pool: &fastn_core::db::PgPool,
next: String,
) -> fastn_core::Result<fastn_core::http::Response> {
use diesel::prelude::*;
use diesel_async::RunQueryDsl;

if req.method() != "POST" {
if req_config.request.method() != "POST" {
let main = fastn_core::Document {
package_name: config.package.name.clone(),
package_name: req_config.config.package.name.clone(),
id: "/-/confirm-email/".to_string(),
content: email_confirmation_sent_ftd().to_string(),
parent_path: fastn_ds::Path::new("/"),
Expand All @@ -23,7 +22,7 @@ pub(crate) async fn confirm_email(
return Ok(resp.into());
}

let code = req.query().get("code");
let code = req_config.request.query().get("code");

if code.is_none() {
tracing::info!("finishing response due to bad ?code");
Expand Down Expand Up @@ -65,14 +64,14 @@ pub(crate) async fn confirm_email(

let (email_id, session_id, sent_at) = conf_data.unwrap();

if key_expired(ds, sent_at).await {
if key_expired(&req_config.config.ds, sent_at).await {
// TODO: this redirect route should be configurable
tracing::info!("provided code has expired.");
return Ok(fastn_core::http::redirect_with_code(
format!(
"{}://{}/-/auth/resend-confirmation-email/",
req.connection_info.scheme(),
req.connection_info.host(),
req_config.request.connection_info.scheme(),
req_config.request.connection_info.host(),
),
302,
));
Expand All @@ -93,7 +92,12 @@ pub(crate) async fn confirm_email(
tracing::info!("session created, rows affected: {}", affected);

// Onboarding step is opt-in
let onboarding_enabled = ds.env("FASTN_AUTH_ADD_ONBOARDING_STEP").await.is_ok();
let onboarding_enabled = req_config
.config
.ds
.env("FASTN_AUTH_ADD_ONBOARDING_STEP")
.await
.is_ok();

let next_path = if onboarding_enabled {
format!("/-/auth/onboarding/?next={}", next)
Expand All @@ -102,17 +106,23 @@ pub(crate) async fn confirm_email(
};

// redirect to onboarding route with a GET request
let mut resp =
fastn_core::auth::set_session_cookie_and_redirect_to_next(req, ds, session_id, next_path)
.await?;
let mut resp = fastn_core::auth::set_session_cookie_and_redirect_to_next(
&req_config.request,
&req_config.config.ds,
session_id,
next_path,
)
.await?;

if onboarding_enabled {
resp.add_cookie(
&actix_web::cookie::Cookie::build(
fastn_core::auth::FIRST_TIME_SESSION_COOKIE_NAME,
"1",
)
.domain(fastn_core::auth::utils::domain(req.connection_info.host()))
.domain(fastn_core::auth::utils::domain(
req_config.request.connection_info.host(),
))
.path("/")
.finish(),
)
Expand Down
17 changes: 7 additions & 10 deletions fastn-core/src/auth/email_password/create_account.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::auth::email_password::{
create_account_ftd, email_confirmation_sent_ftd, redirect_url_from_next,
create_account_ftd, create_and_send_confirmation_email, redirect_url_from_next,
};

#[derive(serde::Deserialize, serde::Serialize, validator::Validate, Debug)]
Expand All @@ -18,19 +18,17 @@ struct UserPayload {
}

pub(crate) async fn create_account(
req: &fastn_core::http::Request,
req_config: &mut fastn_core::RequestConfig,
config: &fastn_core::Config,
db_pool: &fastn_core::db::PgPool,
next: String,
) -> fastn_core::Result<fastn_core::http::Response> {
use diesel::prelude::*;
use diesel_async::RunQueryDsl;
use validator::ValidateArgs;

if req.method() != "POST" {
if req_config.request.method() != "POST" {
let main = fastn_core::Document {
package_name: config.package.name.clone(),
package_name: req_config.config.package.name.clone(),
id: "/-/create-account/".to_string(),
content: create_account_ftd().to_string(),
parent_path: fastn_ds::Path::new("/"),
Expand All @@ -42,7 +40,7 @@ pub(crate) async fn create_account(
return Ok(resp.into());
}

let user_payload = match req.json::<UserPayload>() {
let user_payload = match req_config.request.json::<UserPayload>() {
Ok(p) => p,
Err(e) => return fastn_core::http::user_err(
vec![("payload".into(), vec![format!("Invalid payload. Required the request body to contain json. Original error: {:?}", e)])],
Expand Down Expand Up @@ -156,18 +154,17 @@ pub(crate) async fn create_account(
tracing::info!("fastn_user email inserted");

let conf_link =
create_and_send_confirmation_email(email.0.to_string(), db_pool, req, req_config, next)
.await?;
create_and_send_confirmation_email(email.0.to_string(), db_pool, req_config, next).await?;

let resp_body = serde_json::json!({
"user": user,
"success": true,
"redirect": redirect_url_from_next(req, "/-/auth/confirm-email/".to_string()),
"redirect": redirect_url_from_next(&req_config.request, "/-/auth/confirm-email/".to_string()),
});

let mut resp = actix_web::HttpResponse::Ok();

if config.test_command_running {
if req_config.config.test_command_running {
resp.insert_header(("X-Fastn-Test", "true"))
.insert_header(("X-Fastn-Test-Email-Confirmation-Link", conf_link));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::auth::email_password::{confirmation_link, confirmation_mail_body, gen
pub(crate) async fn create_and_send_confirmation_email(
email: String,
db_pool: &fastn_core::db::PgPool,
req: &fastn_core::http::Request,
req_config: &mut fastn_core::RequestConfig,
next: String,
) -> fastn_core::Result<String> {
Expand Down Expand Up @@ -54,7 +53,7 @@ pub(crate) async fn create_and_send_confirmation_email(
.get_result(&mut conn)
.await?;

let confirmation_link = confirmation_link(req, stored_key, next);
let confirmation_link = confirmation_link(&req_config.request, stored_key, next);

let mailer = fastn_core::mail::Mailer::from_env(&req_config.config.ds).await;

Expand Down Expand Up @@ -123,7 +122,11 @@ pub(crate) async fn create_and_send_confirmation_email(

mailer
.send_raw(
req_config.config.ds.env_bool("FASTN_ENABLE_EMAIL", true),
req_config
.config
.ds
.env_bool("FASTN_ENABLE_EMAIL", true)
.await,
format!("{} <{}>", name, email)
.parse::<lettre::message::Mailbox>()
.unwrap(),
Expand Down
2 changes: 1 addition & 1 deletion fastn-core/src/auth/email_password/resend_email.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub(crate) async fn resend_email(
}
};

create_and_send_confirmation_email(email, db_pool, req, req_config, next.clone()).await?;
create_and_send_confirmation_email(email, db_pool, req_config, next.clone()).await?;

// TODO: there's no GET /-/auth/login/ yet
// the client will have to create one for now
Expand Down
6 changes: 2 additions & 4 deletions fastn-core/src/auth/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,10 @@ pub async fn handle_auth(
"/-/auth/logout/" => logout(&req, &req_config.config.ds, pool, next).await,

"/-/auth/create-account/" => {
fastn_core::auth::email_password::create_account(&req, req_config, config, pool, next)
.await
fastn_core::auth::email_password::create_account(req_config, pool, next).await
}
"/-/auth/confirm-email/" => {
fastn_core::auth::email_password::confirm_email(&req, &req_config.config.ds, pool, next)
.await
fastn_core::auth::email_password::confirm_email(req_config, pool, next).await
}
"/-/auth/resend-confirmation-email/" => {
fastn_core::auth::email_password::resend_email(&req, req_config, pool, next).await
Expand Down

0 comments on commit 0b8d321

Please sign in to comment.