31
31
}
32
32
</ style >
33
33
< script >
34
- function registerUser ( ) {
34
+ async function registerUser ( ) {
35
35
const username = document . getElementById ( "username" ) . value . trim ( ) ;
36
36
const messageBox = document . getElementById ( "message" ) ;
37
37
42
42
43
43
// Base64 encode the redirect URL where Scratch Auth will send the response
44
44
const redirectLocation = btoa ( window . location . href ) ; // Redirect back to the current page after authentication
45
-
46
- // Add specific parameters to the URL to prioritize the "Cloud data" method
47
- const authUrl = `https://auth.itinerary.eu.org/auth/?redirect=${ redirectLocation } &name=Scratch Auth Tutorial&sign_in_method=cloud` ;
48
45
49
- // Display the instructions to the user
50
- messageBox . style . color = "green" ;
51
- messageBox . textContent = `Redirecting to Scratch Auth... Follow the steps there.` ;
46
+ // Request to get the token from Scratch Auth's backend
47
+ const authUrl = `https://auth-api.itinerary.eu.org/auth/getTokens?redirect=${ redirectLocation } &method=cloud&username=${ username } ` ;
52
48
53
- // Redirect the user to Scratch Auth after a brief message
54
- setTimeout ( ( ) => {
55
- window . location . href = authUrl ;
56
- } , 2000 ) ; // Delay the redirect for 2 seconds
49
+ try {
50
+ const response = await fetch ( authUrl ) ;
51
+ const data = await response . json ( ) ;
52
+
53
+ if ( data . token ) {
54
+ // Display the message and redirect user to Scratch Auth
55
+ messageBox . style . color = "green" ;
56
+ messageBox . textContent = `Redirecting to Scratch Auth... Follow the steps there.` ;
57
+
58
+ // Redirect to Scratch Auth after a brief delay
59
+ setTimeout ( ( ) => {
60
+ window . location . href = `https://auth.itinerary.eu.org/auth/?redirect=${ redirectLocation } &token=${ data . token } ` ;
61
+ } , 2000 ) ; // Delay of 2 seconds before redirect
62
+ } else {
63
+ messageBox . style . color = "red" ;
64
+ messageBox . textContent = "Authentication failed. Please try again." ;
65
+ }
66
+ } catch ( error ) {
67
+ messageBox . style . color = "red" ;
68
+ messageBox . textContent = "Error connecting to the authentication service. Please try again later." ;
69
+ }
57
70
}
58
71
</ script >
59
72
</ head >
@@ -68,10 +81,10 @@ <h2>Welcome! Please log in to continue.</h2>
68
81
< p id ="message " class ="message "> </ p >
69
82
< p >
70
83
Please note: You will be redirected to an external site (Scratch Auth) for authentication. Once there,
71
- choose the "Cloud Data" option for the quickest sign-in method .
84
+ follow the on-screen instructions to authenticate .
72
85
</ p >
73
86
< p >
74
- If you are unable to use the "Cloud Data" option, other sign-in methods are available on Scratch Auth's page .
87
+ If you face issues, try changing your login method .
75
88
</ p >
76
89
</ div >
77
90
</ body >
0 commit comments