mirror of
https://github.com/Uttkarsh-raj/Plannerly.git
synced 2025-11-23 21:54:39 +02:00
33 lines
775 B
Go
33 lines
775 B
Go
package middleware
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/Uttkarsh-raj/To_Do_App/helpers"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func Authentication() gin.HandlerFunc {
|
|
return func(ctx *gin.Context) {
|
|
clientToken := ctx.Request.Header.Get("token")
|
|
if clientToken == "" {
|
|
ctx.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("No Authorization header provided."), "success": false})
|
|
ctx.Abort()
|
|
return
|
|
}
|
|
claims, err := helpers.ValidateToken(clientToken)
|
|
if err != "" {
|
|
ctx.JSON(http.StatusInternalServerError, gin.H{"error": err, "success": false})
|
|
ctx.Abort()
|
|
return
|
|
}
|
|
ctx.Set("email", claims.Email)
|
|
ctx.Set("first_name", claims.First_name)
|
|
ctx.Set("last_name", claims.Last_name)
|
|
ctx.Set("uid", claims.Uid)
|
|
|
|
ctx.Next()
|
|
}
|
|
}
|