adding cors handlers for routes
This commit is contained in:
parent
5a613d31f5
commit
7a25952b1d
|
|
@ -18,7 +18,7 @@ type UsersController struct {
|
|||
|
||||
func (uc *UsersController) Route() {
|
||||
rg:= uc.rg.Group("/users")
|
||||
// rg.Use(middleware.RateLoginMiddleware())
|
||||
rg.Use(middleware.RateLoginMiddleware())
|
||||
rg.Use(middleware.CORSMiddleware())
|
||||
rg.OPTIONS("/login", func(c *gin.Context) {
|
||||
c.Status(http.StatusNoContent)
|
||||
|
|
|
|||
|
|
@ -43,6 +43,10 @@ func getLoginLimiter() *rate.Limiter {
|
|||
|
||||
func RateLimitMiddleware() gin.HandlerFunc{
|
||||
return func(c *gin.Context) {
|
||||
if c.Request.Method == http.MethodOptions {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
userID, exists := c.Get("userID")
|
||||
if !exists {
|
||||
common.ErrorResponses(c, http.StatusUnauthorized, "Unauthorized: No user ID found")
|
||||
|
|
@ -64,6 +68,10 @@ func RateLimitMiddleware() gin.HandlerFunc{
|
|||
|
||||
func RateLoginMiddleware() gin.HandlerFunc{
|
||||
return func(c *gin.Context) {
|
||||
if c.Request.Method == http.MethodOptions {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
limiter := getLoginLimiter()
|
||||
|
||||
if !limiter.Allow() {
|
||||
|
|
|
|||
Loading…
Reference in New Issue