How can i get access token to auth my custom api

How can i get access token to auth my custom api like getAccessTokenSilenty() does in useAuth0()

Hi @pandeysumit832,

Do you need the Access Token for your custom API in your frontend app? If so, you can use the identifier of your API that is registered with Auth0 and use this as the audience in your frontend app.

yeah doing the same but the issue is i want to store it somewhere so that i don’t need to access it again and again but the problem is that every time my react app runs the function is to get called is there any way i can check that the token is valid or not then call the api again if not valid

Are you using the Auth0 Rect SDK? Auth0 React SDK Quickstarts: Login

The SDK will handle the caching for you so that your app does not make unnecessary auth requests.

Here is an article on token storage using the SDK: Token Storage

nope bcoz i had to use my own dedicated login page i am using normal webauth package

I see! In that case, I’d recommend taking a look at this migration guide to compare auth0.js withauth0-spa-js: Migrate from Auth0.js to the Auth0 Single Page App SDK

The equivalent implementation for getAccessTokenSilenty() in auth0-js would be checkSession:

auth0.checkSession({}, function(err, authResult) {
    // Authentication tokens or error
  });

can i use my own dedicated login page

Universal Login is recommended for security and other reasons, however, you can set up an embedded login instead of Universal Login. When using an embedded login, it’s best to use a Custom Domain so that you don’t run into cross-origin authentication issues. Here is more info about setting up an embedded login:

I tired check session dosent work for me

Here are some ready-to-go examples of auth0-js:

Let me know if you are seeing a specific error when you try these out!

i m passing my custom aud to check session but its saying undefined

Would you mind sharing a code snippet?

Here is the full documentation for the checkSession method: JSDoc: Global

const token = () => {

        webAuth.checkSession(

            {

                audience: https://quickstart/api,

                scope: "read:message",

            },

            function (err, authResult) {

                if (err) {

                    return err;

                }

                return authResult;

            }

        );

    };

It looks like you need quotation marks around https://quickstart/api:

audience: "https://quickstart/api",

Would you mind sharing the specific error message? Also, if you look in your tenant logs, are you seeing any errors?

Yeup I tried doing that no errors when I m trying to console log it return undefined

Just to clarify, is it the token var that is logging as undefined?

there is an error in the logs saying consent required

Based on the docs, it looks like this error will be thrown when running the app on localhost when the following scopes are not requested: openid profile email

Are you requesting these scopes?

https://auth0.com/docs/libraries/auth0js#scope

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