jeudi 28 octobre 2021

Authentication middleware is given still giving CORS error

I am making an Jwt applicaion using go react and mongodb application is backend is working absolutely fine when I am trying to connect and perform POST and GET operation using Postmaster. But when I am doing the same thing from front end react it's giving me error saying CORS error request forbidden, I am giving you the middle ware code as well as the git lab link please go to gitlab and see my code where I done this mistake and please provide me a solution

[GitLab full code link]  

https://gitlab.com/RajrupDasid/golang-user-authentication/-/tree/master

func Authentication() gin.HandlerFunc {
    return func(c *gin.Context) {
        clientToken := c.Request.Header.Get("token")
        if clientToken == "" {
            c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("No Authorization header provided")})
            c.Abort()
            return
        }

        claims, err := helper.ValidateToken(clientToken)
        if err != "" {
            c.JSON(http.StatusInternalServerError, gin.H{"error": err})
            c.Abort()
            return
        }

        c.Set("email", claims.Email)
        c.Set("first_name", claims.First_name)
        c.Set("last_name", claims.Last_name)
        c.Set("uid", claims.Uid)

        c.Next()

    }
}
func CORSMiddleware() gin.HandlerFunc {
    return func(c *gin.Context) {
        c.Writer.Header().Set("Content-Type", "application/json")
        c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
        c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
        c.Writer.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
    }
}



Aucun commentaire:

Enregistrer un commentaire