From 2fde47d909a1a2aa01f8ccfb36c559dcc5c15d52 Mon Sep 17 00:00:00 2001 From: Trinity Pointard Date: Mon, 4 Jun 2018 20:21:43 +0200 Subject: [PATCH 1/2] add optional login message and callback --- src/main.rs | 1 + src/routes/session.rs | 24 ++++++++++++++++++++---- templates/session/login.tera | 3 +++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7b6b803b..2c50b408 100644 --- a/src/main.rs +++ b/src/main.rs @@ -94,6 +94,7 @@ fn main() { routes::reshares::create, routes::session::new, + routes::session::new_message, routes::session::create, routes::session::delete, diff --git a/src/routes/session.rs b/src/routes/session.rs index f8a1ed94..bf7d5e6b 100644 --- a/src/routes/session.rs +++ b/src/routes/session.rs @@ -1,7 +1,7 @@ use rocket::{ http::{Cookie, Cookies}, response::{Redirect, status::NotFound}, - request::Form + request::{Form,FlashMessage} }; use rocket_contrib::Template; @@ -15,6 +15,20 @@ fn new(user: Option) -> Template { })) } +#[derive(FromForm)] +struct Message { + m: String +} + +#[get("/login?")] +fn new_message(user: Option, message: Message) -> Template { + Template::render("session/login", json!({ + "account": user, + "message": message.m + })) +} + + #[derive(FromForm)] struct LoginForm { email_or_name: String, @@ -22,7 +36,7 @@ struct LoginForm { } #[post("/login", data = "")] -fn create(conn: DbConn, data: Form, mut cookies: Cookies) -> Result> { +fn create(conn: DbConn, data: Form, flash: Option, mut cookies: Cookies) -> Result> { let form = data.get(); let user = match User::find_by_email(&*conn, form.email_or_name.to_string()) { Some(usr) => Ok(usr), @@ -31,12 +45,14 @@ fn create(conn: DbConn, data: Form, mut cookies: Cookies) -> Result Err("Invalid username or password") } }; - match user { Ok(usr) => { if usr.auth(form.password.to_string()) { cookies.add_private(Cookie::new(AUTH_COOKIE, usr.id.to_string())); - Ok(Redirect::to("/")) + Ok(Redirect::to(&flash + .and_then(|f| if f.name()=="callback" { Some(f.msg().to_owned()) } else { None }) + .unwrap_or("/".to_owned())) + ) } else { Err(NotFound(String::from("Invalid username or password"))) } diff --git a/templates/session/login.tera b/templates/session/login.tera index eb2cebe8..1099b5e8 100644 --- a/templates/session/login.tera +++ b/templates/session/login.tera @@ -6,6 +6,9 @@ Login {% block content %}

Login

+{% if message %} +

{{ message | escape }}

