Failed to load resource: the server responded with a status of 401 () in React.js/Nest.js

Hello webdev newbie here, i have seen this issue before in getting Failed to load resource: the server responded with a status of 401 () in /oauth/token.

My frontend is in React.js and backend is with Nest.js, it does work without issue if i set up the auth method ‘none’ but i’d like to my set up on Clinet Secret (Basic), any help would be appreciated!

// auth.strategy.ts
@Injectable()
export class Auth0Strategy extends PassportStrategy(Strategy, 'auth0') {
  constructor() {
    super({
      clientID: process.env.AUTH0_CLIENT_ID,
      clientSecret: process.env.AUTH0_CLIENT_SECRET,
      domain: process.env.AUTH0_DOMAIN,
      callbackURL: process.env.AUTH0_CALLBACK_URL,
      scope: 'openid profile email',
      state: false
    });
  }
  async validate(accessToken, refreshToken, extraParams, profile, done): Promise<any> {
    return done(null, profile);
  }
}
// auth.guard.ts
@Injectable()
export class Auth0Guard extends AuthGuard('auth0') {}
async validate(accessToken, refreshToken, extraParams, profile, done): Promise<any> {
    return done(null, profile);
  }
}
import React, { useEffect } from 'react';
import { useAuth0 } from '@auth0/auth0-react';
import { getAllFeed } from '../api/asset';

const Feed = () => {
  const { user, isAuthenticated, getAccessTokenSilently } = useAuth0();

  if (!isAuthenticated) {
    return <div>Loading or not authenticated...</div>;
  }

  return (
    <div>
      <h2>User Profile</h2>
      {/* Display user's info */}
      <p>Name: {user.name}</p>
      <p>Email: {user.email}</p>
      {/* ... other user information */}
    </div>
  );
};

export default Feed;

Hey there @ludovicoc welcome to the community!

I am guessing the clientID in question has the token_endpoint_auth_method set to none which won’t allow you to perform a client credentials exchange. You’ll need to update the token_endpoint_auth_method to client_secret_basic using the Management API.

1 Like

hey @tyf thanks for the quick reply, i did exactly that and I’m getting the error I mentioned above, when it’s set ‘none’ everything works smoothly. Am I missing something in order to make it work with client secret (basic) ?

1 Like

Hey @ludovicoc no problem, happy to help!

Do you mind DMing me the tenant and specific client_id you are working with so I can take a closer look at your settings?

1 Like

Sure, just sent it over :slight_smile:

1 Like

Awesome, thanks!

Can you share a bit more detail regarding where/how specifically you are running into this error? I guess I’d also like to learn why the need to use client secret (basic) in the context of your application. Typically, there is no need for a client credentials exchange in a user authentication/authorization flow but rather when authenticating the client itself against a resource (think machine to machine).

The more information you can provide about your specific use case the better!

yes of course I DM’you with more details, thanks again

1 Like

Resolved in a DM as additional context was needed :smile:

1 Like

Thanks again @tyf this was great and super helpful!

1 Like

No problem @ludovicoc I’m happy I was able to help!

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