mirror of
https://github.com/mgerb/go-discord-bot
synced 2026-01-11 01:22:48 +00:00
back end done for video archiving
This commit is contained in:
61
server/webserver/routes/oauth.go
Normal file
61
server/webserver/routes/oauth.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/mgerb/go-discord-bot/server/webserver/discord"
|
||||
"github.com/mgerb/go-discord-bot/server/webserver/middleware"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
const cashGuildID = "101198129352691712"
|
||||
|
||||
type oauthReq struct {
|
||||
Code string `json:"code"`
|
||||
}
|
||||
|
||||
// AddOauthRoutes -
|
||||
func AddOauthRoutes(group *gin.RouterGroup) {
|
||||
group.POST("/oauth", oauthHandler)
|
||||
}
|
||||
|
||||
func oauthHandler(c *gin.Context) {
|
||||
|
||||
var json oauthReq
|
||||
|
||||
err := c.ShouldBindJSON(&json)
|
||||
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
c.JSON(500, err)
|
||||
return
|
||||
}
|
||||
|
||||
// get users oauth code
|
||||
oauth, err := discord.Oauth(json.Code)
|
||||
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
c.JSON(500, err)
|
||||
return
|
||||
}
|
||||
|
||||
// verify and grab user information
|
||||
user, err := discord.GetUserInfo(oauth.AccessToken)
|
||||
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
c.JSON(500, err)
|
||||
return
|
||||
}
|
||||
|
||||
// generate json web token
|
||||
token, err := middleware.GetJWT(user)
|
||||
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
c.JSON(500, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, token)
|
||||
}
|
||||
Reference in New Issue
Block a user