New character route, and login cleanup.

This commit is contained in:
jeff 2020-12-03 19:45:56 +00:00
parent ea506ab54b
commit 499b1748ce
7 changed files with 36 additions and 6 deletions

View File

@ -1,3 +1,4 @@
pub mod auth;
pub mod characters;
pub mod common;
pub mod root;

View File

@ -17,13 +17,13 @@ struct Login {
#[post("/login", data = "<login>")]
fn login(mut cookies: Cookies, login: Form<Login>) -> Result<Redirect, Flash<Redirect>> {
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.",
))
}
}

View File

@ -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<rocket::Route> {
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<Template, Error> {
let context = HashMap::<String, String>::new();
Ok(Template::render("new_character", context))
}
#[get("/new", rank = 2)]
fn new_character_not_logged_in() -> Redirect {
super::common::redirect_to_login()
}
#[get("/<owner>/<character_id>/edit")]
fn edit_character(
character_id: i32,

6
src/routes/common.rs Normal file
View File

@ -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))
}

View File

@ -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()
}

View File

@ -2,7 +2,7 @@
{% block content %}
<div>
<h1>Rocket Session: Please Login</h1>
<h1>Tenebrous: Login</h1>
<p>Please login to continue.</p>

View File

@ -0,0 +1,7 @@
{% extends "base" %}
{% block content %}
<div>
New character page.
</div>
{% endblock content %}