diff --git a/src/main.rs b/src/main.rs index f838393..1b0fb52 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,8 +3,8 @@ use async_session::{MemoryStore, Session, SessionStore as _}; use axum::{ async_trait, extract::{ - self, rejection::TypedHeaderRejectionReason, Extension, FromRequest, Path, Query, - RequestParts, TypedHeader, + self, rejection::TypedHeaderRejectionReason, Extension, FromRequest, Query, RequestParts, + TypedHeader, }, headers::Cookie, http::{ @@ -14,8 +14,8 @@ use axum::{ StatusCode, }, response::{Html, IntoResponse, Redirect, Response}, - routing::{get, post}, - Json, Router, + routing::get, + Router, }; use http::header; use oauth2::{ @@ -24,11 +24,20 @@ use oauth2::{ }; use serde::{Deserialize, Serialize}; use std::{env, net::SocketAddr}; -use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; use uuid::Uuid; const COOKIE_NAME: &str = "SESSION"; +// The user data we'll get back from Discord. +// https://discord.com/developers/docs/resources/user#user-object-user-structure +#[derive(Debug, Serialize, Deserialize)] +struct User { + id: String, + avatar: Option, + username: String, + discriminator: String, +} + #[tokio::main] async fn main() { // Set the RUST_LOG, if it hasn't been explicitly defined @@ -45,8 +54,7 @@ async fn main() { // build our application with a route let app = Router::new() // `GET /` goes to `root` - .route("/greet/:name", get(greet)) - .route("/", get(handler)) + .route("/", get(index)) .layer(Extension(store)) .layer(Extension(oauth_client)); @@ -60,25 +68,14 @@ async fn main() { .unwrap(); } -async fn handler(user_id: UserIdFromSession) -> impl IntoResponse { - let (headers, user_id, create_cookie) = match user_id { - UserIdFromSession::FoundUserId(user_id) => (HeaderMap::new(), user_id, false), - UserIdFromSession::CreatedFreshUserId(new_user) => { - let mut headers = HeaderMap::new(); - headers.insert(http::header::SET_COOKIE, new_user.cookie); - (headers, new_user.user_id, true) - } +// Session is optional +async fn index(user: Option) -> impl IntoResponse { + let name = match user { + Some(u) => u.username, + None => "You're not logged in.\nVisit `/auth/discord` to do so.".to_string(), }; - - tracing::debug!("handler: user_id={:?} send_headers={:?}", user_id, headers); - - ( - headers, - format!( - "user_id={:?} session_cookie_name={} create_new_session_cookie={}", - user_id, COOKIE_NAME, create_cookie - ), - ) + let template = IndexTemplate { name }; + HtmlTemplate(template) } struct FreshUserId { @@ -167,14 +164,9 @@ impl UserId { } } -async fn greet(extract::Path(name): extract::Path) -> impl IntoResponse { - let template = HelloTemplate { name }; - HtmlTemplate(template) -} - #[derive(Template)] #[template(path = "index.html")] -struct HelloTemplate { +struct IndexTemplate { name: String, } @@ -204,8 +196,8 @@ fn oauth_client() -> BasicClient { // "AUTH_URL" "https://discord.com/api/oauth2/authorize?response_type=code"; // "TOKEN_URL" "https://discord.com/api/oauth2/token"; - let client_id = env::var("CLIENT_ID").expect("Missing CLIENT_ID!"); - let client_secret = env::var("CLIENT_SECRET").expect("Missing CLIENT_SECRET!"); + let client_id = env::var("GOOGLE_CLIENT_ID").expect("Missing CLIENT_ID!"); + let client_secret = env::var("GOOGLE_CLIENT_SECRET").expect("Missing CLIENT_SECRET!"); let redirect_url = env::var("REDIRECT_URL") .unwrap_or_else(|_| "http://127.0.0.1:3000/auth/authorized".to_string()); @@ -225,27 +217,6 @@ fn oauth_client() -> BasicClient { .set_redirect_uri(RedirectUrl::new(redirect_url).unwrap()) } -// The user data we'll get back from Discord. -// https://discord.com/developers/docs/resources/user#user-object-user-structure -#[derive(Debug, Serialize, Deserialize)] -struct User { - id: String, - avatar: Option, - username: String, - discriminator: String, -} - -// Session is optional -async fn index(user: Option) -> impl IntoResponse { - match user { - Some(u) => format!( - "Hey {}! You're logged in!\nYou may now access `/protected`.\nLog out with `/logout`.", - u.username - ), - None => "You're not logged in.\nVisit `/auth/discord` to do so.".to_string(), - } -} - async fn discord_auth(Extension(client): Extension) -> impl IntoResponse { let (auth_url, _csrf_token) = client .authorize_url(CsrfToken::new_random)