diff --git a/backend/Cargo.toml b/backend/Cargo.toml index 6444b9d..42e299d 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -21,6 +21,7 @@ actix-web = "3.3.2" actix-files = "0.5.0" actix-web-actors = "3.0.0" urlencoding = "2.1.0" +serde = "1.0" serde_json = "1.0" rand = "0.8" diff --git a/backend/src/main.rs b/backend/src/main.rs index e332564..83b975d 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -3,13 +3,18 @@ use actix_session::CookieSession; use actix_web::http::{ContentEncoding, StatusCode}; use actix_web::*; -use femme; use log::info; use rand::Rng; mod user; mod ws; +// pub struct AppState ; + +// impl Actor for AppState { +// type Context = actix::Context; +// } + const STATIC_DIR: &str = "./static/"; const PAGE_INDEX: &str = "./static/index.html"; const PAGE_NOT_FOUND: &str = "./static/p404.html"; @@ -38,6 +43,7 @@ async fn main() -> std::io::Result<()> { let private_key = rand::thread_rng().gen::<[u8; 32]>(); HttpServer::new(move || { App::new() + // .data(AppState) .wrap(CookieSession::signed(&private_key).secure(false)) .wrap(middleware::Compress::new(ContentEncoding::Gzip)) .service(index) diff --git a/backend/src/user/auth.rs b/backend/src/user/auth.rs index 25630ce..a471af6 100644 --- a/backend/src/user/auth.rs +++ b/backend/src/user/auth.rs @@ -1,12 +1,63 @@ +use actix::{Actor, Context, Handler, Message, MessageResponse}; use actix_web::*; +use serde::{Deserialize, Serialize}; use serde_json::json; use log::info; -#[post("/auth")] -pub async fn auth(req: HttpRequest) -> Result { - info!("{:?}", req); - Ok(HttpResponse::Ok() - .content_type("application/json") - .body(json!({"status": "success"}))) +#[derive(Message)] +#[rtype(result = "Self")] +#[derive(MessageResponse)] +#[allow(dead_code)] +enum AuthMessage { + DoAuth, + AuthSuccess, + AuthFailure, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct AuthInfo { + username: String, + password: String, +} + +impl Actor for AuthInfo { + type Context = Context; + + fn started(&mut self, _ctx: &mut Self::Context) { + info!("AuthInfo started"); + info!("{:?}", self); + } + + fn stopped(&mut self, _ctx: &mut Self::Context) { + info!("AuthInfo stopped"); + } +} + +impl Handler for AuthInfo { + type Result = AuthMessage; + + fn handle(&mut self, _msg: AuthMessage, _ctx: &mut Context) -> Self::Result { + info!("AuthInfo handle"); + AuthMessage::AuthSuccess + } +} + +#[post("/auth")] +pub async fn auth(params: web::Json) -> Result { + let auth = params.into_inner(); + let auth_addr = auth.start(); + let res = auth_addr.send(AuthMessage::DoAuth).await; + + match res { + Ok(AuthMessage::AuthSuccess) => Ok(HttpResponse::Ok().json(json!({ + "status": "success", + }))), + Ok(AuthMessage::AuthFailure) => Ok(HttpResponse::Ok().json(json!({ + "status": "failure", + }))), + _ => Ok(HttpResponse::Ok().json(json!({ + "status": "failure", + }))), + } } diff --git a/frontend/src/app.rs b/frontend/src/app.rs index 997184e..ec27cb6 100644 --- a/frontend/src/app.rs +++ b/frontend/src/app.rs @@ -1,10 +1,10 @@ use std::borrow::Cow; +use crate::components::auth; use crate::pages::{page_home::PageHome, page_not_found::PageNotFound, page_ssh::PageSsh}; use yew::html::IntoPropValue; use yew::prelude::*; use yew::services::ConsoleService; -use yew::virtual_dom::VNode; use yew::Component; use yew_router::prelude::*; use yew_router::{router::Router, Switch}; @@ -21,11 +21,11 @@ enum AppRoute { NotFound, } -impl Into<&str> for AppRoute { - fn into(self) -> &'static str { - match self { - AppRoute::Ssh => &"/ssh", - _ => &"/", +impl From for &str { + fn from(route: AppRoute) -> Self { + match route { + AppRoute::Ssh => "/ssh", + _ => "/", } } } @@ -36,19 +36,30 @@ impl IntoPropValue>> for AppRoute { } } -pub struct App {} +pub struct App { + authdone: bool, + link: ComponentLink, +} -pub enum Msg {} +pub enum AppMsg { + AuthDone, +} impl Component for App { - type Message = Msg; + type Message = AppMsg; type Properties = (); - fn create(_: Self::Properties, _link: ComponentLink) -> Self { - Self {} + fn create(_: Self::Properties, link: ComponentLink) -> Self { + Self { + authdone: false, + link, + } } - fn update(&mut self, _msg: Self::Message) -> ShouldRender { + fn update(&mut self, msg: Self::Message) -> ShouldRender { + match msg { + AppMsg::AuthDone => self.authdone = true, + } true } @@ -56,19 +67,35 @@ impl Component for App { false } - fn view(&self) -> VNode { + fn view(&self) -> Html { html! { <> - { self.view_nav() } -
- - render = Router::render(Self::switch) - redirect=Router::redirect(|route: Route| { - ConsoleService::log(&format!("{:?}", route)); - AppRoute::NotFound - }) - /> -
+ { + + if self.authdone { + html! { + <> + {self.view_nav()} + +
+ + render = Router::render(Self::switch) + redirect=Router::redirect(|route: Route| { + ConsoleService::log(&format!("{:?}", route)); + AppRoute::NotFound + }) + /> +
+ + } + } + else { + let onauthdone = &self.link.callback(|_| AppMsg::AuthDone); + html!{ + + } + } + }