- initialize survey user

- skip survey user registration if cookie is already present
This commit is contained in:
Aravinth Manivannan 2021-10-14 20:39:39 +05:30
parent c901aaa922
commit 35937134f2
Signed by: realaravinth
GPG Key ID: AD9F0F08E855ED88
4 changed files with 52 additions and 9 deletions

View File

@ -129,8 +129,20 @@ async fn register(
session: Session,
path: web::Query<RedirectQuery>,
) -> ServiceResult<HttpResponse> {
let uuid = runners::register_runner(&data).await?;
session.insert(SURVEY_USER_ID, uuid.to_string()).unwrap();
let is_authenticated = || {
if let Ok(Some(_)) = session.get::<String>(SURVEY_USER_ID) {
log::info!("user already registered");
true
} else {
false
}
};
if !is_authenticated() {
let uuid = runners::register_runner(&data).await?;
session.insert(SURVEY_USER_ID, uuid.to_string()).unwrap();
session.get::<String>(SURVEY_USER_ID).unwrap().unwrap();
}
let path = path.into_inner();
if let Some(redirect_to) = path.redirect_to {
Ok(HttpResponse::Found()

View File

@ -109,8 +109,8 @@ async fn main() -> std::io::Result<()> {
actix_middleware::DefaultHeaders::new()
.header("Permissions-Policy", "interest-cohort=()"),
)
.wrap(get_identity_service())
.wrap(get_survey_session())
.wrap(get_identity_service())
.wrap(actix_middleware::NormalizePath::new(
actix_middleware::TrailingSlash::Trim,
))
@ -134,12 +134,13 @@ pub fn get_json_err() -> JsonConfig {
#[cfg(not(tarpaulin_include))]
pub fn get_survey_session() -> actix_session::CookieSession {
let cookie_secret = &SETTINGS.server.cookie_secret2;
actix_session::CookieSession::private(cookie_secret.as_bytes())
actix_session::CookieSession::signed(cookie_secret.as_bytes())
.lazy(true)
.domain(&SETTINGS.server.domain)
.name("survey-id")
.path("/survey")
.http_only(true)
.path("/")
.max_age(30 * 60)
.domain(&SETTINGS.server.domain)
.secure(false)
}

View File

@ -0,0 +1,22 @@
/*
* 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/>.
*/
const ROUTES = {
register: "/survey/api/v1/benches/register",
};
export default ROUTES;

View File

@ -15,10 +15,15 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { Perf } from "./types";
import ROUTES from "../api/v1/routes";
export const index = () => {
console.log("running bench init");
const FACTOR = 500000;
const initSession = async () => {
fetch(ROUTES.register);
};
const worker = new Worker("/bench.js");
const res: Array<Perf> = [];
const stats = document.getElementById("stats");
@ -71,8 +76,9 @@ export const index = () => {
s.innerHTML = "Benchmark finished";
};
const run = (e: Event) => {
const run = async (e: Event) => {
e.preventDefault();
await initSession();
document.getElementById("pre-bench").style.display = "none";
document.getElementById("bench").style.display = "flex";
@ -100,5 +106,7 @@ export const index = () => {
addDeviceInfo();
};
document.getElementById("start").addEventListener("click", (e) => run(e));
document
.getElementById("start")
.addEventListener("click", async (e) => await run(e));
};