Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
levkk committed Dec 10, 2024
1 parent 31c623d commit 4d90ddc
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 19 deletions.
17 changes: 6 additions & 11 deletions examples/users/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
use rwf::controller::{LoginController, LogoutController, SignupController};
use rwf::{http::Server, prelude::*};
use rwf_auth::controllers::{LogoutController, PasswordController};

mod controllers;
mod models;

#[tokio::main]
async fn main() {
Logger::init();
rwf_auth::migrate().await.expect("rwf-auth migrations");

let signup: SignupController<models::User> =
SignupController::new("templates/signup.html").redirect("/profile");

let login: LoginController<models::User> =
LoginController::new("templates/login.html").redirect("/profile");

Server::new(vec![
route!("/signup" => { signup }),
route!("/login" => { login }),
route!("/logout" => { LogoutController::default().redirect("/signup") }),
route!("/auth" => {
PasswordController::template("templates/login.html")
.redirect("/profile")
}),
route!("/logout" => { LogoutController::redirect("/") }),
route!("/profile" => controllers::profile),
])
.launch()
Expand Down
2 changes: 1 addition & 1 deletion examples/users/templates/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<body>
<div class="container pt-5 col-lg-6 col-12">
<h1 class="mb-4">Login</h1>
<form method="post" action="/login">
<form method="post" action="/auth">
<%= csrf_token() %>

<% if error_user_does_not_exist %>
Expand Down
2 changes: 1 addition & 1 deletion examples/users/templates/signup.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<body>
<div class="container pt-5 col-lg-6 col-12">
<h1 class="mb-4">Create account</h1>
<form method="post" action="/signup">
<form method="post" action="/auth">
<% if error_user_exists %>
<div class="alert alert-danger">
Account with this email already exists.
Expand Down
31 changes: 31 additions & 0 deletions rwf-auth/src/controllers/logout.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//! Logout controller.
use rwf::prelude::*;

/// Log the user out.
pub struct LogoutController {
redirect: String,
}

impl LogoutController {
/// Redirect user to this URL after logging out.
pub fn redirect(redirect: impl ToString) -> Self {
Self {
redirect: redirect.to_string(),
}
}
}

impl Default for LogoutController {
fn default() -> Self {
Self {
redirect: "/".to_string(),
}
}
}

#[async_trait]
impl Controller for LogoutController {
async fn handle(&self, request: &Request) -> Result<Response, Error> {
Ok(request.logout().redirect(self.redirect.clone()))
}
}
6 changes: 3 additions & 3 deletions rwf-auth/src/controllers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// This file is automatically generated by rwf-cli.
// Manual modifications to this file will not be preserved.
pub mod logout;
pub mod password;

pub use password::{Password, PasswordController};
pub use logout::LogoutController;
pub use password::PasswordController;
3 changes: 2 additions & 1 deletion rwf-auth/src/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use rwf::prelude::*;

#[derive(Clone, macros::Model, macros::UserModel, Debug)]
#[derive(Clone, macros::Model, Debug, macros::UserModel)]
#[table_name("rwf_auth_users")]
#[user_model(identifier, password)]
pub struct User {
id: Option<i64>,
identifier: String,
Expand Down
2 changes: 1 addition & 1 deletion rwf-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ pub fn controller(_args: TokenStream, input: TokenStream) -> TokenStream {
.into()
}

#[proc_macro_derive(UserModel, attributes(user_model))]
#[proc_macro_derive(UserModel, attributes(user_model, rwf))]
pub fn derive_user_model(input: TokenStream) -> TokenStream {
user::impl_derive_user_model(input)
}
2 changes: 1 addition & 1 deletion rwf-macros/src/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ 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() {
for attr in input.attrs {
match attr.meta {
Meta::List(ref attrs) => {
if let Ok(attrs) = syn::parse2::<UserModel>(attrs.tokens.clone()) {
Expand Down

0 comments on commit 4d90ddc

Please sign in to comment.