Skip to content

Commit 421aebe

Browse files
committed
Remove username MC account addition
1 parent bdbfead commit 421aebe

File tree

1 file changed

+33
-70
lines changed

1 file changed

+33
-70
lines changed

pkg/web/api/account.go

Lines changed: 33 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package api
22

33
import (
4-
"bytes"
54
"encoding/json"
65
"github.com/meyskens/go-turnstile"
76
"image"
87
_ "image/png"
9-
"io/ioutil"
108
"meteor-server/pkg/discord"
119
"net/http"
1210
"os"
@@ -258,86 +256,51 @@ func McAccountHandler(w http.ResponseWriter, r *http.Request) {
258256

259257
if r.Method == "POST" {
260258
code := r.URL.Query().Get("code")
259+
if code == "" {
260+
core.JsonError(w, "Invalid code")
261+
return
262+
}
261263

262-
if code != "" {
263-
res, err := http.Get("http://mcauth:8080/retrieve/" + code)
264-
if err != nil {
265-
core.JsonError(w, "Failed to retrieve code's UUID. "+err.Error())
266-
return
267-
}
268-
269-
//goland:noinspection GoUnhandledErrorResult
270-
defer res.Body.Close()
271-
272-
if res.StatusCode != http.StatusOK {
273-
var data struct{ Error string }
274-
275-
err := json.NewDecoder(res.Body).Decode(&data)
276-
if err != nil {
277-
core.JsonError(w, "Failed to parse code's UUID Error")
278-
return
279-
}
280-
281-
core.JsonError(w, data.Error)
282-
return
283-
}
264+
res, err := http.Get("http://mcauth:8080/retrieve/" + code)
265+
if err != nil {
266+
core.JsonError(w, "Failed to retrieve code's UUID. "+err.Error())
267+
return
268+
}
284269

285-
var data struct{ Uuid string }
270+
//goland:noinspection GoUnhandledErrorResult
271+
defer res.Body.Close()
286272

287-
err = json.NewDecoder(res.Body).Decode(&data)
288-
if err != nil {
289-
core.JsonError(w, "Failed to parse code's UUID")
290-
return
291-
}
273+
if res.StatusCode != http.StatusOK {
274+
var data struct{ Error string }
292275

293-
id, err := uuid.Parse(data.Uuid)
276+
err := json.NewDecoder(res.Body).Decode(&data)
294277
if err != nil {
295-
core.JsonError(w, "Invalid UUID")
278+
core.JsonError(w, "Failed to parse code's UUID Error")
296279
return
297280
}
298281

299-
err = account.AddMcAccount(id)
300-
if err != nil {
301-
core.JsonError(w, err.Error())
302-
return
303-
}
304-
} else {
305-
// Get Minecraft username
306-
username := r.URL.Query().Get("username")
307-
if username == "" {
308-
core.JsonError(w, "Invalid username 1.")
309-
return
310-
}
311-
312-
// Get uuid
313-
req, _ := http.NewRequest("GET", "https://api.mojang.com/users/profiles/minecraft/"+username, bytes.NewReader([]byte{}))
314-
req.Header.Set("User-Agent", "Meteor Server")
315-
316-
client := http.Client{}
317-
res, err := client.Do(req)
318-
if err != nil {
319-
core.JsonError(w, "Invalid username 2.")
320-
return
321-
}
282+
core.JsonError(w, data.Error)
283+
return
284+
}
322285

323-
body, _ := ioutil.ReadAll(res.Body)
324-
var user mcUser
325-
_ = json.Unmarshal(body, &user)
286+
var data struct{ Uuid string }
326287

327-
_ = res.Body.Close()
288+
err = json.NewDecoder(res.Body).Decode(&data)
289+
if err != nil {
290+
core.JsonError(w, "Failed to parse code's UUID")
291+
return
292+
}
328293

329-
id, err := uuid.Parse(user.Id)
330-
if err != nil {
331-
core.JsonError(w, "Invalid username 3. "+err.Error())
332-
return
333-
}
294+
id, err := uuid.Parse(data.Uuid)
295+
if err != nil {
296+
core.JsonError(w, "Invalid UUID")
297+
return
298+
}
334299

335-
// Add Minecraft account
336-
err = account.AddMcAccount(id)
337-
if err != nil {
338-
core.JsonError(w, err.Error())
339-
return
340-
}
300+
err = account.AddMcAccount(id)
301+
if err != nil {
302+
core.JsonError(w, err.Error())
303+
return
341304
}
342305

343306
UpdateCapes()

0 commit comments

Comments
 (0)