-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ca75fc1
commit 00c25a9
Showing
4 changed files
with
65 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,66 @@ | ||
use actix_web::{web, HttpResponse}; | ||
use reqwest::header::LOCATION; | ||
use actix_web::{web, HttpResponse, ResponseError}; | ||
use reqwest::{header::LOCATION, StatusCode}; | ||
use secrecy::Secret; | ||
use sqlx::PgPool; | ||
|
||
use crate::routes::home_route; | ||
use crate::{ | ||
authentication::{validate_credentials, AuthError, Credentials}, | ||
routes::{error_chain_fmt, home_route}, | ||
}; | ||
|
||
#[derive(serde::Deserialize)] | ||
pub struct FormData { | ||
_email: String, | ||
_password: Secret<String>, | ||
email: String, | ||
password: Secret<String>, | ||
} | ||
|
||
pub async fn login(_form: web::Form<FormData>) -> HttpResponse { | ||
HttpResponse::SeeOther() | ||
#[derive(thiserror::Error)] | ||
pub enum LoginError { | ||
#[error("Authentication failed")] | ||
AuthError(#[source] anyhow::Error), | ||
#[error("Something went wrong")] | ||
UnexpectedError(#[from] anyhow::Error), | ||
} | ||
|
||
impl std::fmt::Debug for LoginError { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
error_chain_fmt(self, f) | ||
} | ||
} | ||
|
||
impl ResponseError for LoginError { | ||
fn status_code(&self) -> reqwest::StatusCode { | ||
match self { | ||
LoginError::AuthError(_) => StatusCode::UNAUTHORIZED, | ||
LoginError::UnexpectedError(_) => StatusCode::INTERNAL_SERVER_ERROR, | ||
} | ||
} | ||
} | ||
|
||
#[tracing::instrument( | ||
skip(form, pool), | ||
fields(email=tracing::field::Empty, user_id=tracing::field::Empty) | ||
)] | ||
pub async fn login( | ||
form: web::Form<FormData>, | ||
pool: web::Data<PgPool>, | ||
) -> Result<HttpResponse, LoginError> { | ||
println!("login called"); | ||
let credentials = Credentials { | ||
email: form.0.email, | ||
password: form.0.password, | ||
}; | ||
tracing::Span::current().record("email", &tracing::field::display(&credentials.email)); | ||
|
||
let user_id = validate_credentials(credentials, &pool) | ||
.await | ||
.map_err(|e| match e { | ||
AuthError::InvalidCredentials(_) => LoginError::AuthError(e.into()), | ||
AuthError::UnexpectedError(_) => LoginError::UnexpectedError(e.into()), | ||
})?; | ||
tracing::Span::current().record("user_id", &tracing::field::display(user_id)); | ||
|
||
Ok(HttpResponse::SeeOther() | ||
.insert_header((LOCATION, home_route())) | ||
.finish() | ||
.finish()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters