From 499b1748cecdda7770e6f8f8e3bf662033877351 Mon Sep 17 00:00:00 2001 From: jeff Date: Thu, 3 Dec 2020 19:45:56 +0000 Subject: [PATCH] New character route, and login cleanup. --- src/routes.rs | 1 + src/routes/auth.rs | 4 ++-- src/routes/characters.rs | 20 ++++++++++++++++++-- src/routes/common.rs | 6 ++++++ src/routes/root.rs | 2 +- templates/login.html.tera | 2 +- templates/new_character.html.tera | 7 +++++++ 7 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 src/routes/common.rs create mode 100644 templates/new_character.html.tera diff --git a/src/routes.rs b/src/routes.rs index 818984b..4df8d41 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -1,3 +1,4 @@ pub mod auth; pub mod characters; +pub mod common; pub mod root; diff --git a/src/routes/auth.rs b/src/routes/auth.rs index a675709..99cc033 100644 --- a/src/routes/auth.rs +++ b/src/routes/auth.rs @@ -17,13 +17,13 @@ struct Login { #[post("/login", data = "")] fn login(mut cookies: Cookies, login: Form) -> Result> { - if login.username == "Sergio" && login.password == "password" { + if login.username == "test" && login.password == "test" { cookies.add_private(Cookie::new("user_id", 1.to_string())); Ok(Redirect::to(uri!(super::root::index))) } else { Err(Flash::error( Redirect::to(uri!(login_page)), - "Invalid username/password.", + "Invalid username orpassword.", )) } } diff --git a/src/routes/characters.rs b/src/routes/characters.rs index 337743a..3809910 100644 --- a/src/routes/characters.rs +++ b/src/routes/characters.rs @@ -4,13 +4,18 @@ use crate::models::{ characters::{CharacterEntry, NewCharacter}, User, }; -use diesel::prelude::*; +use rocket::response::Redirect; use rocket_contrib::templates::Template; use serde_derive::Serialize; use std::collections::HashMap; pub(crate) fn routes() -> Vec { - routes![view_character, edit_character] + routes![ + view_character, + new_character, + new_character_not_logged_in, + edit_character + ] } //TODO make private -- currently is referenced in homepage route. @@ -37,6 +42,17 @@ fn view_character( Ok(Template::render("view_character", context)) } +#[get("/new")] +fn new_character(logged_in_user: User, conn: TenebrousDbConn) -> Result { + let context = HashMap::::new(); + Ok(Template::render("new_character", context)) +} + +#[get("/new", rank = 2)] +fn new_character_not_logged_in() -> Redirect { + super::common::redirect_to_login() +} + #[get("///edit")] fn edit_character( character_id: i32, diff --git a/src/routes/common.rs b/src/routes/common.rs new file mode 100644 index 0000000..7dd6bcb --- /dev/null +++ b/src/routes/common.rs @@ -0,0 +1,6 @@ +use rocket::response::Redirect; + +/// Common redirect to the login page. +pub(super) fn redirect_to_login() -> Redirect { + Redirect::to(uri!(super::auth::login_page)) +} diff --git a/src/routes/root.rs b/src/routes/root.rs index 7d542ff..e85a2a3 100644 --- a/src/routes/root.rs +++ b/src/routes/root.rs @@ -37,5 +37,5 @@ fn user_index(user: User, conn: TenebrousDbConn) -> Template { #[get("/", rank = 2)] fn index() -> Redirect { - Redirect::to(uri!(super::auth::login_page)) + super::common::redirect_to_login() } diff --git a/templates/login.html.tera b/templates/login.html.tera index b59ee80..b7653d3 100644 --- a/templates/login.html.tera +++ b/templates/login.html.tera @@ -2,7 +2,7 @@ {% block content %}
-

Rocket Session: Please Login

+

Tenebrous: Login

Please login to continue.

diff --git a/templates/new_character.html.tera b/templates/new_character.html.tera new file mode 100644 index 0000000..a1eb8b8 --- /dev/null +++ b/templates/new_character.html.tera @@ -0,0 +1,7 @@ +{% extends "base" %} + +{% block content %} +
+New character page. +
+{% endblock content %}