add: Login system
This commit is contained in:
@@ -17,7 +17,7 @@ var (
|
||||
jwtParser = jwt.NewParser(jwt.WithValidMethods([]string{jwtSigningMethod.Alg()}))
|
||||
)
|
||||
|
||||
func GenerateToken(userID string) (string, error) {
|
||||
func GenerateToken(userID int64) (string, error) {
|
||||
token := jwt.NewWithClaims(jwtSigningMethod, jwt.MapClaims{
|
||||
"user_id": userID,
|
||||
"exp": time.Now().Add(time.Minute * 15).Unix(), // Token expires in 15 Minutes
|
||||
|
||||
@@ -19,7 +19,7 @@ func TestAuthMiddleware(t *testing.T) {
|
||||
name string
|
||||
setupAuth func(r *http.Request)
|
||||
expectedStatus int
|
||||
expectedUserID string
|
||||
expectedUserID int64
|
||||
}{
|
||||
{
|
||||
name: "Valid Token",
|
||||
@@ -28,13 +28,13 @@ func TestAuthMiddleware(t *testing.T) {
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
},
|
||||
expectedStatus: http.StatusOK,
|
||||
expectedUserID: "user123",
|
||||
expectedUserID: 12,
|
||||
},
|
||||
{
|
||||
name: "Missing Auth Header",
|
||||
setupAuth: func(r *http.Request) {},
|
||||
expectedStatus: http.StatusUnauthorized,
|
||||
expectedUserID: "",
|
||||
expectedUserID: 0,
|
||||
},
|
||||
{
|
||||
name: "Invalid Token Format",
|
||||
@@ -42,7 +42,7 @@ func TestAuthMiddleware(t *testing.T) {
|
||||
r.Header.Set("Authorization", "InvalidFormat")
|
||||
},
|
||||
expectedStatus: http.StatusUnauthorized,
|
||||
expectedUserID: "",
|
||||
expectedUserID: 0,
|
||||
},
|
||||
{
|
||||
name: "Expired Token",
|
||||
@@ -55,7 +55,7 @@ func TestAuthMiddleware(t *testing.T) {
|
||||
r.Header.Set("Authorization", "Bearer "+tokenString)
|
||||
},
|
||||
expectedStatus: http.StatusUnauthorized,
|
||||
expectedUserID: "",
|
||||
expectedUserID: 0,
|
||||
},
|
||||
{
|
||||
name: "Invalid Signature",
|
||||
@@ -68,13 +68,13 @@ func TestAuthMiddleware(t *testing.T) {
|
||||
r.Header.Set("Authorization", "Bearer "+tokenString)
|
||||
},
|
||||
expectedStatus: http.StatusUnauthorized,
|
||||
expectedUserID: "",
|
||||
expectedUserID: 0,
|
||||
},
|
||||
{
|
||||
name: "Missing Auth Header",
|
||||
setupAuth: func(r *http.Request) {},
|
||||
expectedStatus: http.StatusUnauthorized,
|
||||
expectedUserID: "",
|
||||
expectedUserID: 0,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -88,7 +88,7 @@ func TestAuthMiddleware(t *testing.T) {
|
||||
if exists {
|
||||
c.JSON(http.StatusOK, gin.H{"user_id": userID})
|
||||
} else {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"user_id": ""})
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"user_id": 0})
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user