Login
4 branches 0 tags
Ben (U939/Arch Linux) Some styling / nicer form validation d580b41 1 month ago 23 Commits
rubhub / src / auth.rs
use axum::{
    extract::{Form, Path, State},
    http::StatusCode,
    response::{Html, Redirect, Response},
};
use tower_cookies::Cookies;

use crate::{
    services::{
        project::{self, NewProjectForm},
        session,
        user::{self, LoginForm, SettingsForm},
    },
    state::GlobalState,
};

pub async fn login_page(cookies: Cookies) -> Html<String> {
    user::login_page(cookies).await
}

pub async fn handle_login(
    State(state): State<GlobalState>,
    cookies: Cookies,
    Form(form): Form<LoginForm>,
) -> Result<Response, (StatusCode, Html<String>)> {
    user::handle_login(&state, cookies, form).await
}

pub async fn logout(
    State(state): State<GlobalState>,
    cookies: Cookies,
) -> Result<Redirect, (StatusCode, Html<String>)> {
    Ok(session::logout(&state, cookies).await)
}

pub async fn settings_page(
    State(state): State<GlobalState>,
    cookies: Cookies,
) -> Result<Html<String>, Redirect> {
    user::settings_page(&state, cookies).await
}

pub async fn handle_settings(
    State(state): State<GlobalState>,
    cookies: Cookies,
    Form(form): Form<SettingsForm>,
) -> Result<Response, (StatusCode, Html<String>)> {
    user::handle_settings(&state, cookies, form).await
}

pub async fn projects_page(
    State(state): State<GlobalState>,
    cookies: Cookies,
    Path(username): Path<String>,
) -> Result<Html<String>, (StatusCode, Html<String>)> {
    project::projects_page(&state, cookies, username).await
}

pub async fn new_project_page(
    State(state): State<GlobalState>,
    cookies: Cookies,
) -> Result<Html<String>, Redirect> {
    project::new_project_page(&state, cookies).await
}

pub async fn handle_new_project(
    State(state): State<GlobalState>,
    cookies: Cookies,
    Form(form): Form<NewProjectForm>,
) -> Result<Response, Redirect> {
    project::handle_new_project(&state, cookies, form).await
}

pub async fn project_page(
    State(state): State<GlobalState>,
    cookies: Cookies,
    Path((username, slug)): Path<(String, String)>,
) -> Result<Html<String>, (StatusCode, Html<String>)> {
    project::project_page(&state, cookies, username, slug).await
}

pub async fn project_settings_page(
    State(state): State<GlobalState>,
    cookies: Cookies,
    Path((username, slug)): Path<(String, String)>,
) -> Result<Html<String>, Redirect> {
    project::project_settings_page(&state, cookies, username, slug).await
}

pub async fn handle_project_settings(
    State(state): State<GlobalState>,
    cookies: Cookies,
    Path((username, slug)): Path<(String, String)>,
    Form(form): Form<project::ProjectSettingsForm>,
) -> Result<Response, Redirect> {
    project::handle_project_settings(&state, cookies, username, slug, form).await
}