fix: duplicate home page. Redirect to login page, if user is
unauthenticated and redirect to dashboard homepage if user is authenticated
This commit is contained in:
parent
26fdc1db9f
commit
8a25459985
4 changed files with 16 additions and 187 deletions
|
@ -77,7 +77,6 @@ lazy_static! {
|
|||
tera.autoescape_on(vec![".html", ".sql"]);
|
||||
auth::register_templates(&mut tera);
|
||||
dash::register_templates(&mut tera);
|
||||
HOME.register(&mut tera).expect(HOME.name);
|
||||
tera
|
||||
};
|
||||
}
|
||||
|
@ -135,53 +134,20 @@ impl<'a> Footer<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
pub const HOME: TemplateFile = TemplateFile::new("home", "pages/index.html");
|
||||
|
||||
pub struct Home {
|
||||
ctx: RefCell<Context>,
|
||||
}
|
||||
|
||||
impl CtxError for Home {
|
||||
fn with_error(&self, e: &ReadableError) -> String {
|
||||
self.ctx.borrow_mut().insert(ERROR_KEY, e);
|
||||
self.render()
|
||||
}
|
||||
}
|
||||
|
||||
impl Home {
|
||||
pub fn new(settings: &Settings) -> Self {
|
||||
let ctx = RefCell::new(context(settings));
|
||||
Self { ctx }
|
||||
}
|
||||
|
||||
pub fn render(&self) -> String {
|
||||
TEMPLATES.render(HOME.name, &self.ctx.borrow()).unwrap()
|
||||
}
|
||||
|
||||
pub fn page(s: &Settings) -> String {
|
||||
let p = Self::new(s);
|
||||
p.render()
|
||||
}
|
||||
}
|
||||
|
||||
#[actix_web_codegen_const_routes::get(path = "PAGES.home")]
|
||||
#[tracing::instrument(name = "Dashboard homepage", skip(id, ctx))]
|
||||
pub async fn home(ctx: AppCtx, id: Identity) -> impl Responder {
|
||||
if id.identity().is_none() {
|
||||
let home = Home::page(&ctx.settings);
|
||||
let html = header::ContentType::html();
|
||||
HttpResponse::Ok().content_type(html).body(home)
|
||||
pub async fn home(ctx: AppCtx, id: &Identity) -> HttpResponse {
|
||||
let location = if id.identity().is_some() {
|
||||
PAGES.home
|
||||
} else {
|
||||
HttpResponse::Found()
|
||||
.append_header((header::LOCATION, PAGES.dash.home))
|
||||
.finish()
|
||||
}
|
||||
PAGES.dash.home
|
||||
};
|
||||
HttpResponse::Found()
|
||||
.append_header((header::LOCATION, location))
|
||||
.finish()
|
||||
}
|
||||
|
||||
pub fn services(cfg: &mut web::ServiceConfig) {
|
||||
auth::services(cfg);
|
||||
dash::services(cfg);
|
||||
cfg.service(home);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -203,7 +169,6 @@ mod tests {
|
|||
auth::login::LOGIN,
|
||||
auth::register::REGISTER,
|
||||
errors::ERROR_TEMPLATE,
|
||||
HOME,
|
||||
]
|
||||
.iter()
|
||||
{
|
||||
|
|
|
@ -35,7 +35,7 @@ impl Pages {
|
|||
const fn new() -> Pages {
|
||||
let auth = Auth::new();
|
||||
let dash = Dash::new();
|
||||
let home = "/";
|
||||
let home = auth.login;
|
||||
Pages { auth, home, dash }
|
||||
}
|
||||
}
|
||||
|
|
12
src/serve.rs
12
src/serve.rs
|
@ -1,3 +1,4 @@
|
|||
use actix_identity::Identity;
|
||||
/*
|
||||
* Copyright (C) 2022 Aravinth Manivannan <realaravinth@batsense.net>
|
||||
*
|
||||
|
@ -17,6 +18,7 @@
|
|||
use actix_web::{http::header::ContentType, web, HttpRequest, HttpResponse, Responder};
|
||||
|
||||
use crate::errors::*;
|
||||
use crate::pages;
|
||||
use crate::AppCtx;
|
||||
|
||||
pub mod routes {
|
||||
|
@ -34,19 +36,19 @@ pub mod routes {
|
|||
}
|
||||
|
||||
#[actix_web_codegen_const_routes::get(path = "crate::V1_API_ROUTES.serve.catch_all")]
|
||||
#[tracing::instrument(name = "Serve webpages", skip(req, ctx))]
|
||||
async fn index(req: HttpRequest, ctx: AppCtx) -> ServiceResult<impl Responder> {
|
||||
#[tracing::instrument(name = "Serve webpages", skip(req, ctx, id))]
|
||||
async fn index(req: HttpRequest, ctx: AppCtx, id: Identity) -> ServiceResult<impl Responder> {
|
||||
let c = req.connection_info();
|
||||
let mut host = c.host();
|
||||
if host.contains(':') {
|
||||
host = host.split(':').next().unwrap();
|
||||
}
|
||||
tracing::debug!("Current host {host}");
|
||||
|
||||
// serve meta page
|
||||
if host == ctx.settings.server.domain || host == "localhost" {
|
||||
return Ok(HttpResponse::Ok()
|
||||
.content_type(ContentType::html())
|
||||
.body("Welcome to Librepages!"));
|
||||
tracing::debug!("Into home");
|
||||
return Ok(pages::home(ctx.clone(), &id).await);
|
||||
}
|
||||
|
||||
// serve default hostname content
|
||||
|
|
|
@ -1,138 +0,0 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<link rel="stylesheet" href="{{ assets.css }}" />
|
||||
<title>LibrePages</title>
|
||||
</head>
|
||||
<body class="auth__body">
|
||||
<header>
|
||||
<nav>
|
||||
<p>LibrePages</p>
|
||||
<span class="nav__spacer"></span>
|
||||
<ul class="nav__links">
|
||||
<li class="nav__item">Help</li>
|
||||
</ul>
|
||||
</nav>
|
||||
</header>
|
||||
<main>
|
||||
<h1>LibrePages: FOSS static site hosting</h1>
|
||||
<p>Welcome to LibrePages. Homepage.</p>
|
||||
</main>
|
||||
{% include "footer" %}
|
||||
</body>
|
||||
|
||||
<style>
|
||||
header {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
nav {
|
||||
width: 100%;
|
||||
margin: auto;
|
||||
display: flex;
|
||||
}
|
||||
|
||||
.nav__spacer {
|
||||
flex: 4;
|
||||
}
|
||||
|
||||
.nav__links {
|
||||
display: flex;
|
||||
list-style: none;
|
||||
}
|
||||
|
||||
.nav__item {
|
||||
margin: 0 20px;
|
||||
}
|
||||
|
||||
body {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
main {
|
||||
width: 100%;
|
||||
margin: 20px;
|
||||
}
|
||||
|
||||
.sites__collection {
|
||||
margin: auto;
|
||||
width: 70%;
|
||||
|
||||
border: 1px solid #e8ebed;
|
||||
border-radius: 8px;
|
||||
}
|
||||
|
||||
.sites__actions {
|
||||
width: 100%;
|
||||
height: 50px;
|
||||
display: flex;
|
||||
flex-direction: row-reverse;
|
||||
align-items: center;
|
||||
box-sizing: border-box;
|
||||
padding: 0px 20px;
|
||||
margin: 10px 0;
|
||||
}
|
||||
|
||||
.sites__actions__new-site {
|
||||
min-height: 36px;
|
||||
background: green;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
padding: 0px 8px;
|
||||
}
|
||||
|
||||
.sites__actions__new-site > button {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
height: 100%;
|
||||
border: none;
|
||||
width: 100%;
|
||||
color: white;
|
||||
background: none;
|
||||
}
|
||||
|
||||
.site__container {
|
||||
box-sizing: border-box;
|
||||
margin: 10px 0;
|
||||
width: 100%;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
padding: 10px 20px;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.site__container:hover {
|
||||
background: #f7f8f8;
|
||||
}
|
||||
|
||||
.site__info--head {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.site__info--column {
|
||||
margin-left: 20px;
|
||||
}
|
||||
|
||||
.site__info--column > p,
|
||||
.site__info--column > a {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
.site__container:visited,
|
||||
.site__container {
|
||||
color: black;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.site__container--preview {
|
||||
width: 50px;
|
||||
height: 50px;
|
||||
border-radius: 50%;
|
||||
}
|
||||
</style>
|
||||
</html>
|
Loading…
Reference in a new issue