53
53
}
54
54
</ style >
55
55
< script >
56
- function registerUser ( ) {
56
+ function registerScratchAuth ( ) {
57
57
const messageBox = document . getElementById ( "message" ) ;
58
58
59
59
const redirectLocation = encodeURIComponent ( window . location . href ) ;
@@ -90,7 +90,27 @@ <h1>Scratch Authentication</h1>
90
90
</ div >
91
91
< div class ="content ">
92
92
< h2 > Welcome! Please log in to continue.</ h2 >
93
- < button onclick ="registerUser() "> Register</ button >
93
+ < button onclick ="registerScratchAuth() "> Register With ScratchAuth (Currently not working.)</ button >
94
+
95
+ <!DOCTYPE html>
96
+ < html lang ="en ">
97
+ < head >
98
+ < meta charset ="UTF-8 ">
99
+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
100
+ < title > Login</ title >
101
+ < style >
102
+ body { font-family : Arial, sans-serif; background : # f4f4f4 ; display : flex; align-items : center; justify-content : center; height : 100vh ; }
103
+ .container { background : # fff ; padding : 20px ; border-radius : 10px ; box-shadow : 0 4px 10px rgba (0 , 0 , 0 , 0.1 ); text-align : center; width : 350px ; }
104
+ input , button { width : 100% ; padding : 10px ; margin : 10px 0 ; border : 1px solid # ddd ; border-radius : 5px ; font-size : 16px ; }
105
+ button { background : # 007bff ; color : white; border : none; cursor : pointer; }
106
+ </ style >
107
+ </ head >
108
+ < body >
109
+ < div class ="container ">
110
+ < button onclick ="login() "> Login With APIAuth (Made by < a href ="scratch.mit.edu/users/kRxZy_kRxZy>kRxZy_kRxZy</a>)</button>
111
+ <p id= "codeMessage "> </ p >
112
+ < button onclick ="verifyCode() "> Done</ button >
113
+ </ div >
94
114
< p id ="message " class ="message " aria-live ="polite "> </ p >
95
115
< p >
96
116
Please note: You will be redirected to an external site (Scratch Auth) for authentication. Once there,
@@ -102,6 +122,51 @@ <h2>Welcome! Please log in to continue.</h2>
102
122
< a href ="https://github.com/Scratch-Coding-Hut/Scratch-Coding-Hut.github.io/issues/new ">
103
123
< button > Having trouble signing in? Report an issue</ button >
104
124
</ a >
125
+ < script >
126
+ const serverUrl = 'https://image-hoster.onrender.com' ;
127
+
128
+ if ( localStorage . getItem ( 'verifiedUser' ) ) {
129
+ window . location . href = 'index' ;
130
+ }
131
+
132
+ async function login ( ) {
133
+ let username = document . getElementById ( 'username' ) . value . trim ( ) . toLowerCase ( ) ;
134
+ if ( ! username ) return alert ( 'Enter your Scratch username.' ) ;
135
+
136
+ const response = await fetch ( `${ serverUrl } /login` , {
137
+ method : 'POST' ,
138
+ headers : { 'Content-Type' : 'application/json' } ,
139
+ body : JSON . stringify ( { username } )
140
+ } ) ;
141
+
142
+ const result = await response . json ( ) ;
143
+ if ( result . verified ) {
144
+ localStorage . setItem ( 'verifiedUser' , username ) ;
145
+ window . location . href = 'upload.html' ;
146
+ } else {
147
+ document . getElementById ( 'codeMessage' ) . textContent = result . message ;
148
+ }
149
+ }
150
+
151
+ async function verifyCode ( ) {
152
+ let username = document . getElementById ( 'username' ) . value . trim ( ) . toLowerCase ( ) ;
153
+ if ( ! username ) return alert ( 'Enter your username first.' ) ;
154
+
155
+ const response = await fetch ( `${ serverUrl } /verify` , {
156
+ method : 'POST' ,
157
+ headers : { 'Content-Type' : 'application/json' } ,
158
+ body : JSON . stringify ( { username } )
159
+ } ) ;
160
+
161
+ const result = await response . json ( ) ;
162
+ if ( result . verified ) {
163
+ localStorage . setItem ( 'verifiedUser' , username ) ;
164
+ window . location . href = 'index.html' ;
165
+ } else {
166
+ alert ( result . message || 'Verification failed.' ) ;
167
+ }
168
+ }
169
+ </ script >
105
170
</ div >
106
171
</ body >
107
172
</ html >
0 commit comments