11class HackclubAuthController < ApplicationController
2- require ' http'
2+ require " http"
33
4- AUTH_URL = ' https://auth.hackclub.com/oauth/authorize'
5- TOKEN_URL = ' https://auth.hackclub.com/oauth/token'
6- API_URL = ' https://auth.hackclub.com/api/v1/me'
7- REDIRECT_URI = ' http://localhost:3000/oauth/callback'
4+ AUTH_URL = " https://auth.hackclub.com/oauth/authorize"
5+ TOKEN_URL = " https://auth.hackclub.com/oauth/token"
6+ API_URL = " https://auth.hackclub.com/api/v1/me"
7+ REDIRECT_URI = " http://localhost:3000/oauth/callback"
88
99 def authorize
1010 state = SecureRandom . urlsafe_base64 ( 32 )
@@ -25,50 +25,50 @@ def callback
2525 error = params [ :error ]
2626
2727 if error
28- redirect_to root_path , alert : ' Authentication failed or was cancelled.'
28+ redirect_to root_path , alert : " Authentication failed or was cancelled."
2929 return
3030 end
3131
3232 if state != session [ :oauth_state ]
33- redirect_to root_path , alert : ' Invalid OAuth state.'
33+ redirect_to root_path , alert : " Invalid OAuth state."
3434 return
3535 end
3636
3737 # Exchange code for access token
3838 token_response = HTTP . post ( TOKEN_URL , form : {
39- client_id : ENV . fetch ( ' HACKCLUB_CLIENT_ID' ) ,
40- client_secret : ENV . fetch ( ' HACKCLUB_CLIENT_SECRET' ) ,
39+ client_id : ENV . fetch ( " HACKCLUB_CLIENT_ID" ) ,
40+ client_secret : ENV . fetch ( " HACKCLUB_CLIENT_SECRET" ) ,
4141 redirect_uri : REDIRECT_URI ,
4242 code : code ,
43- grant_type : ' authorization_code'
43+ grant_type : " authorization_code"
4444 } )
4545
4646 unless token_response . status == 200
47- redirect_to root_path , alert : ' Failed to exchange authorization code.'
47+ redirect_to root_path , alert : " Failed to exchange authorization code."
4848 return
4949 end
5050
5151 token_data = JSON . parse ( token_response . body )
52- access_token = token_data [ ' access_token' ]
52+ access_token = token_data [ " access_token" ]
5353
5454 # Get user info from Hack Club API
5555 user_response = HTTP . auth ( "Bearer #{ access_token } " ) . get ( API_URL )
5656
5757 unless user_response . status == 200
58- redirect_to root_path , alert : ' Failed to fetch user info.'
58+ redirect_to root_path , alert : " Failed to fetch user info."
5959 return
6060 end
6161
6262 user_info = JSON . parse ( user_response . body )
6363
64- identity = user_info . dig ( ' identity' )
65- email = identity &.dig ( ' primary_email' )
66- first_name = identity &.dig ( ' first_name' )
67- last_name = identity &.dig ( ' last_name' )
68- slack_id = identity &.dig ( ' slack_id' )
64+ identity = user_info . dig ( " identity" )
65+ email = identity &.dig ( " primary_email" )
66+ first_name = identity &.dig ( " first_name" )
67+ last_name = identity &.dig ( " last_name" )
68+ slack_id = identity &.dig ( " slack_id" )
6969
7070 if email . blank?
71- redirect_to root_path , alert : ' No email found in Hack Club profile.'
71+ redirect_to root_path , alert : " No email found in Hack Club profile."
7272 return
7373 end
7474
@@ -79,8 +79,6 @@ def callback
7979
8080 session [ :user_id ] = user . id
8181
82- redirect_to dashboard_path , notice : ' Welcome back, Adventurer!'
82+ redirect_to dashboard_path , notice : " Welcome back, Adventurer!"
8383 end
8484end
85-
86-
0 commit comments