adding cors handlers for routes

This commit is contained in:
areeqakbr 2025-02-20 12:44:32 +07:00
parent 5a613d31f5
commit 7a25952b1d
2 changed files with 9 additions and 1 deletions

View File

@ -18,7 +18,7 @@ type UsersController struct {
func (uc *UsersController) Route() { func (uc *UsersController) Route() {
rg:= uc.rg.Group("/users") rg:= uc.rg.Group("/users")
// rg.Use(middleware.RateLoginMiddleware()) rg.Use(middleware.RateLoginMiddleware())
rg.Use(middleware.CORSMiddleware()) rg.Use(middleware.CORSMiddleware())
rg.OPTIONS("/login", func(c *gin.Context) { rg.OPTIONS("/login", func(c *gin.Context) {
c.Status(http.StatusNoContent) c.Status(http.StatusNoContent)

View File

@ -43,6 +43,10 @@ func getLoginLimiter() *rate.Limiter {
func RateLimitMiddleware() gin.HandlerFunc{ func RateLimitMiddleware() gin.HandlerFunc{
return func(c *gin.Context) { return func(c *gin.Context) {
if c.Request.Method == http.MethodOptions {
c.Next()
return
}
userID, exists := c.Get("userID") userID, exists := c.Get("userID")
if !exists { if !exists {
common.ErrorResponses(c, http.StatusUnauthorized, "Unauthorized: No user ID found") common.ErrorResponses(c, http.StatusUnauthorized, "Unauthorized: No user ID found")
@ -64,6 +68,10 @@ func RateLimitMiddleware() gin.HandlerFunc{
func RateLoginMiddleware() gin.HandlerFunc{ func RateLoginMiddleware() gin.HandlerFunc{
return func(c *gin.Context) { return func(c *gin.Context) {
if c.Request.Method == http.MethodOptions {
c.Next()
return
}
limiter := getLoginLimiter() limiter := getLoginLimiter()
if !limiter.Allow() { if !limiter.Allow() {