Skip to content

Instantly share code, notes, and snippets.

View robbypambudi's full-sized avatar
🏠
Working from home

Robby Pambudi robbypambudi

🏠
Working from home
View GitHub Profile
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]
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)