# Error: Login Required at requests to API

**URL:** <https://community.auth0.com/t/error-login-required-at-requests-to-api/64577>\
**Category:** Get Help\
**Created:** [June 16, 2021, 10:26am UTC](https://community.auth0.com/t/error-login-required-at-requests-to-api/64577 "2021-06-16T10:26:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![maxhov](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/maxhov/32/30045_2.png) [@maxhov](https://community.auth0.com/u/maxhov)\
**Post date:** [June 16, 2021, 10:26am UTC](https://community.auth0.com/t/error-login-required-at-requests-to-api/64577/1 "2021-06-16T10:26:22Z")

</div>

Hi!

We are piloting with Auth0 to see if it would be a good fit for our application. However, when walking through the [quickstart of Angular](https://auth0.com/docs/quickstart/spa/angular/), we face some issues. We did the usual SO/Google research but none give a definitive answer to fix the issue. There is a range of topics touching upon the issue(s) described, but none give a definitive answer on how to actually fix it.

# Our setup

We have the following setup to test with:

- A vanilla Auth0 tenant in EU with a Free license;
- One SPA registration for the Angular application. This is configured with all defaults as stated in the quickstart;
- One API application for a backend application to test some backend requests. Also same as in the quickstart is explained;
- Testing with browsers: Chrome Version 91.0.4472.106 (Official Build) (x86\_64) and Firefox 89.0 (64-bit) on Mac;
- A demo application that can be found here: [GitHub - maxhov/auth0](https://github.com/maxhov/auth0)

# The issue

Logging in and retrieving user data works as expected. However, when trying to make a backend call that requires a new token to be retrieved (e.g. with a different audience), the application freezes. Upon debugging we found out that the `this.authService.getAccessTokenSilently(options)` call in the `AuthHttpInterceptor` is giving back an “Error: Login required”. This causes the entire authentication context (so to say) to be dropped and the user is required to relogin. The token is never attached to the request.

Steps that we tried (in any combination) without success:

- Enable refresh tokens
- Enable SSL
- Set cacheLocation to `localstorage`
- Relax browser settings as far as possible (Chrome recently dropped some SameSite flags unfortunately…)

# Expected behaviour

As advertised, when calling an api that is configured in the `HttpInterceptor.AllowedList` an access token should be retrieved and added to the request.

# Potential cause

Chrome making mention of SameSite cookie configuration for the following cookies: did\_compact, auth0\_compact when making requests to the /authorize endpoint. The actual issue texts are:

- Indicate whether a cookie is intended to be set in a cross-site context by specifying its SameSite attribute
- Indicate whether to send a cookie in a cross-site request by specifying its SameSite attribute

It seems that the cookies coming from Auth0 are not configured with SameSite=none?

---

<div class="post-metadata">

**Author:** ![stephanie.chamblee](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/stephanie.chamblee/32/26087_2.png) [@stephanie.chamblee](https://community.auth0.com/u/stephanie.chamblee)\
**Post date:** [June 16, 2021, 12:54pm UTC](https://community.auth0.com/t/error-login-required-at-requests-to-api/64577/3 "2021-06-16T12:54:14Z")

</div>

Hi @maxhov,

Welcome to the Community!

When you call `getAccessTokenSilently`, the SDK will retrieve a cached token or perform [silent authentication](https://auth0.com/docs/authorization/configure-silent-authentication) to retrieve a new Access Token. When you include the `audience` parameter, silent authentication will always take place.

The `login_required` error often occurs when the browser blocks third-party cookies during silent authentication.

There are a few ways around this:

1. If you only need to interact with one API, then you can include an audience in your app’s `AuthModule.forRoot` settings and remove the `audience` from the `getAccessTokenSilently` call. You can also configure your app to use [Refresh Token Rotation](https://auth0.com/docs/tokens/refresh-tokens/configure-refresh-token-rotation):

```auto
    AuthModule.forRoot({
      domain: env.auth.domain,
      clientId: env.auth.clientId,
      redirectUri: window.location.origin,
      audience: env.auth.audience,
      useRefreshTokens: true,
      cacheLocation: 'localstorage',
      scope: 'offline_access',
      httpInterceptor: {
        ...env.httpInterceptor,
      },
    }),

```

1. If you need to interact with two APIs, and you control both APIs (the API that your app initiates within its `AuthModule.forRoot` settings and the one which you call `getAccessTokenSilently` for), then you may consider representing multiple APIs in a single logical API so that you only need to include the audience in the `AuthModule.forRoot` settings and not in the `getAccessTokenSilently` call:

> **[Configure Logical API for Multiple APIs](https://auth0.com/docs/get-started/apis/set-logical-api)**
>
> Learn how to use a single logical API in Auth0 to represent and control access to multiple APIs.

1. Use a Custom Domain so that the silent authentication will not be blocked: [Custom Domains](https://auth0.com/docs/custom-domains)

**FAQ:** [Why is authentication lost after refreshing my single page application?](https://community.auth0.com/t/why-is-authentication-lost-after-refreshing-my-single-page-application/56276)  
**Related topic:** [SPA client authentication against multiple APIs - #2 by jmangelo](https://community.auth0.com/t/spa-client-authentication-against-multiple-apis/7491/2)

---

<div class="post-metadata">

**Author:** ![Whitfield258](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/whitfield258/32/12977_2.png) [@Whitfield258](https://community.auth0.com/u/Whitfield258)\
**Post date:** [June 23, 2021, 8:32am UTC](https://community.auth0.com/t/error-login-required-at-requests-to-api/64577/4 "2021-06-23T08:32:16Z")

</div>

A POST is preferable for login request, because the authentication information will be sent in the HTTP messages body rather than the URL. Although it will still be sent plain text, unless you’re encrypting via HTTPS.

GET method data is sent to the server followed by the URL which will be seen to everyone.

Both GET and POST method are used to transfer data from client to server in HTTP protocol but main difference between POST and GET method is that GET carries request parameter appended in URL string, while POST carries request parameter in message body which makes it more secure way of transferring data from client to server in HTTP protocol[.](https://www.myloweslife.win/)

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [July 8, 2021, 8:33am UTC](https://community.auth0.com/t/error-login-required-at-requests-to-api/64577/5 "2021-07-08T08:33:15Z")

</div>

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.
