10
|
1 package api
|
|
2
|
|
3 import (
|
|
4 "net/http"
|
|
5
|
|
6 "github.com/denniscmcom/pacobot/auth"
|
|
7 "github.com/denniscmcom/pacobot/socket"
|
|
8 "github.com/gin-gonic/gin"
|
|
9 )
|
|
10
|
|
11 func GetUserHandler(c *gin.Context) {
|
|
12 userName := c.Query("username")
|
|
13 user := auth.GetUser(userName, getAccessToken())
|
|
14
|
|
15 c.JSON(http.StatusOK, gin.H{
|
|
16 "message": user.Data[len(user.Data)-1].Id,
|
|
17 })
|
|
18 }
|
|
19
|
|
20 func AuthHandler(c *gin.Context) {
|
|
21 authUrl := auth.GetAuthUrl()
|
|
22 c.Redirect(http.StatusMovedPermanently, authUrl)
|
|
23 }
|
|
24
|
|
25 func AuthValidateHandler(c *gin.Context) {
|
|
26 msg := "failed"
|
|
27
|
|
28 if auth.IsAuthTokenValid(getAccessToken()) {
|
|
29 msg = "ok"
|
|
30 }
|
|
31
|
|
32 c.JSON(http.StatusOK, gin.H{
|
|
33 "message": msg,
|
|
34 })
|
|
35 }
|
|
36
|
|
37 func AuthRefreshHandler(c *gin.Context) {
|
|
38 authRes := auth.RefreshAuthToken(getAccessToken(), getRefreshToken())
|
|
39 setAccessToken(authRes.AccessToken)
|
|
40 setRefreshToken(authRes.RefreshToken)
|
|
41
|
|
42 c.JSON(http.StatusOK, gin.H{
|
|
43 "message": "ok",
|
|
44 })
|
|
45 }
|
|
46
|
|
47 func AuthRevokeHandler(c *gin.Context) {
|
|
48 auth.RevokeAuthToken(getAccessToken())
|
|
49
|
|
50 c.JSON(http.StatusOK, gin.H{
|
|
51 "message": "ok",
|
|
52 })
|
|
53 }
|
|
54
|
|
55 func TwitchCallbackHandler(c *gin.Context) {
|
|
56 authCode := c.Query("code")
|
|
57 authRes := auth.GetAuthToken(authCode)
|
|
58 authStore.Store("accessToken", authRes.AccessToken)
|
|
59 authStore.Store("refreshToken", authRes.RefreshToken)
|
|
60
|
|
61 c.JSON(http.StatusOK, gin.H{
|
|
62 "message": "ok",
|
|
63 })
|
|
64 }
|
|
65
|
|
66 func ConnectHandler(c *gin.Context) {
|
|
67 go socket.Connect(getAccessToken())
|
|
68
|
|
69 c.JSON(http.StatusOK, gin.H{
|
|
70 "message": "ok",
|
|
71 })
|
|
72 }
|