Skip to content

Commit

Permalink
Add twitch oauth routes to router
Browse files Browse the repository at this point in the history
  • Loading branch information
sneakycrow committed Dec 1, 2024
1 parent a3c38f2 commit 906859f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
16 changes: 15 additions & 1 deletion services/silo-api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,21 @@ async fn main() {
"/auth",
Router::new()
.route("/register", post(routes::auth::register))
.route("/login", post(routes::auth::login)),
.route("/login", post(routes::auth::login))
.nest(
"/oauth/twitch",
Router::new()
.route("/", get(routes::auth::oauth::twitch::oauth_redirect))
.route(
"/callback",
get(routes::auth::oauth::twitch::oauth_callback).layer(
axum_mw::from_fn_with_state(
state.clone(),
middleware::auth::auth_middleware,
),
),
),
),
)
.nest(
"/user",
Expand Down
17 changes: 5 additions & 12 deletions services/silo-api/src/routes/auth/oauth/twitch.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use axum::{
extract::{Query, State},
http::{Response, StatusCode},
response::Redirect,
Extension,
};
use chrono::{Duration, Utc};
Expand Down Expand Up @@ -141,13 +142,9 @@ impl TwitchCredentials {
}
}

pub async fn oauth_redirect() -> Result<Response<()>, StatusCode> {
pub async fn oauth_redirect() -> Result<Redirect, StatusCode> {
match TwitchCredentials::from_env() {
Ok(creds) => Ok(Response::builder()
.status(StatusCode::FOUND)
.header("Location", creds.generate_oauth_url())
.body(())
.unwrap()),
Ok(creds) => Ok(Redirect::to(&creds.generate_oauth_url())),
Err(_e) => {
tracing::error!("Failed to initialize Twitch OAuth: {}", _e);
Err(StatusCode::INTERNAL_SERVER_ERROR)
Expand All @@ -159,7 +156,7 @@ pub async fn oauth_callback(
State(state): State<Arc<AppState>>,
Extension(current_user): Extension<Option<User>>,
Query(params): Query<TwitchCallback>,
) -> Result<Response<()>, StatusCode> {
) -> Result<Redirect, StatusCode> {
// Initialize Twitch credentials
let credentials = match TwitchCredentials::from_env() {
Ok(creds) => creds,
Expand Down Expand Up @@ -261,9 +258,5 @@ pub async fn oauth_callback(
}

// Redirect to dashboard
Ok(Response::builder()
.status(StatusCode::FOUND)
.header("Location", "/dashboard")
.body(())
.unwrap())
Ok(Redirect::to("/dashboard"))
}

0 comments on commit 906859f

Please sign in to comment.