add optional login message and callback
This commit is contained in:
parent
d604b62917
commit
2fde47d909
@ -94,6 +94,7 @@ fn main() {
|
|||||||
routes::reshares::create,
|
routes::reshares::create,
|
||||||
|
|
||||||
routes::session::new,
|
routes::session::new,
|
||||||
|
routes::session::new_message,
|
||||||
routes::session::create,
|
routes::session::create,
|
||||||
routes::session::delete,
|
routes::session::delete,
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use rocket::{
|
use rocket::{
|
||||||
http::{Cookie, Cookies},
|
http::{Cookie, Cookies},
|
||||||
response::{Redirect, status::NotFound},
|
response::{Redirect, status::NotFound},
|
||||||
request::Form
|
request::{Form,FlashMessage}
|
||||||
};
|
};
|
||||||
use rocket_contrib::Template;
|
use rocket_contrib::Template;
|
||||||
|
|
||||||
@ -15,6 +15,20 @@ fn new(user: Option<User>) -> Template {
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(FromForm)]
|
||||||
|
struct Message {
|
||||||
|
m: String
|
||||||
|
}
|
||||||
|
|
||||||
|
#[get("/login?<message>")]
|
||||||
|
fn new_message(user: Option<User>, message: Message) -> Template {
|
||||||
|
Template::render("session/login", json!({
|
||||||
|
"account": user,
|
||||||
|
"message": message.m
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(FromForm)]
|
#[derive(FromForm)]
|
||||||
struct LoginForm {
|
struct LoginForm {
|
||||||
email_or_name: String,
|
email_or_name: String,
|
||||||
@ -22,7 +36,7 @@ struct LoginForm {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[post("/login", data = "<data>")]
|
#[post("/login", data = "<data>")]
|
||||||
fn create(conn: DbConn, data: Form<LoginForm>, mut cookies: Cookies) -> Result<Redirect, NotFound<String>> {
|
fn create(conn: DbConn, data: Form<LoginForm>, flash: Option<FlashMessage>, mut cookies: Cookies) -> Result<Redirect, NotFound<String>> {
|
||||||
let form = data.get();
|
let form = data.get();
|
||||||
let user = match User::find_by_email(&*conn, form.email_or_name.to_string()) {
|
let user = match User::find_by_email(&*conn, form.email_or_name.to_string()) {
|
||||||
Some(usr) => Ok(usr),
|
Some(usr) => Ok(usr),
|
||||||
@ -31,12 +45,14 @@ fn create(conn: DbConn, data: Form<LoginForm>, mut cookies: Cookies) -> Result<R
|
|||||||
None => Err("Invalid username or password")
|
None => Err("Invalid username or password")
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
match user {
|
match user {
|
||||||
Ok(usr) => {
|
Ok(usr) => {
|
||||||
if usr.auth(form.password.to_string()) {
|
if usr.auth(form.password.to_string()) {
|
||||||
cookies.add_private(Cookie::new(AUTH_COOKIE, usr.id.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 {
|
} else {
|
||||||
Err(NotFound(String::from("Invalid username or password")))
|
Err(NotFound(String::from("Invalid username or password")))
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,9 @@ Login
|
|||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<h1>Login</h1>
|
<h1>Login</h1>
|
||||||
|
{% if message %}
|
||||||
|
<p>{{ message | escape }}</p>
|
||||||
|
{% endif %}
|
||||||
<form method="post">
|
<form method="post">
|
||||||
<label for="email_or_name">Username or email</label>
|
<label for="email_or_name">Username or email</label>
|
||||||
<input name="email_or_name">
|
<input name="email_or_name">
|
||||||
|
Loading…
Reference in New Issue
Block a user