Issue arises when logging in through an endpoint results in a response header with a http-only cookie. However, subsequent requests to other endpoints do not include the set-cookie in the headers. Attempts have been made to resolve this problem.
The following is the Angular code within a service.ts file:
private apiUrl = environment.baseUrl + '/api/v1/login';
private apiUrl2 = environment.baseUrl + '/api/v1/anotherRequest';
login(req: Login): Observable<string> {
const headers = new HttpHeaders({'Content-Type': 'application/json'});
return this.httpClient.post<string>(this.apiUrl,JSON.stringify(req),{ headers })
}
anotherRequestBeingMade(): Observable<any]> {
const headers = new HttpHeaders({'Content-Type': 'application/json'});
return this.httpClient.get<any>(this.apiUrl2,{withCredentials:true})
}
This demonstrates the setup of an HTTP-only cookie in Fiber using Golang:
cookie := fiber.Cookie{
Name: "my_cookie",
Value: *value_here*,
Expires: time.Now().Add(time.Hour * 24),
HTTPOnly: true,
Secure: true,
SameSite: "None",
Path: "/",
}
c.Cookie(&cookie)
Testing in Postman shows the cookie being set in the response header for the login endpoint but not carried over to subsequent requests in the Cookie header when tested in Angular client. Various attempted solutions include:
Including
{withCredentials:true}
inside the get httpClientResolving CORS issues in the Golang backend
app.Use(cors.New(cors.Config{
AllowOrigins: "http://localhost:4200",
AllowHeaders: "Origin, Content-Type, Accept",
AllowMethods: "GET, POST, PUT, DELETE, OPTIONS",
ExposeHeaders: "Set-Cookie",
AllowCredentials: true,
}))
- Attempting to bypass the proxy by setting up a proxy.conf.json and configuring the package.json script as follows:
"start": "ng serve --proxy-config proxy.conf.json",
{
"/api/*": {
"target": "http://localhost:8080",
"secure": false,
"logLevel": "debug"
}
}
Adding an environment.ts file in the src folder of the Angular app:
export const environment = {
production: false,
baseUrl: 'http://localhost:8080' // Update according to your backend server URL
};
Thank you for any assistance provided.