-
Notifications
You must be signed in to change notification settings - Fork 0
/
link.go
64 lines (55 loc) · 1.38 KB
/
link.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"github.com/gin-gonic/gin"
"net/http"
"strings"
)
const (
IsValidHash = `SELECT EXISTS(SELECT 1 FROM accounts WHERE password = $1)`
GetWiiNumber = `SELECT mlid FROM accounts WHERE password = $1`
LinkAccount = `INSERT INTO users (email, wii_number) VALUES ($1, $2)`
)
func link(c *gin.Context) {
hash := c.Query("H")
hash = strings.ToLower(hash)
var valid bool
err := wiiMailPool.QueryRow(ctx, IsValidHash, hash).Scan(&valid)
if err != nil {
c.HTML(http.StatusBadRequest, "error.html", gin.H{
"Error": err.Error(),
})
return
}
if !valid {
c.HTML(http.StatusOK, "link.html", gin.H{
"message": "Invalid Wii console. Please ensure you are registered with WiiLink Wii Mail.",
})
return
}
// Link the account.
email, ok := c.Get("email")
if !ok {
c.HTML(http.StatusBadRequest, "error.html", gin.H{
"Error": "some how got here unauthorized",
})
return
}
var wiiNumber string
err = wiiMailPool.QueryRow(ctx, GetWiiNumber, hash).Scan(&wiiNumber)
if err != nil {
c.HTML(http.StatusBadRequest, "error.html", gin.H{
"Error": err.Error(),
})
return
}
_, err = pool.Exec(ctx, LinkAccount, email.(string), wiiNumber)
if err != nil {
c.HTML(http.StatusBadRequest, "error.html", gin.H{
"Error": err.Error(),
})
return
}
c.HTML(http.StatusOK, "link.html", gin.H{
"message": "Successfully linked your Wii to your account!",
})
}