How to give Auth0Client an access token retrieved from to authenticate application

I am using @auth0/auth0-spa-js in my Vue 2 application, with Cypress. I have authentication working using createAuth0Client. However, I want to use programmatic authentication in Cypress to prevent running a UI-traversing login command every time we run a test, which would make our specs take minutes longer equaling to many, many minutes for a single command for all our suites.

I’ve got a request to Auth0 using a username and password successfully returning an access token, but I don’t know how and I can’t figure out from all Auth0’s documentation, specifically Auth Spa JS, to pass that access_token to my auth0 plugin within Vue to authenticate my test user.

I really don’t want to refactor parts of my application to think it’s authenticated if a token exists while the environment is testing.

Hello @cjohnson welcome to the community!

Have you had a chance to check out the following blogpost? It’s a bit out of date but may still apply in your use case.

Yes, I’ve been over this a few times, but the example repo it uses is using auth-js, which I believe is what’s under the hood of auth-spa-js. I am really trying to avoid refactoring my auth0 integration on the FE as it took a LOT of work and refactoring of other pieces of our app to get it working.

I used this post to do most of the Cypress command refactoring and my code closely matches what’s recommended in this post.

I do know my application fails at this portion of the login tests in the post:

const callbackUrl = ...
cy.visit(callbackUrl, ...

I don’t have anything looking for those url parameters in my auth0Client plugin as it only recommends looking for state and code.

I can also see that even in the auth-spa-js package, it doesn’t really matter what you pass as parameters in a callback URL because handleRedirectCallback only looks for state, code, error, error_description.

My question → is there a way to pass an auth token to the auth-spa-js package to tell it to use the token to authenticate the user or do I need to swap to a different auth0 JS package?

I think my solution is to use auth.js instead of auth-spa-js, as it supplies a webAuth.login() function, which allows a username/password login. I can call this function inside of Cypress.

1 Like

Hey @cjohnson I apologize for the delayed response on this one - Thanks for following here with your solution, I’m sure it will help others in the future :pray:

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