flabk/src/servek/mod.rs

88 lines
1.6 KiB
Rust

use axum::response::{Html, IntoResponse};
use serde::{Deserialize, Serialize};
use tower_cookies::{Cookie, Cookies};
use crate::svc::{
auth::{Auth, AuthError, Claims},
profiles::User,
};
mod html;
pub mod servek;
#[derive(Debug, Clone, Serialize)]
struct Notification {
pub message: String,
pub tag_name: String,
}
#[derive(Debug, Clone, Deserialize)]
pub struct LoginRequest {
pub username: String,
pub password: String,
}
#[derive(Debug, Clone, Deserialize)]
pub struct CreateProfileRequest {
pub username: String,
pub password: String,
pub email: String,
}
// pub enum HtmlOrRedirect {
// Html(Html<String>),
// Redirect(()),
// }
#[derive(Debug, Clone, Serialize)]
pub struct Redirect {
pub location: String,
}
#[derive(Debug, Clone, Serialize)]
pub enum NavType {
LoggedIn,
LoggedOut,
}
impl From<Result<Claims, AuthError>> for NavType {
fn from(claims: Result<Claims, AuthError>) -> Self {
if claims.map(|c| c.expired()).unwrap_or(true) {
NavType::LoggedOut
} else {
NavType::LoggedIn
}
}
}
impl From<Claims> for NavType {
fn from(c: Claims) -> Self {
if c.expired() {
NavType::LoggedOut
} else {
NavType::LoggedIn
}
}
}
#[derive(Debug, Clone, Serialize)]
pub struct WithNav<T> {
pub nav_type: NavType,
pub obj: T,
}
impl<T> WithNav<T> {
pub fn new(obj: T, nav_type: NavType) -> WithNav<T> {
WithNav { nav_type, obj }
}
}
impl<T> Into<serde_json::Value> for WithNav<T>
where
T: Serialize,
{
fn into(self) -> serde_json::Value {
serde_json::json!(self)
}
}