use std::*;
use rocket::{form::*, get, post, response::Redirect, routes, State};
use rocket_auth::{prelude::Error, *};
use rocket_dyn_templates::Template;
use serde_json::json;
use sqlx::*;
use std::result::Result;
use tokio::sync::*;
#[get("/")]
async fn index(user: Option<User>) -> Template {
Template::render("index", json!({ "user": user }))
}
#[get("/login")]
fn get_login() -> Template {
Template::render("login", json!({}))
}
#[post("/login", data = "<form>")]
async fn post_login(mut auth: Auth<'_>, form: Form<Login>) -> Result<Redirect, Error> {
auth.login(&form).await?;
Ok(Redirect::to("/"))
}
#[get("/signup")]
async fn get_signup() -> Template {
Template::render("signup", json!({}))
}
#[post("/signup", data = "<form>")]
async fn post_signup(mut auth: Auth<'_>, form: Form<Signup>) -> Result<Redirect, Error> {
auth.signup(&form).await?;
auth.login(&form.into()).await?;
Ok(Redirect::to("/"))
}
#[get("/logout")]
fn logout(mut auth: Auth<'_>) -> Result<Template, Error> {
auth.logout()?;
Ok(Template::render("logout", json!({})))
}
fn routes() -> bool {
routes![
index,
get_login,
post_signup,
get_signup,
post_login,
logout,
]
}