added check token handle
This commit is contained in:
@@ -9,7 +9,6 @@ import (
|
||||
)
|
||||
|
||||
func (store *Store) AuthMiddlewear(ctx *gin.Context) {
|
||||
|
||||
access_token, err := ctx.Cookie("access_token")
|
||||
if err != nil {
|
||||
ctx.AbortWithStatusJSON(401, gin.H{"error": "unauthorized"})
|
||||
@@ -27,6 +26,26 @@ func (store *Store) AuthMiddlewear(ctx *gin.Context) {
|
||||
ctx.Next()
|
||||
}
|
||||
|
||||
func (store *Store) CheckToken(ctx *gin.Context) {
|
||||
access_token, err := ctx.Cookie("access_token")
|
||||
if err != nil {
|
||||
ctx.JSON(401, gin.H{"error": "unauthorized"})
|
||||
return
|
||||
}
|
||||
|
||||
claims, err := store.Auth.VerifyJWT(access_token)
|
||||
if err != nil {
|
||||
ctx.JSON(401, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, gin.H{"data": gin.H{
|
||||
"id": (*claims)["id"],
|
||||
"username": (*claims)["username"],
|
||||
"admin": (*claims)["admin"],
|
||||
}})
|
||||
}
|
||||
|
||||
func (store *Store) RefreshToken(ctx *gin.Context) {
|
||||
refreshToken, err := ctx.Cookie("refresh_token")
|
||||
if err != nil {
|
||||
@@ -76,7 +95,6 @@ func (store *Store) RefreshToken(ctx *gin.Context) {
|
||||
)
|
||||
|
||||
ctx.JSON(http.StatusAccepted, gin.H{"data": user})
|
||||
|
||||
}
|
||||
|
||||
func (store *Store) Login(ctx *gin.Context) {
|
||||
|
||||
@@ -58,6 +58,7 @@ func main() {
|
||||
|
||||
r.POST("/auth/login", store.Login)
|
||||
r.POST("/auth/refresh", store.RefreshToken)
|
||||
r.GET("/auth/refresh", store.CheckToken)
|
||||
|
||||
r.GET("/spotify/callback", store.CompleteSpotifyAuth)
|
||||
r.GET("/spotify/listening", store.ListeningTo)
|
||||
|
||||
Reference in New Issue
Block a user