+{% endif %}
From 8158f19b858fccdd2e9817a45c3343a3938625e4 Mon Sep 17 00:00:00 2001 From: Trinity Pointard Date: Mon, 4 Jun 2018 21:57:03 +0200 Subject: [PATCH 2/2] add fallback to url generating 404 --- src/main.rs | 5 +++++ src/routes/blogs.rs | 7 ++++++- src/routes/comments.rs | 12 +++++++++++- src/routes/likes.rs | 9 ++++++++- src/routes/notifications.rs | 8 ++++++++ src/routes/posts.rs | 8 ++++---- src/routes/reshares.rs | 9 ++++++++- src/utils.rs | 6 +++--- 8 files changed, 53 insertions(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index 2c50b408..04403e25 100644 --- a/src/main.rs +++ b/src/main.rs @@ -71,9 +71,11 @@ fn main() { routes::blogs::activity_details, routes::blogs::outbox, routes::blogs::new, + routes::blogs::new_auth, routes::blogs::create, routes::comments::new, + routes::comments::new_auth, routes::comments::create, routes::instance::index, @@ -82,8 +84,10 @@ fn main() { routes::instance::shared_inbox, routes::likes::create, + routes::likes::create_auth, routes::notifications::notifications, + routes::notifications::notifications_auth, routes::posts::details, routes::posts::activity_details, @@ -92,6 +96,7 @@ fn main() { routes::posts::create, routes::reshares::create, + routes::reshares::create_auth, routes::session::new, routes::session::new_message, diff --git a/src/routes/blogs.rs b/src/routes/blogs.rs index 2df26cf5..863f60a2 100644 --- a/src/routes/blogs.rs +++ b/src/routes/blogs.rs @@ -1,7 +1,7 @@ use activitystreams_types::collection::OrderedCollection; use rocket::{ request::Form, - response::Redirect + response::{Redirect, Flash} }; use rocket_contrib::Template; use serde_json; @@ -53,6 +53,11 @@ fn new(user: User) -> Template { })) } +#[get("/blogs/new", rank = 2)] +fn new_auth() -> Flash{ + utils::requires_login("You need to be logged in order to create a new blog", "/blogs/new") +} + #[derive(FromForm)] struct NewBlogForm { pub title: String diff --git a/src/routes/comments.rs b/src/routes/comments.rs index 2a47b87f..57f5a642 100644 --- a/src/routes/comments.rs +++ b/src/routes/comments.rs @@ -1,4 +1,7 @@ -use rocket::{ request::Form, response::Redirect}; +use rocket::{ + request::Form, + response::{Redirect, Flash} +}; use rocket_contrib::Template; use activity_pub::broadcast; @@ -9,6 +12,8 @@ use models::{ users::User }; +use utils; + #[get("/~/<_blog>//comment")] fn new(_blog: String, slug: String, user: User, conn: DbConn) -> Template { let post = Post::find_by_slug(&*conn, slug).unwrap(); @@ -18,6 +23,11 @@ fn new(_blog: String, slug: String, user: User, conn: DbConn) -> Template { })) } +#[get("/~///comment", rank=2)] +fn new_auth(blog: String, slug: String) -> Flash{ + utils::requires_login("You need to be logged in order to post a comment", &format!("~/{}/{}/comment", blog, slug)) +} + #[derive(FromForm)] struct CommentQuery { responding_to: Option diff --git a/src/routes/likes.rs b/src/routes/likes.rs index 11c578bb..55dce944 100644 --- a/src/routes/likes.rs +++ b/src/routes/likes.rs @@ -1,4 +1,4 @@ -use rocket::response::Redirect; +use rocket::response::{Redirect, Flash}; use activity_pub::broadcast; use db_conn::DbConn; @@ -8,6 +8,8 @@ use models::{ users::User }; +use utils; + #[get("/~///like")] fn create(blog: String, slug: String, user: User, conn: DbConn) -> Redirect { let post = Post::find_by_slug(&*conn, slug.clone()).unwrap(); @@ -29,3 +31,8 @@ fn create(blog: String, slug: String, user: User, conn: DbConn) -> Redirect { Redirect::to(format!("/~/{}/{}/", blog, slug).as_ref()) } + +#[get("/~///like", rank = 2)] +fn create_auth(blog: String, slug: String) -> Flash{ + utils::requires_login("You need to be logged in order to like a post", &format!("/~/{}/{}/like", blog, slug)) +} diff --git a/src/routes/notifications.rs b/src/routes/notifications.rs index ae8c0d0b..6b096148 100644 --- a/src/routes/notifications.rs +++ b/src/routes/notifications.rs @@ -1,8 +1,11 @@ +use rocket::response::{Redirect, Flash}; use rocket_contrib::Template; use db_conn::DbConn; use models::{notifications::Notification, users::User}; +use utils; + #[get("/notifications")] fn notifications(conn: DbConn, user: User) -> Template { Template::render("notifications/index", json!({ @@ -10,3 +13,8 @@ fn notifications(conn: DbConn, user: User) -> Template { "notifications": Notification::find_for_user(&*conn, &user) })) } + +#[get("/notifications", rank = 2)] +fn notifications_auth() -> Flash{ + utils::requires_login("You need to be logged in order to see your notifications", "/notifications") +} diff --git a/src/routes/posts.rs b/src/routes/posts.rs index 60be113b..675159b7 100644 --- a/src/routes/posts.rs +++ b/src/routes/posts.rs @@ -1,7 +1,7 @@ use comrak::{markdown_to_html, ComrakOptions}; use heck::KebabCase; use rocket::request::Form; -use rocket::response::Redirect; +use rocket::response::{Redirect, Flash}; use rocket_contrib::Template; use serde_json; @@ -57,9 +57,9 @@ fn activity_details(_blog: String, slug: String, conn: DbConn) -> ActivityPub { activity_pub(act) } -#[get("/~/<_blog>/new", rank = 2)] -fn new_auth(_blog: String) -> Redirect { - utils::requires_login() +#[get("/~//new", rank = 2)] +fn new_auth(blog: String) -> Flash { + utils::requires_login("You need to be logged in order to write a new post", &format!("/~/{}/new",blog)) } #[get("/~/<_blog>/new", rank = 1)] diff --git a/src/routes/reshares.rs b/src/routes/reshares.rs index 7cf74d9d..19f69332 100644 --- a/src/routes/reshares.rs +++ b/src/routes/reshares.rs @@ -1,4 +1,4 @@ -use rocket::response::Redirect; +use rocket::response::{Redirect, Flash}; use activity_pub::broadcast; use db_conn::DbConn; @@ -8,6 +8,8 @@ use models::{ users::User }; +use utils; + #[get("/~///reshare")] fn create(blog: String, slug: String, user: User, conn: DbConn) -> Redirect { let post = Post::find_by_slug(&*conn, slug.clone()).unwrap(); @@ -29,3 +31,8 @@ fn create(blog: String, slug: String, user: User, conn: DbConn) -> Redirect { Redirect::to(format!("/~/{}/{}/", blog, slug).as_ref()) } + +#[get("/~///reshare", rank=1)] +fn create_auth(blog: String, slug: String) -> Flash { + utils::requires_login("You need to be logged in order to reshare a post", &format!("/~/{}/{}/reshare",blog, slug)) +} diff --git a/src/utils.rs b/src/utils.rs index 8e3b9bbf..eb6d1100 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,5 +1,5 @@ use heck::CamelCase; -use rocket::response::Redirect; +use rocket::response::{Redirect, Flash}; /// Remove non alphanumeric characters and CamelCase a string pub fn make_actor_id(name: String) -> String { @@ -11,6 +11,6 @@ pub fn make_actor_id(name: String) -> String { .collect() } -pub fn requires_login() -> Redirect { - Redirect::to("/login") +pub fn requires_login(message: &str, url: &str) -> Flash { + Flash::new(Redirect::to(&format!("/login?m={}", message)), "callback", url) }