|
11 | 11 | padding: 0; |
12 | 12 | background-color: #f4f4f4; |
13 | 13 | } |
14 | | - |
15 | 14 | .header { |
16 | 15 | background-color: #c04d4d; |
17 | 16 | color: white; |
18 | 17 | text-align: center; |
19 | 18 | padding: 20px; |
20 | 19 | } |
21 | | - |
22 | | - .header h1 { |
23 | | - margin: 0; |
24 | | - } |
25 | | - |
26 | | - .menu-container { |
27 | | - position: relative; |
28 | | - display: flex; |
29 | | - justify-content: space-between; |
30 | | - background: #c04d4d; |
31 | | - padding: 20px; |
32 | | - color: #fff; |
33 | | - box-sizing: border-box; |
34 | | - } |
35 | | - |
36 | | - .menu-logo img { |
37 | | - max-height: 40px; |
38 | | - max-width: 100px; |
39 | | - } |
40 | | - |
41 | | - .menu-container a { |
42 | | - text-decoration: none; |
43 | | - color: #ffffff; |
44 | | - transition: color 0.3s ease; |
45 | | - } |
46 | | - |
47 | | - .menu-container a:hover { |
48 | | - color: #50e3c2; |
49 | | - } |
50 | | - |
51 | | - .menu ul { |
52 | | - list-style: none; |
53 | | - display: flex; |
54 | | - padding: 0; |
55 | | - margin: 0; |
56 | | - } |
57 | | - |
58 | | - .menu li { |
59 | | - padding: 0 20px; |
60 | | - font-size: 22px; |
61 | | - } |
62 | | - |
63 | 20 | .content { |
64 | 21 | padding: 20px; |
65 | 22 | text-align: center; |
66 | 23 | } |
67 | | - |
68 | | - .logout-btn { |
69 | | - background-color: #c04d4d; |
70 | | - color: white; |
71 | | - padding: 10px 20px; |
72 | | - border: none; |
73 | | - cursor: pointer; |
74 | | - font-size: 16px; |
75 | | - margin-top: 20px; |
76 | | - } |
77 | | - |
78 | | - .logout-btn:hover { |
79 | | - background-color: #a03b3b; |
80 | | - } |
81 | | - |
82 | | - /* Form Styles */ |
83 | | - input[type="text"] { |
| 24 | + input[type="text"], button { |
84 | 25 | padding: 10px; |
85 | 26 | margin: 10px 0; |
86 | 27 | font-size: 16px; |
87 | | - border: 1px solid #ccc; |
88 | | - width: 250px; |
89 | | - border-radius: 4px; |
90 | | - } |
91 | | - |
92 | | - button { |
93 | | - background-color: #50e3c2; |
94 | | - color: white; |
95 | | - padding: 10px 20px; |
96 | | - font-size: 16px; |
97 | | - border: none; |
98 | | - cursor: pointer; |
99 | 28 | border-radius: 4px; |
100 | 29 | } |
101 | | - |
102 | | - button:hover { |
103 | | - background-color: #3ec1a0; |
104 | | - } |
105 | 30 | </style> |
106 | 31 | <script> |
107 | 32 | async function registerUser() { |
|
110 | 35 | alert("Please enter a Scratch username."); |
111 | 36 | return; |
112 | 37 | } |
113 | | - |
114 | | - const response = await fetch("server.php", { |
115 | | - method: "POST", |
116 | | - headers: { "Content-Type": "application/x-www-form-urlencoded" }, |
117 | | - body: `username=${encodeURIComponent(username)}` |
118 | | - }); |
119 | | - |
120 | | - const data = await response.json(); |
121 | | - if (data.error) { |
122 | | - alert(data.error); |
123 | | - } else { |
124 | | - localStorage.setItem("privateKey", data.privateKey); |
125 | | - alert("Your private key has been generated and saved locally. Keep it safe!"); |
126 | | - } |
| 38 | + |
| 39 | + const privateKey = Math.random().toString(36).substr(2, 10); |
| 40 | + localStorage.setItem("privateKey", privateKey); |
| 41 | + alert(`Post this key on your Scratch profile: ${privateKey}`); |
127 | 42 | } |
128 | 43 |
|
129 | 44 | async function authenticateUser() { |
| 45 | + const username = document.getElementById("username").value.trim(); |
130 | 46 | const privateKey = localStorage.getItem("privateKey"); |
131 | | - if (!privateKey) { |
132 | | - alert("No private key found. Please register first."); |
| 47 | + if (!username || !privateKey) { |
| 48 | + alert("Make sure you have registered and saved your key."); |
133 | 49 | return; |
134 | 50 | } |
135 | | - |
136 | | - const response = await fetch("server.php", { |
137 | | - method: "POST", |
138 | | - headers: { "Content-Type": "application/x-www-form-urlencoded" }, |
139 | | - body: `challengeResponse=${btoa(privateKey)}` |
140 | | - }); |
141 | | - |
| 51 | + |
| 52 | + const response = await fetch(`https://api.scratch.mit.edu/users/${username}/`); |
142 | 53 | const data = await response.json(); |
143 | | - if (data.success) { |
| 54 | + |
| 55 | + if (data.profile?.bio.includes(privateKey)) { |
144 | 56 | alert("Authentication successful!"); |
145 | | - window.location.href = "#dashboard"; // Redirect to dashboard |
| 57 | + document.getElementById("login").style.display = "none"; |
| 58 | + document.getElementById("dashboard").style.display = "block"; |
146 | 59 | } else { |
147 | | - alert(data.error); |
| 60 | + alert("Key not found in bio. Make sure you've posted it and try again."); |
148 | 61 | } |
149 | 62 | } |
150 | 63 |
|
151 | 64 | function logout() { |
152 | | - // Clear the private key from local storage |
153 | 65 | localStorage.removeItem("privateKey"); |
154 | | - |
155 | | - // Redirect to login page |
156 | | - window.location.href = "#login"; // Redirect to login page |
| 66 | + document.getElementById("dashboard").style.display = "none"; |
| 67 | + document.getElementById("login").style.display = "block"; |
157 | 68 | } |
158 | 69 | </script> |
159 | 70 | </head> |
160 | 71 | <body> |
161 | | - <!-- Login Form --> |
162 | 72 | <div id="login"> |
163 | 73 | <div class="header"> |
164 | 74 | <h1>Scratch Authentication</h1> |
165 | 75 | </div> |
166 | | - |
167 | 76 | <div class="content"> |
168 | 77 | <label for="username">Scratch Username:</label> |
169 | 78 | <input type="text" id="username" placeholder="Enter your Scratch username" /> |
170 | 79 | <br /> |
171 | 80 | <button onclick="registerUser()">Register</button> |
172 | 81 | <br /><br /> |
173 | | - <button onclick="authenticateUser()">Login with Key</button> |
| 82 | + <button onclick="authenticateUser()">Login with Scratch</button> |
174 | 83 | </div> |
175 | 84 | </div> |
176 | | - |
177 | | - <!-- Dashboard --> |
| 85 | + |
178 | 86 | <div id="dashboard" style="display: none;"> |
179 | 87 | <div class="header"> |
180 | 88 | <h1>Welcome to Your Dashboard!</h1> |
181 | 89 | </div> |
182 | | - |
183 | 90 | <div class="content"> |
184 | | - <p>You're successfully logged in with your private key. You can now access your personalized settings and manage your account.</p> |
185 | | - <button class="logout-btn" onclick="logout()">Logout</button> |
| 91 | + <p>You're successfully logged in!</p> |
| 92 | + <button onclick="logout()">Logout</button> |
186 | 93 | </div> |
187 | 94 | </div> |
188 | | - |
189 | | - <script> |
190 | | - // Check if user is already authenticated |
191 | | - if (localStorage.getItem("privateKey")) { |
192 | | - document.getElementById("login").style.display = "none"; |
193 | | - document.getElementById("dashboard").style.display = "block"; |
194 | | - } |
195 | | - </script> |
196 | 95 | </body> |
197 | 96 | </html> |
0 commit comments