63
63
}
64
64
</ style >
65
65
< script >
66
- const serverUrl = 'https://image-hoster.onrender.com' ;
67
-
66
+ // Function to redirect user to ScratchAuth for authentication
68
67
function registerScratchAuth ( ) {
69
68
const messageBox = document . getElementById ( "message" ) ;
70
- const redirectLocation = encodeURIComponent ( window . location . href ) ;
69
+ const redirectLocation = encodeURIComponent ( window . location . href ) ; // Redirect back to this page after login
70
+
71
+ // URL for ScratchAuth with redirect parameter
71
72
const authUrl = `https://auth.itinerary.eu.org/auth/?redirect=${ redirectLocation } &name=Coding%20Hut&sign_in_method=cloud` ;
72
73
73
74
messageBox . style . color = "green" ;
74
- messageBox . textContent = "Redirecting to Scratch Auth... Follow the steps there." ;
75
+ messageBox . textContent = "Redirecting to ScratchAuth... Follow the steps there." ;
76
+
77
+ setTimeout ( ( ) => {
78
+ window . location . href = authUrl ; // Redirect to ScratchAuth login page
79
+ } , 2000 ) ;
80
+ }
81
+
82
+ // Function to redirect user to ubbload.netlify.app for APIAuth login
83
+ function registerApiAuth ( ) {
84
+ const messageBox = document . getElementById ( "message" ) ;
85
+ const redirectLocation = encodeURIComponent ( window . location . href ) ; // Redirect back to this page after login
86
+
87
+ // URL for ubbload login, with redirect URL as a query parameter
88
+ const authUrl = `https://ubbload.netlify.app/login?redirect=${ redirectLocation } ` ;
89
+
90
+ messageBox . style . color = "green" ;
91
+ messageBox . textContent = "Redirecting to APIAuth login (ubbload)... Follow the steps there." ;
75
92
76
93
setTimeout ( ( ) => {
77
- window . location . href = authUrl ;
94
+ window . location . href = authUrl ; // Redirect to ubbload login page
78
95
} , 2000 ) ;
79
96
}
80
97
98
+ // Function to handle successful login (after redirect from ScratchAuth or ubbload)
81
99
function checkAuth ( ) {
82
100
const urlParams = new URLSearchParams ( window . location . search ) ;
83
101
const authSuccess = urlParams . get ( 'success' ) ;
84
- const username = urlParams . get ( 'username' ) ;
102
+ const username = urlParams . get ( 'username' ) ; // Get the username parameter from the URL
85
103
86
104
if ( username ) {
87
- localStorage . setItem ( 'username' , username ) ;
105
+ localStorage . setItem ( 'username' , username ) ; // Store the username in localStorage
106
+ localStorage . setItem ( 'loggedIn' , 'true' ) ; // Mark the user as logged in
107
+ window . location . href = 'index.html' ; // Redirect to the main dashboard or home page
88
108
}
89
-
109
+
110
+ // If success is true (from ScratchAuth)
90
111
if ( authSuccess === 'true' ) {
91
112
localStorage . setItem ( 'loggedIn' , 'true' ) ;
92
113
window . location . href = 'index.html' ;
93
114
}
94
115
}
95
116
96
- window . onload = checkAuth ;
117
+ window . onload = checkAuth ; // Call checkAuth when the page loads to handle login status
97
118
98
- if ( localStorage . getItem ( 'verifiedUser' ) ) {
99
- window . location . href = 'index.html' ;
100
- }
101
-
102
- async function login ( ) {
103
- let username = document . getElementById ( 'username' ) . value . trim ( ) . toLowerCase ( ) ;
104
- if ( ! username ) {
105
- alert ( 'Enter your Scratch username.' ) ;
106
- return ;
107
- }
108
-
109
- const response = await fetch ( `${ serverUrl } /login` , {
110
- method : 'POST' ,
111
- headers : { 'Content-Type' : 'application/json' } ,
112
- body : JSON . stringify ( { username } )
113
- } ) ;
114
-
115
- const result = await response . json ( ) ;
116
- if ( result . verified ) {
117
- localStorage . setItem ( 'verifiedUser' , username ) ;
118
- window . location . href = 'upload.html' ;
119
- } else {
120
- document . getElementById ( 'codeMessage' ) . textContent = result . message || 'Login failed. Please try again.' ;
121
- }
122
- }
123
-
124
- async function verifyCode ( ) {
125
- let username = document . getElementById ( 'username' ) . value . trim ( ) . toLowerCase ( ) ;
126
- if ( ! username ) {
127
- alert ( 'Enter your username first.' ) ;
128
- return ;
129
- }
130
-
131
- const response = await fetch ( `${ serverUrl } /verify` , {
132
- method : 'POST' ,
133
- headers : { 'Content-Type' : 'application/json' } ,
134
- body : JSON . stringify ( { username } )
135
- } ) ;
136
-
137
- const result = await response . json ( ) ;
138
- if ( result . verified ) {
139
- localStorage . setItem ( 'verifiedUser' , username ) ;
140
- window . location . href = 'index.html' ;
141
- } else {
142
- alert ( result . message || 'Verification failed. Please try again.' ) ;
143
- }
119
+ if ( localStorage . getItem ( 'loggedIn' ) === 'true' ) {
120
+ window . location . href = 'index.html' ; // Redirect to the dashboard if already logged in
144
121
}
145
122
</ script >
146
123
</ head >
@@ -154,23 +131,20 @@ <h2>Welcome! Please log in to continue.</h2>
154
131
< button onclick ="registerScratchAuth() "> Register With ScratchAuth</ button >
155
132
< p id ="message " class ="message " aria-live ="polite "> </ p >
156
133
< p >
157
- Please note: You will be redirected to an external site (Scratch Auth ) for authentication. Once there,
134
+ Please note: You will be redirected to an external site (ScratchAuth ) for authentication. Once there,
158
135
choose the "Cloud Data" option for the quickest sign-in method.
159
136
</ p >
160
137
< p >
161
138
If you are unable to use the "Cloud Data" option, other sign-in methods are available on Scratch Auth's page, for example, profile commenting.
162
139
</ p >
163
- < a href ="https://github.com/Scratch-Coding-Hut/Scratch-Coding-Hut.github.io/issues/new ">
164
- < button > Having trouble signing in? Report an issue</ button >
165
- </ a >
140
+
166
141
</ div >
167
142
168
143
< div class ="container ">
169
144
< h2 > Login Using APIAuth</ h2 >
170
- < input type ="text " id ="username " placeholder ="Enter your Scratch username " />
171
- < button onclick ="login() "> Login With APIAuth (Made by < a href ="https://scratch.mit.edu/users/kRxZy_kRxZy/ " target ="_blank " style ="color: red "> kRxZy_kRxZy</ a > )</ button >
172
- < p id ="codeMessage " class ="message "> </ p >
173
- < button onclick ="verifyCode() "> Verify Code</ button >
145
+ < button onclick ="registerApiAuth() "> Login With APIAuth (Made by < a href ="https://scratch.mit.edu/users/kRxZy_kRxZy/ " target ="_blank " style ="color: black "> kRxZy_kRxZy</ a > )</ button >
174
146
</ div >
147
+ < div class ="container "> < a href ="https://github.com/Scratch-Coding-Hut/Scratch-Coding-Hut.github.io/issues/new ">
148
+ < button > Having trouble signing in? Report an issue</ button > </ a > < div >
175
149
</ body >
176
150
</ html >
0 commit comments