-
-
Notifications
You must be signed in to change notification settings - Fork 18
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
Showing
11 changed files
with
194 additions
and
159 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,21 +1,4 @@ | ||
// use rwf::model::Error; | ||
use rwf::crypto::{hash, hash_validate}; | ||
use rwf::prelude::*; | ||
use tokio::task::spawn_blocking; | ||
|
||
#[derive(Clone, macros::Model, macros::UserModel)] | ||
#[user_model(email, password_hash)] | ||
pub struct User2 { | ||
id: Option<i64>, | ||
email: String, | ||
password_hash: String, | ||
} | ||
|
||
pub enum UserLogin { | ||
NoSuchUser, | ||
WrongPassword, | ||
Ok(User), | ||
} | ||
|
||
#[derive(Clone, macros::Model, macros::UserModel)] | ||
#[user_model(email, password)] | ||
|
@@ -25,59 +8,3 @@ pub struct User { | |
password: String, | ||
created_at: OffsetDateTime, | ||
} | ||
|
||
impl User { | ||
/// Create new user with email and password. | ||
pub async fn signup(email: &str, password: &str) -> Result<UserLogin, Error> { | ||
let hash_password = password.to_owned(); | ||
let encrypted_password = spawn_blocking(move || hash(hash_password.as_bytes())) | ||
.await | ||
.unwrap()?; | ||
|
||
match Self::login(email, password).await? { | ||
UserLogin::Ok(user) => return Ok(UserLogin::Ok(user)), | ||
UserLogin::WrongPassword => return Ok(UserLogin::WrongPassword), | ||
_ => (), | ||
} | ||
|
||
let user = User::create(&[ | ||
("email", email.to_value()), | ||
("password", encrypted_password.to_value()), | ||
]) | ||
.fetch(Pool::pool()) | ||
.await?; | ||
|
||
Ok(UserLogin::Ok(user)) | ||
} | ||
|
||
/// Login user with email and password. | ||
/// | ||
/// Return a user if one exists and the passwords match. | ||
/// Return `None` otherwise. | ||
pub async fn login(email: &str, password: &str) -> Result<UserLogin, Error> { | ||
if let Some(user) = User::filter("email", email) | ||
.fetch_optional(Pool::pool()) | ||
.await? | ||
{ | ||
if hash_validate(password.as_bytes(), &user.password)? { | ||
return Ok(UserLogin::Ok(user)); | ||
} else { | ||
return Ok(UserLogin::WrongPassword); | ||
} | ||
} | ||
|
||
Ok(UserLogin::NoSuchUser) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[tokio::test] | ||
async fn test_user() { | ||
Migrations::migrate().await.unwrap(); | ||
let _user = User::signup("[email protected]", "password2").await.unwrap(); | ||
let _user = User::login("[email protected]", "password2").await.unwrap(); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<!doctype html> | ||
<html data-bs-theme="dark"> | ||
<head> | ||
<%% "templates/head.html" %> | ||
</head> | ||
<body> | ||
<div class="container pt-5"> | ||
<h1 class="mb-4">Login</h1> | ||
<form method="post" action="/login"> | ||
<%= csrf_token() %> | ||
|
||
<% if error_user_does_not_exist %> | ||
<div class="alert alert-danger"> | ||
Account with this email doesn't exist or the password is incorrect. | ||
</div> | ||
<% end %> | ||
|
||
<% if error_password %> | ||
<div class="alert alert-danger"> | ||
Wrong password. | ||
</div> | ||
<% end %> | ||
|
||
<div class="mb-3"> | ||
<label class="form-label">Email</label> | ||
<input class="form-control" type="email" placeholder="Your email, e.g. [email protected]" required autocomplete="off" name="identifier"> | ||
</div> | ||
|
||
<div class="mb-3"> | ||
<label class="form-label">Password</label> | ||
<input class="form-control" type="password" placeholder="A secure password" required autocomplete="off" name="password"> | ||
</div> | ||
|
||
<div class="d-flex justify-content-end"> | ||
<button type="submit" class="btn btn-primary"> | ||
Signup | ||
</button> | ||
</div> | ||
</form> | ||
</div> | ||
</body> | ||
</html> |
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 |
---|---|---|
|
@@ -5,32 +5,22 @@ | |
</head> | ||
<body> | ||
<div class="container pt-5"> | ||
<h1 class="mb-4">Create account</h1> | ||
<form method="post" action="/signup"> | ||
<% if error_user_exists %> | ||
<div class="alert alert-danger"> | ||
Account with this email already exists, and the password is incorrect. | ||
Account with this email already exists. | ||
</div> | ||
<% end %> | ||
<%= csrf_token() %> | ||
<div class="mb-3"> | ||
<label class="form-label">Email</label> | ||
<input class="form-control" type="email" placeholder="Your email, e.g. [email protected]" required autocomplete="off" name="identifier"> | ||
<% if error_identifier %> | ||
<div class="invalid-feedback"> | ||
Provided email is not valid. | ||
</div> | ||
<% end %> | ||
</div> | ||
|
||
<div class="mb-3"> | ||
<label class="form-label">Password</label> | ||
<input class="form-control" type="password" placeholder="A secure password" required autocomplete="off" name="password"> | ||
|
||
<% if error_password %> | ||
<div class="invalid-feedback"> | ||
Provided password is not valid. | ||
</div> | ||
<% end %> | ||
</div> | ||
|
||
<div class="d-flex justify-content-end"> | ||
|
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
Oops, something went wrong.