view api/handlers.go @ 12:aaf85ae1f942

add very simple html template
author Dennis C. M. <dennis@denniscm.com>
date Thu, 20 Mar 2025 11:12:21 +0000
parents 5c124578fed2
children
line wrap: on
line source

package api

import (
	"net/http"

	"github.com/denniscmcom/pacobot/auth"
	"github.com/denniscmcom/pacobot/socket"
	"github.com/denniscmcom/pacobot/store"
	"github.com/gin-gonic/gin"
)

func GetUser(c *gin.Context) {
	userName := c.Query("username")
	user := auth.GetUser(userName, store.GetAccessToken())

	c.JSON(http.StatusOK, gin.H{
		"message": user.Data[len(user.Data)-1].Id,
	})
}

func Auth(c *gin.Context) {
	authUrl := auth.GetAuthUrl()
	c.Redirect(http.StatusMovedPermanently, authUrl)
}

func AuthValidate(c *gin.Context) {
	msg := "failed"

	if auth.IsAuthTokenValid(store.GetAccessToken()) {
		msg = "ok"
	}

	c.JSON(http.StatusOK, gin.H{
		"message": msg,
	})
}

func AuthRefresh(c *gin.Context) {
	authRes := auth.RefreshAuthToken(store.GetAccessToken(), store.GetRefreshToken())
	store.SetAccessToken(authRes.AccessToken)
	store.SetRefreshToken(authRes.RefreshToken)

	c.JSON(http.StatusOK, gin.H{
		"message": "ok",
	})
}

func AuthRevoke(c *gin.Context) {
	auth.RevokeAuthToken(store.GetAccessToken())

	c.JSON(http.StatusOK, gin.H{
		"message": "ok",
	})
}

func Twitch(c *gin.Context) {
	authCode := c.Query("code")
	authRes := auth.GetAuthToken(authCode)
	store.SetAccessToken(authRes.AccessToken)
	store.SetRefreshToken(authRes.RefreshToken)
	c.Redirect(http.StatusFound, "/")
}

func Connect(c *gin.Context) {
	go socket.Connect(store.GetAccessToken())

	c.JSON(http.StatusOK, gin.H{
		"message": "ok",
	})
}