frontend: logout and add sitekey

This commit is contained in:
realaravinth
2021-04-30 17:30:40 +05:30
parent a5cfa3b305
commit 6e63771868
17 changed files with 255 additions and 104 deletions

View File

@@ -17,6 +17,7 @@
use std::borrow::Cow;
use actix_identity::Identity;
use actix_web::http::header;
use actix_web::{get, post, web, HttpResponse, Responder};
use log::debug;
use serde::{Deserialize, Serialize};
@@ -246,12 +247,14 @@ pub async fn set_email(
Ok(HttpResponse::Ok())
}
#[post("/api/v1/signout")]
#[get("/logout")]
pub async fn signout(id: Identity) -> impl Responder {
if let Some(_) = id.identity() {
id.forget();
}
HttpResponse::Ok()
HttpResponse::TemporaryRedirect()
.set_header(header::LOCATION, "/login")
.body("")
}
/// Check if user is authenticated

View File

@@ -124,13 +124,13 @@ async fn auth_works() {
// 5. signout
let signout_resp = test::call_service(
&mut app,
test::TestRequest::post()
.uri("/api/v1/signout")
test::TestRequest::get()
.uri("/logout")
.cookie(cookies)
.to_request(),
)
.await;
assert_eq!(signout_resp.status(), StatusCode::OK);
assert_eq!(signout_resp.status(), StatusCode::TEMPORARY_REDIRECT);
}
#[actix_rt::test]

View File

@@ -20,30 +20,23 @@ use sailfish::TemplateOnce;
#[derive(Clone, TemplateOnce)]
#[template(path = "auth/login/index.html")]
struct IndexPage {
name: String,
title: String,
struct IndexPage<'a> {
name: &'a str,
title: &'a str,
}
impl Default for IndexPage {
impl<'a> Default for IndexPage<'a> {
fn default() -> Self {
IndexPage {
name: "mCaptcha".into(),
title: "Login".into(),
name: "mCaptcha",
title: "Login",
}
}
}
impl IndexPage {
pub fn run(&self) -> Result<String, &'static str> {
let index = self.clone().render_once().unwrap();
Ok(index)
}
}
#[get("/")]
#[get("/login")]
pub async fn login() -> impl Responder {
let body = IndexPage::default().run().unwrap();
let body = IndexPage::default().render_once().unwrap();
HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(body)

View File

@@ -20,31 +20,23 @@ use sailfish::TemplateOnce;
#[derive(TemplateOnce, Clone)]
#[template(path = "auth/register/index.html")]
pub struct IndexPage {
pub name: String,
pub title: String,
struct IndexPage<'a> {
name: &'a str,
title: &'a str,
}
impl Default for IndexPage {
impl<'a> Default for IndexPage<'a> {
fn default() -> Self {
IndexPage {
name: "mCaptcha".into(),
title: "Join".into(),
name: "mCaptcha",
title: "Join",
}
}
}
impl IndexPage {
pub fn run(&self) -> Result<String, &'static str> {
let index = self.clone().render_once().unwrap();
Ok(index)
}
}
#[get("/join")]
pub async fn join() -> impl Responder {
let body = IndexPage::default().run().unwrap();
let body = IndexPage::default().render_once().unwrap();
HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(body)

View File

@@ -23,7 +23,10 @@ mod panel;
pub fn services(cfg: &mut ServiceConfig) {
cfg.service(auth::login::login);
cfg.service(auth::register::join);
// panel
cfg.service(panel::panel);
cfg.service(panel::sitekey::add_sitekey);
}
//#[cfg(not(tarpaulin_include))]

View File

@@ -15,37 +15,42 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use actix_identity::Identity;
use actix_web::http::header;
use actix_web::{get, HttpResponse, Responder};
use sailfish::TemplateOnce;
use crate::api::v1::auth::is_authenticated;
pub mod sitekey;
#[derive(TemplateOnce, Clone)]
#[template(path = "panel/index.html")]
pub struct IndexPage {
pub name: String,
pub title: String,
pub struct IndexPage<'a> {
pub name: &'a str,
pub title: &'a str,
}
const TITLE: &str = "Dashboard";
impl Default for IndexPage {
impl<'a> Default for IndexPage<'a> {
fn default() -> Self {
IndexPage {
name: "mCaptcha".into(),
title: "Home".into(),
name: "mCaptcha",
title: TITLE,
}
}
}
impl IndexPage {
pub fn run(&self) -> Result<String, &'static str> {
let index = self.clone().render_once().unwrap();
Ok(index)
#[get("/")]
pub async fn panel(id: Identity) -> impl Responder {
if is_authenticated(&id).is_err() {
return HttpResponse::TemporaryRedirect()
.set_header(header::LOCATION, "/login")
.body("");
}
}
#[get("/panel")]
pub async fn panel() -> impl Responder {
let body = IndexPage::default().run().unwrap();
let body = IndexPage::default().render_once().unwrap();
HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(body)

View File

@@ -0,0 +1,45 @@
/*
* Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* 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/>.
*/
use actix_web::{get, HttpResponse, Responder};
use sailfish::TemplateOnce;
#[derive(TemplateOnce, Clone)]
#[template(path = "panel/add-site-key/index.html")]
pub struct IndexPage<'a> {
pub name: &'a str,
pub title: &'a str,
}
const TITLE: &str = "Add Site Key";
impl<'a> Default for IndexPage<'a> {
fn default() -> Self {
IndexPage {
name: "mCaptcha",
title: TITLE,
}
}
}
#[get("/sitekey/add")]
pub async fn add_sitekey() -> impl Responder {
let body = IndexPage::default().render_once().unwrap();
HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(body)
}