Add profile route
Reorder routes and layers to protect required routes
This commit is contained in:
parent
f5e3dd644f
commit
4269d49ddf
|
|
@ -1 +1,2 @@
|
||||||
backend/target
|
backend/target
|
||||||
|
backend/db
|
||||||
|
|
|
||||||
|
|
@ -8,14 +8,16 @@ use sqlx::{sqlite::SqlitePoolOptions, SqlitePool};
|
||||||
use tower_http::services::ServeDir;
|
use tower_http::services::ServeDir;
|
||||||
|
|
||||||
mod error_handling;
|
mod error_handling;
|
||||||
use middlewares::{check_auth, inject_user_data};
|
|
||||||
mod google_oauth;
|
mod google_oauth;
|
||||||
mod middlewares;
|
mod middlewares;
|
||||||
mod oauth;
|
mod oauth;
|
||||||
|
mod routes;
|
||||||
|
|
||||||
use error_handling::AppError;
|
use error_handling::AppError;
|
||||||
use google_oauth::*;
|
use google_oauth::*;
|
||||||
|
use middlewares::{check_auth, inject_user_data};
|
||||||
use oauth::{login, logout, google_auth_return};
|
use oauth::{login, logout, google_auth_return};
|
||||||
|
use routes::*;
|
||||||
|
|
||||||
struct HtmlTemplate<T>(T);
|
struct HtmlTemplate<T>(T);
|
||||||
|
|
||||||
|
|
@ -76,12 +78,13 @@ async fn main() {
|
||||||
|
|
||||||
// build our application with some routes
|
// build our application with some routes
|
||||||
let app = Router::new()
|
let app = Router::new()
|
||||||
|
.route("/logout", get(logout))
|
||||||
|
.route("/profile", get(profile))
|
||||||
|
.route_layer(middleware::from_fn_with_state(app_state.clone(), check_auth))
|
||||||
.nest_service("/assets", ServeDir::new("templates/assets")
|
.nest_service("/assets", ServeDir::new("templates/assets")
|
||||||
.fallback(get_service(ServeDir::new("templates/assets"))))
|
.fallback(get_service(ServeDir::new("templates/assets"))))
|
||||||
.route_layer(middleware::from_fn_with_state(app_state.clone(), check_auth))
|
|
||||||
.route("/", get(index))
|
.route("/", get(index))
|
||||||
.route("/login", get(login))
|
.route("/login", get(login))
|
||||||
.route("/logout", get(logout))
|
|
||||||
.route("/google_auth", get(google_auth))
|
.route("/google_auth", get(google_auth))
|
||||||
.route("/google_auth_return", get(google_auth_return))
|
.route("/google_auth_return", get(google_auth_return))
|
||||||
.route_layer(middleware::from_fn_with_state(app_state.db_pool.clone(), inject_user_data))
|
.route_layer(middleware::from_fn_with_state(app_state.db_pool.clone(), inject_user_data))
|
||||||
|
|
@ -90,7 +93,7 @@ async fn main() {
|
||||||
;
|
;
|
||||||
|
|
||||||
// run it
|
// run it
|
||||||
let addr = SocketAddr::from(([127, 0, 0, 1], 40192));
|
let addr = SocketAddr::from(([0, 0, 0, 0], 40192));
|
||||||
tracing::debug!("listening on {}", addr);
|
tracing::debug!("listening on {}", addr);
|
||||||
axum_server::bind(addr)
|
axum_server::bind(addr)
|
||||||
.serve(app.into_make_service())
|
.serve(app.into_make_service())
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,24 @@
|
||||||
|
use askama::Template;
|
||||||
|
use axum::{response::IntoResponse, Extension};
|
||||||
|
use http::Request;
|
||||||
|
|
||||||
|
use crate::{HtmlTemplate, UserData};
|
||||||
|
|
||||||
|
#[derive(Template)]
|
||||||
|
#[template(path = "profile.html")]
|
||||||
|
struct ProfileTemplate {
|
||||||
|
logged_in: bool,
|
||||||
|
name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn profile<T>(
|
||||||
|
Extension(user_data): Extension<Option<UserData>>,
|
||||||
|
_request: Request<T>,
|
||||||
|
) -> impl IntoResponse {
|
||||||
|
let user_email = user_data.map(|s| s.user_email);
|
||||||
|
let logged_in = user_email.is_some();
|
||||||
|
let name = user_email.unwrap_or_default();
|
||||||
|
|
||||||
|
let template = ProfileTemplate { logged_in, name};
|
||||||
|
HtmlTemplate(template)
|
||||||
|
}
|
||||||
|
|
@ -2,5 +2,5 @@
|
||||||
{% block title %}User Profile{% endblock %}
|
{% block title %}User Profile{% endblock %}
|
||||||
{% block content %}
|
{% block content %}
|
||||||
This is your user profile page. <br />
|
This is your user profile page. <br />
|
||||||
Your email address: {{ user_email }}.
|
Your email address: {{ name }}.
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue