@@ -20,76 +20,87 @@ document.addEventListener("DOMContentLoaded", () => {
2020 } ) ;
2121
2222 // GitHub Authentication Setup
23- const GITHUB_CLIENT_ID = "your_github_client_id " ;
23+ const GITHUB_CLIENT_ID = "Ov23liz64bw3989HVcKK " ;
2424 const BACKEND_URL = "http://localhost:3000" ;
2525
26- // Handle GitHub OAuth
26+ // GitHub auth handler
2727 const handleGitHubAuth = ( ) => {
28- const params = window . location . search ;
29- const urlParams = new URLSearchParams ( params ) ;
30- const code = urlParams . get ( "code" ) ;
31-
28+ const params = new URLSearchParams ( window . location . search ) ;
29+ const code = params . get ( "code" ) ;
30+
3231 if ( code ) {
33- localStorage . setItem ( " code", code ) ;
34- getGhUser ( ) ;
32+ console . log ( "OAuth code received: ", code ) ;
33+ getGhUser ( code ) ;
3534 }
3635 } ;
3736
38- // Get GitHub User Data
39- const getGhUser = ( ) => {
40- let code = localStorage . getItem ( "code" ) ;
41- if ( ! code || code === "null" ) return ;
42-
37+ // Get GitHub user data
38+ const getGhUser = ( code ) => {
39+ console . log ( "Starting GitHub user fetch with code:" , code ) ;
40+
4341 fetch ( `${ BACKEND_URL } /api/auth/github?code=${ code } ` )
44- . then ( ( res ) => res . json ( ) )
45- . then ( ( response ) => {
46- let resData = response . data ;
47- let token = new URLSearchParams ( resData ) . get ( "access_token" ) ;
42+ . then ( res => {
43+ console . log ( "Auth response status:" , res . status ) ;
44+ if ( ! res . ok ) {
45+ throw new Error ( `HTTP error! status: ${ res . status } ` ) ;
46+ }
47+ return res . json ( ) ;
48+ } )
49+ . then ( response => {
50+ console . log ( "Auth response data:" , response ) ;
51+ if ( ! response . data || ! response . data . access_token ) {
52+ throw new Error ( 'No access token received' ) ;
53+ }
54+
55+ const token = response . data . access_token ;
56+ console . log ( "Received access token, fetching user data" ) ;
4857
4958 return fetch ( `${ BACKEND_URL } /api/auth/github/getUser` , {
5059 headers : {
51- Authorization : " Bearer " + token ,
52- } ,
60+ ' Authorization' : ` Bearer ${ token } `
61+ }
5362 } ) ;
5463 } )
55- . then ( ( res ) => res . json ( ) )
56- . then ( ( response ) => {
64+ . then ( res => {
65+ console . log ( "User data response status:" , res . status ) ;
66+ if ( ! res . ok ) {
67+ throw new Error ( `HTTP error! status: ${ res . status } ` ) ;
68+ }
69+ return res . json ( ) ;
70+ } )
71+ . then ( response => {
72+ console . log ( "User data response:" , response ) ;
73+ if ( ! response . user ) {
74+ throw new Error ( 'No user data received' ) ;
75+ }
76+
5777 const { name, email } = response . user ;
58- localStorage . setItem (
59- "user-info" ,
60- JSON . stringify ( {
61- name : name ,
62- email : email ,
63- } )
64- ) ;
65- localStorage . removeItem ( "code" ) ;
78+ localStorage . setItem ( 'user-info' , JSON . stringify ( { name, email } ) ) ;
79+
80+ // Clean up URL
81+ const cleanUrl = window . location . href . split ( '?' ) [ 0 ] ;
82+ window . history . replaceState ( { } , '' , cleanUrl ) ;
83+
84+ // Redirect to home page
6685 window . location . href = "/" ;
6786 } )
68- . catch ( ( error ) => {
69- console . error ( "GitHub auth error:" , error ) ;
87+ . catch ( error => {
88+ console . error ( "Authentication error:" , error ) ;
89+ alert ( "Failed to authenticate with GitHub. Please try again." ) ;
7090 } ) ;
7191 } ;
7292
73- // Handle initial auth check
74- if ( localStorage . getItem ( "user-info" ) ) {
75- window . location . href = "/" ;
76- } else if ( localStorage . getItem ( "code" ) ) {
77- getGhUser ( ) ;
78- }
79-
8093 // GitHub button click handlers
81- github_signIn . onclick = ( ) => {
82- window . location . assign (
83- `https://github.com/login/oauth/authorize?client_id=${ GITHUB_CLIENT_ID } `
84- ) ;
85- } ;
86-
87- github_login . onclick = ( ) => {
88- window . location . assign (
89- `https://github.com/login/oauth/authorize?client_id=${ GITHUB_CLIENT_ID } `
90- ) ;
94+ const initiateGitHubAuth = ( ) => {
95+ const authUrl = new URL ( 'https://github.com/login/oauth/authorize' ) ;
96+ authUrl . searchParams . append ( 'client_id' , GITHUB_CLIENT_ID ) ;
97+ authUrl . searchParams . append ( 'scope' , 'user' ) ;
98+ window . location . href = authUrl . toString ( ) ;
9199 } ;
92100
101+ github_signIn . onclick = initiateGitHubAuth ;
102+ github_login . onclick = initiateGitHubAuth ;
103+
93104 // Email validation function
94105 const isValidEmail = ( email ) => {
95106 const emailRegex = / ^ [ a - z A - Z 0 - 9 . _ % + - ] + @ [ a - z A - Z 0 - 9 . - ] + \. [ a - z A - Z ] { 2 , } $ / ;
@@ -125,7 +136,7 @@ document.addEventListener("DOMContentLoaded", () => {
125136 localStorage . removeItem ( "rememberedUsername" ) ;
126137 }
127138 alert ( "Login successful!" ) ;
128- window . location . href = "index.html " ;
139+ window . location . href = "/ " ;
129140 } else {
130141 alert ( "Invalid username or password" ) ;
131142 }
@@ -161,7 +172,7 @@ document.addEventListener("DOMContentLoaded", () => {
161172 localStorage . setItem ( "isLoggedIn" , "true" ) ;
162173
163174 alert ( "Signup successful!" ) ;
164- window . location . href = "index.html " ;
175+ window . location . href = "/ " ;
165176 } ) ;
166177
167178 // Password visibility toggle
@@ -188,4 +199,9 @@ document.addEventListener("DOMContentLoaded", () => {
188199 usernameInput . value = localStorage . getItem ( "rememberedUsername" ) ;
189200 rememberMeCheckbox . checked = true ;
190201 }
202+
203+ // Handle GitHub auth on page load
204+ if ( window . location . search . includes ( "code" ) ) {
205+ handleGitHubAuth ( ) ;
206+ }
191207} ) ;
0 commit comments