feat: port join page to tera
This commit is contained in:
parent
23330a29a7
commit
c645bf83a3
3 changed files with 115 additions and 98 deletions
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
|
* Copyright (C) 2022 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU Affero General Public License as
|
* it under the terms of the GNU Affero General Public License as
|
||||||
|
@ -14,76 +14,81 @@
|
||||||
* You should have received a copy of the GNU Affero General Public License
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
use actix_web::HttpResponseBuilder;
|
use actix_web::http::header::ContentType;
|
||||||
use actix_web::{error::ResponseError, http::header, web, HttpResponse, Responder};
|
use std::cell::RefCell;
|
||||||
use lazy_static::lazy_static;
|
use tera::Context;
|
||||||
use sailfish::TemplateOnce;
|
|
||||||
|
use crate::pages::errors::*;
|
||||||
|
use crate::settings::Settings;
|
||||||
|
|
||||||
|
use actix_web::{http::header, web, HttpResponse, Responder};
|
||||||
|
|
||||||
use crate::api::v1::admin::auth::runners;
|
use crate::api::v1::admin::auth::runners;
|
||||||
use crate::errors::*;
|
|
||||||
use crate::pages::errors::ErrorPage;
|
|
||||||
use crate::AppData;
|
use crate::AppData;
|
||||||
use crate::PAGES;
|
use crate::PAGES;
|
||||||
|
|
||||||
#[derive(Clone, TemplateOnce)]
|
pub use super::*;
|
||||||
#[template(path = "auth/join/index.html")]
|
|
||||||
struct IndexPage<'a> {
|
pub const REGISTER: TemplateFile = TemplateFile::new("register", "auth/join/index.html");
|
||||||
error: Option<ErrorPage<'a>>,
|
|
||||||
|
pub struct Register {
|
||||||
|
ctx: RefCell<Context>,
|
||||||
}
|
}
|
||||||
|
|
||||||
const PAGE: &str = "Join";
|
impl CtxError for Register {
|
||||||
|
fn with_error(&self, e: &ReadableError) -> String {
|
||||||
impl<'a> Default for IndexPage<'a> {
|
self.ctx.borrow_mut().insert(ERROR_KEY, e);
|
||||||
fn default() -> Self {
|
self.render()
|
||||||
IndexPage { error: None }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> IndexPage<'a> {
|
impl Register {
|
||||||
pub fn new(title: &'a str, message: &'a str) -> Self {
|
fn new(settings: &Settings) -> Self {
|
||||||
Self {
|
let ctx = RefCell::new(context(settings, "Join"));
|
||||||
error: Some(ErrorPage::new(title, message)),
|
Self { ctx }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn render(&self) -> String {
|
||||||
|
TEMPLATES.render(REGISTER.name, &self.ctx.borrow()).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn page(s: &Settings) -> String {
|
||||||
|
let p = Self::new(s);
|
||||||
|
p.render()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
lazy_static! {
|
#[actix_web_codegen_const_routes::get(path = "PAGES.auth.join")]
|
||||||
static ref INDEX: String = IndexPage::default().render_once().unwrap();
|
#[tracing::instrument(name = "Serve registration page", skip(ctx))]
|
||||||
|
pub async fn get_join(ctx: AppData) -> impl Responder {
|
||||||
|
let login = Register::page(&ctx.settings);
|
||||||
|
let html = ContentType::html();
|
||||||
|
HttpResponse::Ok().content_type(html).body(login)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[my_codegen::get(path = "crate::PAGES.auth.join")]
|
pub fn services(cfg: &mut web::ServiceConfig) {
|
||||||
pub async fn join() -> impl Responder {
|
cfg.service(get_join);
|
||||||
HttpResponse::Ok()
|
cfg.service(join_submit);
|
||||||
.content_type("text/html; charset=utf-8")
|
|
||||||
.body(&*INDEX.as_str())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[my_codegen::post(path = "PAGES.auth.join")]
|
#[actix_web_codegen_const_routes::post(path = "PAGES.auth.join")]
|
||||||
|
#[tracing::instrument(name = "Process web UI registration", skip(data))]
|
||||||
pub async fn join_submit(
|
pub async fn join_submit(
|
||||||
payload: web::Form<runners::Register>,
|
payload: web::Form<runners::Register>,
|
||||||
data: AppData,
|
data: AppData,
|
||||||
) -> PageResult<impl Responder> {
|
) -> PageResult<impl Responder, Register> {
|
||||||
let mut payload = payload.into_inner();
|
let mut payload = payload.into_inner();
|
||||||
if payload.email.is_some() && payload.email.as_ref().unwrap().is_empty() {
|
if payload.email.is_some() && payload.email.as_ref().unwrap().is_empty() {
|
||||||
payload.email = None;
|
payload.email = None;
|
||||||
}
|
}
|
||||||
|
|
||||||
match runners::register_runner(&payload, &data).await {
|
runners::register_runner(&payload, &data)
|
||||||
Ok(()) => Ok(HttpResponse::Found()
|
.await
|
||||||
.insert_header((header::LOCATION, PAGES.auth.login))
|
.map_err(|e| PageError::new(Register::new(&data.settings), e))?;
|
||||||
.finish()),
|
|
||||||
Err(e) => {
|
Ok(HttpResponse::Found()
|
||||||
let status = e.status_code();
|
.insert_header((header::LOCATION, PAGES.auth.login))
|
||||||
let heading = status.canonical_reason().unwrap_or("Error");
|
.finish())
|
||||||
Ok(HttpResponseBuilder::new(status)
|
|
||||||
.content_type("text/html; charset=utf-8")
|
|
||||||
.body(
|
|
||||||
IndexPage::new(heading, &format!("{}", e))
|
|
||||||
.render_once()
|
|
||||||
.unwrap(),
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -103,7 +108,8 @@ mod tests {
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn auth_join_form_works() {
|
async fn auth_join_form_works() {
|
||||||
let data = Data::new().await;
|
let settings = Settings::new().unwrap();
|
||||||
|
let data = Data::new(settings).await;
|
||||||
const NAME: &str = "testuserformjoin";
|
const NAME: &str = "testuserformjoin";
|
||||||
const NAME2: &str = "testuserformjoin2";
|
const NAME2: &str = "testuserformjoin2";
|
||||||
const EMAIL: &str = "testuserformjoin@a.com";
|
const EMAIL: &str = "testuserformjoin@a.com";
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
|
* Copyright (C) 2023 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU Affero General Public License as
|
* it under the terms of the GNU Affero General Public License as
|
||||||
|
@ -14,21 +14,31 @@
|
||||||
* You should have received a copy of the GNU Affero General Public License
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
use actix_web::*;
|
||||||
|
|
||||||
|
pub use super::{context, Footer, TemplateFile, PAGES, PAYLOAD_KEY, TEMPLATES};
|
||||||
|
|
||||||
|
pub fn register_templates(t: &mut tera::Tera) {
|
||||||
|
for template in [login::LOGIN, join::REGISTER].iter() {
|
||||||
|
template.register(t).expect(template.name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub mod join;
|
pub mod join;
|
||||||
pub mod login;
|
pub mod login;
|
||||||
pub mod sudo;
|
//pub mod sudo;
|
||||||
|
|
||||||
pub fn services(cfg: &mut actix_web::web::ServiceConfig) {
|
pub fn services(cfg: &mut actix_web::web::ServiceConfig) {
|
||||||
cfg.service(login::login);
|
login::services(cfg);
|
||||||
cfg.service(login::login_submit);
|
join::services(cfg);
|
||||||
cfg.service(join::join);
|
|
||||||
cfg.service(join::join_submit);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod routes {
|
pub mod routes {
|
||||||
use actix_auth_middleware::GetLoginRoute;
|
use actix_auth_middleware::GetLoginRoute;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
|
||||||
pub struct Auth {
|
pub struct Auth {
|
||||||
pub login: &'static str,
|
pub login: &'static str,
|
||||||
pub join: &'static str,
|
pub join: &'static str,
|
||||||
|
|
|
@ -1,48 +1,49 @@
|
||||||
<. include!("../../components/base/top.html"); .>
|
{% extends 'base' %}
|
||||||
<body class="auth__body">
|
|
||||||
<main class="auth__container">
|
|
||||||
<img src="<.= crate::assets::LOGO.path .>" alt="logo" class="auth__logo" />
|
|
||||||
<h1>Sign in</h1>
|
|
||||||
<. include!("../../components/error/index.html"); .>
|
|
||||||
<form
|
|
||||||
action="<.= crate::PAGES.auth.login .>"
|
|
||||||
method="POST"
|
|
||||||
class="form"
|
|
||||||
accept-charset="utf-8"
|
|
||||||
>
|
|
||||||
<label class="form__label" for="login">
|
|
||||||
Username or Email
|
|
||||||
<input
|
|
||||||
class="form__input"
|
|
||||||
name="login"
|
|
||||||
required
|
|
||||||
id="login"
|
|
||||||
type="text"
|
|
||||||
/>
|
|
||||||
</label>
|
|
||||||
|
|
||||||
<label class="form__label" for="password">
|
{% block body %}
|
||||||
password
|
<body class="auth__body">
|
||||||
<input
|
<main class="auth__container">
|
||||||
class="form__input"
|
<img src="{{ assets.logo.path }}" alt="logo" class="auth__logo" />
|
||||||
name="password"
|
<h1>Sign in</h1>
|
||||||
required
|
{% include "error_comp" %}
|
||||||
id="password"
|
<form
|
||||||
type="password"
|
action="{{ page.auth.login }}"
|
||||||
/>
|
method="POST"
|
||||||
</label>
|
class="form"
|
||||||
|
accept-charset="utf-8"
|
||||||
|
>
|
||||||
|
<label class="form__label" for="login">
|
||||||
|
Username or Email
|
||||||
|
<input
|
||||||
|
class="form__input"
|
||||||
|
name="login"
|
||||||
|
required
|
||||||
|
id="login"
|
||||||
|
type="text"
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
|
||||||
<div class="form__action-container">
|
<label class="form__label" for="password">
|
||||||
<a href="/forgot-password">Forgot password?</a>
|
password
|
||||||
<button class="form__submit" type="submit">Login</button>
|
<input
|
||||||
</div>
|
class="form__input"
|
||||||
</form>
|
name="password"
|
||||||
|
required
|
||||||
|
id="password"
|
||||||
|
type="password"
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
|
||||||
<p class="form__alt-action">
|
<div class="form__action-container">
|
||||||
New to mCaptcha Survey?
|
<a href="/forgot-password">Forgot password?</a>
|
||||||
<a href="<.= crate::PAGES.auth.join .>">Create an account </a>
|
<button class="form__submit" type="submit">Login</button>
|
||||||
</p>
|
</div>
|
||||||
</main>
|
</form>
|
||||||
<. include!("../../components/footer/index.html"); .>
|
|
||||||
</body>
|
<p class="form__alt-action">
|
||||||
<. include!("../../components/base/bottom.html"); .>
|
New to mCaptcha Survey?
|
||||||
|
<a href="{{ page.auth.join }}">Create an account </a>
|
||||||
|
</p>
|
||||||
|
</main>
|
||||||
|
</body>
|
||||||
|
{% endblock body %}
|
||||||
|
|
Loading…
Add table
Reference in a new issue