Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing user's auth0 access token on next-js client. #734

Closed
saidybarry opened this issue Jun 18, 2022 · 2 comments
Closed

Accessing user's auth0 access token on next-js client. #734

saidybarry opened this issue Jun 18, 2022 · 2 comments

Comments

@saidybarry
Copy link

Hi Guys, I am currently working on building a multi-vendor web app with next-js as the client, auth0 as the authentication and authorization provider and Apollo graphQL server for my API/data-layer.

Issue -
I am having difficulties in accessing the users access token to send it along any api request via authorization headers for user authorization on Apollo server.

Any help is appreciated.

@adamjmcgrath
Copy link
Contributor

adamjmcgrath commented Jun 20, 2022

Hi @saidybarry - thanks for raising this

This has been discussed and answered in #277, #201 and #67

Take a look at the answers in those and feel free to raise another issue if you have a specific problem with this (be sure to use the issue template)

@saidybarry
Copy link
Author

saidybarry commented Jun 20, 2022

Hello @adamjmcgrath thanks for getting back to me on this, I am literally going crazy on this I have search endlessly for a solution to the problem.

I have implemented the suggestions made on these threads about having a next api to get the access token and then invoke it to get an access token to pass it as authorization header. These are the issues I am facing currently -

  • If the API route is secured with withApiAuthRequired I can't access it even when user is login.
  • If the API route is not protected I get an error " AccessTokenError - user does not have a valid session ".

I would really appreciate it if you could help me with these issue I would be even happy to hop on a call with you if you are available. I am in desperate need.

Code Snippet -

import auth0 from '../../utils/auth0';

export default auth0.withApiAuthRequired(async function requestAccessToken(req, res) {
try {

const { accessToken } = await auth0.getAccessToken(req, res, {
  refresh: true,
  scopes: ['openid profile email offline_access'],
});

res.status(200).json({ accessToken });

} catch(error) {
console.error(error);
res.status(error.status || 500).end(error.message)
}
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants