This commit is contained in:
Michael Zhang 2019-02-17 15:44:44 -06:00
parent fabd64a42e
commit b36d01fde5
No known key found for this signature in database
GPG Key ID: 5BAEFE5D04F0CE6C
5 changed files with 56 additions and 6 deletions

View File

@ -28,6 +28,9 @@ pub trait ErrorExt: StdError {
/// An error caused by the user. /// An error caused by the user.
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
pub enum UserErrorKind { pub enum UserErrorKind {
/// Forbidden
Forbidden,
/// The user supplied bad credentials during login. /// The user supplied bad credentials during login.
BadUsernameOrPassword, BadUsernameOrPassword,
} }
@ -37,12 +40,16 @@ pub enum UserErrorKind {
pub enum ErrorKind { pub enum ErrorKind {
#[doc(hidden)] #[doc(hidden)]
Bcrypt, Bcrypt,
#[doc(hidden)] #[doc(hidden)]
Diesel, Diesel,
#[doc(hidden)] #[doc(hidden)]
Migrations, Migrations,
#[doc(hidden)] #[doc(hidden)]
R2d2, R2d2,
#[doc(hidden)] #[doc(hidden)]
Tera, Tera,

View File

@ -1,8 +1,23 @@
use core::models::User; use core::{models::User, Error, UserErrorKind};
use warp::{Filter, Rejection}; use warp::{Filter, Rejection};
use crate::session::Session;
use crate::extractors::get; use crate::extractors::get;
pub fn require_login() -> impl Clone + Filter<Extract = (), Error = Rejection> { pub fn require_login() -> impl Clone + Filter<Extract = (), Error = Rejection> {
get::<User>().map(|_| ()).untuple_one() get::<Session>()
.and_then(|session: Session|
match session.get_user() {
Some(user) => Ok(()),
None => Err(warp::reject::not_found())
}
)
.recover(|_| {
Err(warp::reject::custom(Error::user(
"You're not allowed to be here.",
UserErrorKind::Forbidden,
)))
})
.unify()
.untuple_one()
} }

View File

@ -56,8 +56,7 @@ pub fn routes(state: State) -> BoxedFilter<(impl Reply,)> {
GET ("users" / "register") => users::get_register(), GET ("users" / "register") => users::get_register(),
POST ("users" / "login") => users::post_login(), POST ("users" / "login") => users::post_login(),
POST ("users" / "register") => users::post_register(), POST ("users" / "register") => users::post_register(),
} };
.recover(Error::reply);
let statics = warp::path("static") let statics = warp::path("static")
.and(warp::path::param()) .and(warp::path::param())
@ -73,5 +72,5 @@ pub fn routes(state: State) -> BoxedFilter<(impl Reply,)> {
None => warp::reply::with_header(reply, "please", "fix this"), None => warp::reply::with_header(reply, "please", "fix this"),
}); });
statics.or(routes).boxed() statics.or(routes).recover(Error::reply).boxed()
} }

View File

@ -10,7 +10,7 @@ pub fn get_create() -> Resp!() {
require_login() require_login()
.and(navbar()) .and(navbar())
.and(get_context()) .and(get_context())
.and_then(|ctx: Context| render_template("users/login.html", ctx.into())) .and_then(|ctx: Context| render_template("teams/create.html", ctx.into()))
} }
pub fn get_index() -> Resp!() { pub fn get_index() -> Resp!() {

View File

@ -2,4 +2,33 @@
{% block title %}Create Team{% endblock %} {% block title %}Create Team{% endblock %}
{% block content %} {% block content %}
<section class="hero">
<div class="hero-body">
<div class="container has-text-centered">
<div class="column is-4 is-offset-4">
<h3 class="title has-text-grey">Create Team</h3>
<div class="box">
<form action="/teams/create" method="POST">
<div class="field">
<div class="control">
<input class="input" type="text" name="name" placeholder="Team name" autofocus="">
</div>
</div>
<div class="field">
<div class="control">
<input class="input" type="text" name="affiliation" placeholder="(optional) affiliation" autofocus="">
</div>
</div>
<button class="button is-block is-info is-fullwidth">Login</button>
</form>
</div>
<p>&nbsp;</p>
<h3 class="title has-text-grey">Join Team</h3>
<div class="box">
<p>You need an invitation to join another team!</p>
</div>
</div>
</div>
</div>
</section>
{% endblock %} {% endblock %}