-
-
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
15 changed files
with
346 additions
and
26 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,27 +1,36 @@ | ||
<!doctype html> | ||
<html data-bs-theme="dark"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous"> | ||
<%% "templates/head.html" %> | ||
</head> | ||
<body> | ||
<div class="container pt-5"> | ||
<form method="post" action="/signup"> | ||
<% if error %> | ||
<% if error_user_exists %> | ||
<div class="alert alert-danger"> | ||
Account with this email already exists, and the password is incorrect. | ||
</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="email"> | ||
<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
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,4 +1,5 @@ | ||
pub use proc_macro::TokenStream; | ||
pub use quote::quote; | ||
pub use syn::parse::*; | ||
pub use syn::punctuated::Punctuated; | ||
pub use syn::*; |
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,34 @@ | ||
use crate::prelude::*; | ||
|
||
struct RouteInput { | ||
route: Expr, | ||
controller: Expr, | ||
} | ||
|
||
impl Parse for RouteInput { | ||
fn parse(input: ParseStream) -> Result<Self> { | ||
let route = input.parse()?; | ||
let _arrow: Token![=>] = input.parse()?; | ||
|
||
let controller = input.parse()?; | ||
|
||
Ok(Self { route, controller }) | ||
} | ||
} | ||
|
||
pub(crate) fn route_impl(input: TokenStream) -> TokenStream { | ||
let input = parse_macro_input!(input as RouteInput); | ||
|
||
let route = input.route; | ||
let controller = input.controller; | ||
|
||
let controller = match controller { | ||
Expr::Path(expr) => quote! { #expr::default() }, | ||
expr => quote! { #expr }, | ||
}; | ||
|
||
quote! { | ||
#controller.route(#route) | ||
} | ||
.into() | ||
} |
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,52 @@ | ||
use super::prelude::*; | ||
|
||
struct UserModel { | ||
identifier: Ident, | ||
password: Ident, | ||
} | ||
|
||
impl Parse for UserModel { | ||
fn parse(input: parse::ParseStream) -> Result<Self> { | ||
let identifier: Ident = input.parse()?; | ||
let _: Token![,] = input.parse()?; | ||
let password = input.parse()?; | ||
|
||
Ok(UserModel { | ||
identifier, | ||
password, | ||
}) | ||
} | ||
} | ||
|
||
pub(crate) fn impl_derive_user_model(input: TokenStream) -> TokenStream { | ||
let input = parse_macro_input!(input as DeriveInput); | ||
let ident = &input.ident; | ||
|
||
if let Some(attr) = input.attrs.first() { | ||
match attr.meta { | ||
Meta::List(ref attrs) => { | ||
let attrs = syn::parse2::<UserModel>(attrs.tokens.clone()).unwrap(); | ||
|
||
let identifier = attrs.identifier.to_string(); | ||
let password = attrs.password.to_string(); | ||
|
||
return quote! { | ||
impl rwf::model::UserModel for #ident { | ||
fn identifier_column() -> &'static str { | ||
#identifier | ||
} | ||
|
||
fn password_column() -> &'static str { | ||
#password | ||
} | ||
} | ||
} | ||
.into(); | ||
} | ||
|
||
_ => (), | ||
} | ||
} | ||
|
||
quote! {}.into() | ||
} |
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 |
---|---|---|
@@ -0,0 +1,85 @@ | ||
use std::marker::PhantomData; | ||
|
||
use super::{Controller, Error, PageController}; | ||
use crate::view::{Context, Template}; | ||
use crate::{ | ||
http::{Request, Response}, | ||
model::{user::Error as UserError, UserModel}, | ||
}; | ||
use async_trait::async_trait; | ||
|
||
pub struct LoginController<T> { | ||
redirect: Option<String>, | ||
template: &'static str, | ||
_marker: PhantomData<T>, | ||
} | ||
|
||
impl<T: UserModel> LoginController<T> { | ||
pub fn new(template: &'static str) -> Self { | ||
Self { | ||
redirect: None, | ||
template, | ||
_marker: PhantomData, | ||
} | ||
} | ||
|
||
pub fn redirect(mut self, redirect: impl ToString) -> Self { | ||
self.redirect = Some(redirect.to_string()); | ||
self | ||
} | ||
|
||
fn error(&self, request: &Request, error: &str) -> Result<Response, Error> { | ||
let template = Template::load(self.template)?; | ||
let mut ctx = Context::new(); | ||
ctx.set(error, true)?; | ||
ctx.set("request", request.clone())?; | ||
Ok(Response::new().html(template.render(&ctx)?).code(400)) | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl<T: UserModel> PageController for LoginController<T> { | ||
async fn get(&self, request: &Request) -> Result<Response, Error> { | ||
let mut ctx = Context::new(); | ||
ctx.set("request", request.clone())?; | ||
let template = Template::load(self.template)?; | ||
Ok(Response::new().html(template.render(&ctx)?)) | ||
} | ||
|
||
async fn post(&self, request: &Request) -> Result<Response, Error> { | ||
let form = request.form_data()?; | ||
let identifier: String = match form.get_required("identifier") { | ||
Ok(field) => field, | ||
Err(_) => return self.error(request, "error_identifier"), | ||
}; | ||
let identifier = identifier.trim().to_string(); | ||
let password: String = match form.get_required("password") { | ||
Ok(field) => field, | ||
Err(_) => return self.error(request, "error_password"), | ||
}; | ||
|
||
match T::create_user(identifier, password).await { | ||
Ok(user) => { | ||
let id = user.id().integer()?; | ||
let response = request.login(id); | ||
|
||
if let Some(ref redirect) = self.redirect { | ||
Ok(response.redirect(redirect)) | ||
} else { | ||
Ok(response) | ||
} | ||
} | ||
Err(err) => match err { | ||
UserError::UserExists => return self.error(request, "error_user_exists"), | ||
err => return Err(err.into()), | ||
}, | ||
} | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl<T: UserModel> Controller for LoginController<T> { | ||
async fn handle(&self, request: &Request) -> Result<Response, Error> { | ||
PageController::handle(self, request).await | ||
} | ||
} |
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
Oops, something went wrong.