This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const jwt = require ('jsonwebtoken'); | |
function generateAccessToken(user) { | |
return jwt.sign(user, process.env.TOKEN_SECRET); | |
} | |
function authenticateToken(req, res, next) { | |
const authHeader = req.headers['authorization'] | |
const token = authHeader && authHeader.split(' ')[1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func Authenticate(jwtService service.JWTService, role string) gin.HandlerFunc { | |
return func(c *gin.Context) { | |
authHeader := c.GetHeader("Authorization") | |
if authHeader == "" { | |
response := utils.BuildErrorResponse("No token found", http.StatusUnauthorized) | |
c.AbortWithStatusJSON(http.StatusUnauthorized, response) | |
return | |
} | |
if !strings.Contains(authHeader, "Bearer ") { | |
response := utils.BuildErrorResponse("No token found", http.StatusUnauthorized) |