bugfixing refresh token
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package handlers
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"adam-french.co.uk/backend/models"
|
"adam-french.co.uk/backend/models"
|
||||||
@@ -59,15 +60,17 @@ func (store *Store) CheckToken(ctx *gin.Context) {
|
|||||||
func (store *Store) RefreshToken(ctx *gin.Context) {
|
func (store *Store) RefreshToken(ctx *gin.Context) {
|
||||||
refreshToken, err := ctx.Cookie("refresh_token")
|
refreshToken, err := ctx.Cookie("refresh_token")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.JSON(http.StatusUnauthorized, err.Error())
|
ctx.JSON(http.StatusUnauthorized, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
claims, err := store.Auth.VerifyJWT(refreshToken)
|
claims, err := store.Auth.VerifyJWT(refreshToken)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.JSON(http.StatusUnauthorized, err.Error())
|
ctx.JSON(http.StatusUnauthorized, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Printf("claims: %v\n", claims)
|
||||||
|
|
||||||
userID, ok := (*claims)["id"].(uint)
|
userID, ok := (*claims)["id"].(uint)
|
||||||
if !ok {
|
if !ok {
|
||||||
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "invalid token claims"})
|
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "invalid token claims"})
|
||||||
@@ -110,7 +113,7 @@ func (store *Store) RefreshToken(ctx *gin.Context) {
|
|||||||
func (store *Store) Login(ctx *gin.Context) {
|
func (store *Store) Login(ctx *gin.Context) {
|
||||||
var input UserCredentials
|
var input UserCredentials
|
||||||
if err := ctx.ShouldBindBodyWithJSON(&input); err != nil {
|
if err := ctx.ShouldBindBodyWithJSON(&input); err != nil {
|
||||||
ctx.JSON(http.StatusBadRequest, err.Error())
|
ctx.JSON(http.StatusBadRequest, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,13 +124,13 @@ func (store *Store) Login(ctx *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := bcrypt.CompareHashAndPassword(user.Password, []byte(input.Password)); err != nil {
|
if err := bcrypt.CompareHashAndPassword(user.Password, []byte(input.Password)); err != nil {
|
||||||
ctx.JSON(http.StatusUnauthorized, err.Error())
|
ctx.JSON(http.StatusUnauthorized, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
tokens, err := store.Auth.GenerateJWT(&user)
|
tokens, err := store.Auth.GenerateJWT(&user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.JSON(http.StatusInternalServerError, err.Error())
|
ctx.JSON(http.StatusInternalServerError, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user