Using Flash for login error message.
This commit is contained in:
parent
9eaf121454
commit
dd21d419bd
27
src/main.rs
27
src/main.rs
|
@ -26,7 +26,14 @@ use num::BigUint;
|
|||
use openssl::rsa::Rsa;
|
||||
|
||||
use ldap3::{ LdapConn, Scope, SearchEntry };
|
||||
use rocket::request::Form;
|
||||
use rocket::request::{
|
||||
FlashMessage,
|
||||
Form,
|
||||
};
|
||||
use rocket::response::{
|
||||
Flash,
|
||||
Redirect,
|
||||
};
|
||||
use rocket_contrib::json::Json;
|
||||
use rocket_contrib::templates::Template;
|
||||
|
||||
|
@ -110,24 +117,30 @@ struct LoginData {
|
|||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct EmptyContext {
|
||||
struct LoginFormContext {
|
||||
message: Option<String>,
|
||||
}
|
||||
|
||||
#[get("/login")]
|
||||
fn login_form() -> Template {
|
||||
let context = EmptyContext {};
|
||||
fn login_form(flash: Option<FlashMessage<'_, '_>>) -> Template {
|
||||
let context = LoginFormContext {
|
||||
message: match flash {
|
||||
Some(ref msg) => Some(msg.msg().to_string()),
|
||||
_ => None,
|
||||
},
|
||||
};
|
||||
Template::render("login_form", &context)
|
||||
}
|
||||
|
||||
#[post("/login", data = "<form_data>")]
|
||||
fn login(form_data: Form<LoginData>) -> String {
|
||||
fn login(form_data: Form<LoginData>) -> Result<Redirect, Flash<Redirect>> {
|
||||
let auth = BasicAuthentication {
|
||||
username: form_data.username.to_owned(),
|
||||
password: form_data.password.to_owned(),
|
||||
};
|
||||
match auth_user(&auth) {
|
||||
Ok(ldap_user) => format!("OK! {:?}", ldap_user),
|
||||
_ => format!("Bad :("),
|
||||
Ok(_ldap_user) => Ok(Redirect::to("/")),
|
||||
_ => Err(Flash::error(Redirect::to(uri!(login_form)), "Not able to authenticate with given credentials.")),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -3,6 +3,9 @@
|
|||
{% block content %}
|
||||
<h1>Login</h1>
|
||||
<form action="/login" method="POST">
|
||||
{% if message %}
|
||||
<strong>{{ message }}</strong>
|
||||
{% endif %}
|
||||
<fieldset>
|
||||
<legend>Login</legend>
|
||||
<div class="form-group">
|
||||
|
|
Loading…
Reference in New Issue