DEV Community

Kaan Kuscu
Kaan Kuscu

Posted on

Configuring Gofiber API to access session or cookies from JavaScript Side

In this post we gonna learn how to access to api-created cookie or session data from JavaScript side.

What's inside of Gofiber side (Go)

Firstly, to access cookie or session data, we have to configure Gofiber app.

app := fiber.New()

app.Use(cors.New(cors.Config{
    AllowOrigins:     "http://localhost:3000",
    AllowCredentials: true,
}))
Enter fullscreen mode Exit fullscreen mode

In above, we activated CORS, and defined a server address for JavaScript-used website.

Warning: Using * char for AllowOrigins property is invalid. Because you might write a specific address for healthy solution.

Then AllowCredentials property has to be true value.

According to these configs, We can request to this API from http://localhost:3000 address.

What's inside of JavaScript Side

For case in above, we can use fetch api to request to Gofiber app.

Get Gofiber app's address https://localhost:3001

fetch("http://localhost:3000/example", {
        credentials: 'include'
    })
Enter fullscreen mode Exit fullscreen mode

According to example in above, as an option, we can set credentials property to 'include'. Thus we can add cookies or session datas coming from api to browser.

Top comments (0)