-
Notifications
You must be signed in to change notification settings - Fork 0
/
authMiddleware.go
54 lines (46 loc) · 1.33 KB
/
authMiddleware.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package middlewares
import (
"fmt"
"net/http"
"github.com/btdjangbah001/chat-app/models"
"github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"
)
func AuthMiddleware(chat bool) gin.HandlerFunc {
return func(c *gin.Context) {
var tokenString string
if chat {
tokenString = c.Param("token")
} else {
tokenString = c.GetHeader("Authorization")
}
if tokenString == "" {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
return
}
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
}
return []byte("my_secret_key"), nil // replace with your own secret key
})
if err != nil {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
return
}
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
userID := claims["UserID"].(float64)
var user *models.User
user, err = models.GetUser(uint(userID))
if err != nil {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
return
}
c.Set("user", user)
c.Next()
} else {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
return
}
}
}