0

I just started with an angular app using angular-token and a rails back-end with devise-token-auth, I have the next "on-submit" in a sign-in component:

onSubmit() {
    this.output = null;
    this.tokenService.signIn(this.signInData).subscribe(
        res => {
            console.log(res)
            this.output = res;
            this.signInForm.resetForm();
            this.router.navigate(['/products']);
        }, error => {
            this.output = error;
            this.signInForm.resetForm();
        }
    );
}

When checking with Chrome's DevTools, the Sign-In is successful and the back-end returns good credential headers (uid, access-token, client) that I tested with postman. The problem is, when loading the destination page "products", the httpClient call for populating that new page (I put the call in a resolver to not render anything until the "products" call is complete) is not including any auth headers, so of course, I get a 401 non-authorized error, so it appears angular-token is not correctly updating the headers according to the ones I obtained using the .signIn method.

  • How do I make sure the headers are correctly sent?
  • Where do I check if they are actually arriving to the subscribed "res"
    • As it can be seen, I console.logged it and I cannot see any headers in that variable, although I see them in devTools' "network" tab.
Fito
  • 478
  • 4
  • 10

1 Answers1

1

It turns out I just needed to enable headers from the back-end.

In my particular case, I just needed to make sure I exposed the headers with the CORS initializer looking like this:

# config/initializers/cors.rb

 Rails.application.config.middleware.insert_before 0, Rack::Cors do
   allow do
    origins '*'
    resource '*', 
        headers: :any,
        expose: ['access-token', 'expiry', 'token-type', 'uid', 'client'],
        methods: [:get, :post, :put, :patch, :delete, :options]
  end
end

I was missing the "expose" line:

expose: ['access-token', 'expiry', 'token-type', 'uid', 'client'],
Fito
  • 478
  • 4
  • 10