36 lines
653 B
Go
36 lines
653 B
Go
package routes
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"stereo.cat/backend/internal/auth"
|
|
"stereo.cat/backend/internal/types"
|
|
)
|
|
|
|
func RegisterAuthRoutes(cfg *types.StereoConfig, api *gin.RouterGroup) {
|
|
api.GET("/auth/callback", func(c *gin.Context) {
|
|
code := c.Query("code")
|
|
|
|
t, err := cfg.Client.ExchangeCode(code)
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
user, err := cfg.Client.GetUser(t)
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
jwt, err := auth.GenerateJWT(cfg.JWTSecret, user, uint64(time.Now().Add(time.Second*time.Duration(t.ExpiresIn)).Unix()))
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
c.String(http.StatusOK, jwt)
|
|
})
|
|
}
|