Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
levkk committed Dec 7, 2024
1 parent f686dcc commit ee4eb92
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 5 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ members = [
"examples/request-tracking",
"examples/engine",
"rwf-admin",
"examples/files", "examples/users",
"examples/files", "examples/users", "rwf-auth",
]
exclude = ["examples/rails", "rwf-ruby", "examples/django", "rwf-fuzz"]
7 changes: 7 additions & 0 deletions rwf-auth/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "rwf-auth"
version = "0.1.0"
edition = "2021"

[dependencies]
rwf = { path = "../rwf", version = "0.2.1" }
Empty file added rwf-auth/migrations/.gitkeep
Empty file.
3 changes: 3 additions & 0 deletions rwf-auth/src/controllers/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file is automatically generated by rwf-cli.
// Manual modifications to this file will not be preserved.
pub mod password;
101 changes: 101 additions & 0 deletions rwf-auth/src/controllers/password.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
use std::marker::PhantomData;

use rwf::{
model::{user::Error as UserError, UserModel},
prelude::*,
};

#[derive(macros::Form)]
struct PasswordForm {
identifier: String,
password: String,
}

/// Errors passed to the template.
#[derive(macros::Context, Default)]
pub struct Errors {
/// Something was wrong with the identifier.
pub error_identifier: bool,
/// Password was incorrect or the user didn't exist.
pub error_password: bool,
}

impl Errors {
fn form() -> Self {
let mut ctx = Self::default();
ctx.error_identifier = true;
ctx.error_password = true;
ctx
}

fn wrong_password() -> Self {
let mut ctx = Self::default();
ctx.error_password = true;
ctx
}
}

#[derive(Default)]
pub struct Password<T: UserModel> {
template_path: String,
redirect_url: String,
_marker: PhantomData<T>,
}

impl<T: UserModel> Password<T> {
pub fn template(template_path: &str) -> Self {
Self {
template_path: template_path.to_owned(),
redirect_url: "/".into(),
_marker: PhantomData,
}
}

pub fn redirect(mut self, redirect_url: &str) -> Self {
self.redirect_url = redirect_url.to_owned();
self
}
}

#[async_trait]
impl<T: UserModel> Controller for Password<T> {
async fn handle(&self, request: &Request) -> Result<Response, Error> {
PageController::handle(self, request).await
}
}

#[async_trait]
impl<T: UserModel> PageController for Password<T> {
async fn get(&self, request: &Request) -> Result<Response, Error> {
render!(request, &self.template_path)
}

async fn post(&self, request: &Request) -> Result<Response, Error> {
let tpl = Template::load(&self.template_path)?;

let form = if let Ok(form) = request.form::<PasswordForm>() {
form
} else {
return Ok(Response::new().html(tpl.render(Errors::form())?).code(400));
};

let user = match T::create_user(&form.identifier, &form.password).await {
Ok(user) => user,
Err(UserError::UserExists) => {
match T::login_user(&form.identifier, &form.password).await {
Ok(user) => user,
Err(UserError::WrongPassword) => {
return Ok(Response::new()
.html(tpl.render(Errors::wrong_password())?)
.code(400))
}
Err(err) => return Err(err.into()),
}
}

Err(err) => return Err(err.into()),
};

Ok(request.login_user(&user)?.redirect(&self.redirect_url))
}
}
2 changes: 2 additions & 0 deletions rwf-auth/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod controllers;
pub mod models;
Empty file added rwf-auth/src/models/mod.rs
Empty file.
Empty file added rwf-auth/static/.gitkeep
Empty file.
Empty file added rwf-auth/templates/.gitkeep
Empty file.
8 changes: 4 additions & 4 deletions rwf-macros/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::prelude::*;
struct RenderInput {
request: Expr,
_comma_0: Token![,],
template_name: LitStr,
template_name: Expr,
_comma_1: Option<Token![,]>,
context: Vec<ContextInput>,
code: Option<LitInt>,
Expand All @@ -13,7 +13,7 @@ struct RenderInput {
struct TurboStreamInput {
request: Expr,
_comma_0: Token![,],
template_name: LitStr,
template_name: Expr,
_comma_1: Token![,],
id: Expr,
_comma_2: Option<Token![,]>,
Expand All @@ -38,7 +38,7 @@ impl Parse for TurboStreamInput {
fn parse(input: ParseStream) -> Result<Self> {
let request: Expr = input.parse()?;
let _comma_0: Token![,] = input.parse()?;
let template_name: LitStr = input.parse()?;
let template_name: Expr = input.parse()?;
let _comma_1: Token![,] = input.parse()?;
let id: Expr = input.parse()?;
let _comma_2: Option<Token![,]> = input.parse()?;
Expand Down Expand Up @@ -116,7 +116,7 @@ impl Parse for RenderInput {
fn parse(input: ParseStream) -> Result<Self> {
let request: Expr = input.parse()?;
let _comma_0: Token![,] = input.parse()?;
let template_name: LitStr = input.parse()?;
let template_name: Expr = input.parse()?;
let _comma_1: Option<Token![,]> = input.parse()?;
let mut code = None;
let mut _comma_2 = None;
Expand Down

0 comments on commit ee4eb92

Please sign in to comment.