-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[change] Add UserAuthMiddleware to handle user authentication
- Loading branch information
1 parent
a393e2a
commit 4a46c58
Showing
2 changed files
with
34 additions
and
4 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,37 @@ | ||
package middleware | ||
|
||
import ( | ||
"github.com/Firdavs9512/qk-server/app/models" | ||
"github.com/Firdavs9512/qk-server/config" | ||
"github.com/kataras/iris/v12" | ||
) | ||
|
||
type RequestHeader struct { | ||
// | ||
Authorization string `header:"Authorization,required"` | ||
} | ||
|
||
func UserAuthMiddleware()iris.Handler{ | ||
// | ||
} | ||
func UserAuthMiddleware() iris.Handler { | ||
return func(ctx iris.Context) { | ||
var requestHeader RequestHeader | ||
if err := ctx.ReadHeaders(&requestHeader); err != nil { | ||
ctx.StatusCode(iris.StatusBadRequest) | ||
ctx.JSON(iris.Map{"message": "Invalid request"}) | ||
return | ||
} | ||
|
||
if requestHeader.Authorization == "" { | ||
ctx.StatusCode(iris.StatusUnauthorized) | ||
ctx.JSON(iris.Map{"message": "Unauthorized"}) | ||
return | ||
} | ||
|
||
var token models.AuthToken | ||
if err := config.Database.DB.Where("token = ?", requestHeader.Authorization).First(&token).Error; err != nil { | ||
ctx.StatusCode(iris.StatusUnauthorized) | ||
ctx.JSON(iris.Map{"message": "Unauthorized"}) | ||
return | ||
} | ||
|
||
ctx.Next() | ||
} | ||
} |
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