angular ng serve with proxy config doesn't set cookies from laravel API
Asked Answered
P

1

12

I have a project that uses angular as the front end and has a laravel API for the backend. I want to make use of ng serve's proxy live reloading:

ng serve --proxy-config proxy.config.js

Here is the proxy.config.js file:

const PROXY_CONFIG = {                             
  "/v2/*": {                                     
    "target": "http://local-laravel-api/",
    "secure": false,                           
    "pathRewrite": {                           
      "^/v2": "/v2"                            
    },                                         
    "logLevel": "debug", 
    "changeOrigin": true
  }
}

module.exports = PROXY_CONFIG;

The laravel API has auth. When I visit localhost:4200 I get "Not Authenticated" and that is because the auth cookie is not set in the browser when going through the proxy.

If I visit the API directly (going to local-laravel-api in the browser) the cookies exist and I am authenticated.

So basically, it seems the proxy isn't picking up the cookies (and I have no idea why).

I've seen multiple threads on the net discussing the same issues, I've exhausted all those and still cannot access the API.

Does anyone have any ideas that could shed some light on this issue?

Any help is greatly appreciated!

Poyang answered 31/10, 2017 at 22:57 Comment(2)
Is this only happening when developing locally? If you deploy your solution on the same domain do you still see the error? I have not tested this but I think you may be able to set the cookieDomainRewrite property in your proxy config file. See github.com/chimurai/http-proxy-middleware#http-proxy-optionsNub
I just realized this is an almost 2 year old question but hopefully, if someone else ends up here this information might helpNub
C
16

Try "cookieDomainRewrite": "localhost", as additional setting in proxy conf.

Crore answered 24/3, 2020 at 8:28 Comment(3)
If you ever come to Prague, ping me for free beers!Clearness
This must be marked as correct answer. Also where can I get all list of available options for proxy settings.Aleciaaleck
Try directly at github.com/chimurai/http-proxy-middleware.Crore

© 2022 - 2024 — McMap. All rights reserved.