Hello!
Thanks in advance for any advice or support.
Use case: Login to our Vue.js app using the auth0-js helper library.
Setup: Auth0’s tutorial for authentication, Cypress.io for front end testing, Following this fantastic auth0 guide by Johnny Reilly.
The guide has got us almost all the way. Here’s the commands.js
Cypress file (The login setup).
const auth0 = require('auth0-js');
Cypress.Commands.add('loginAsAdmin', (overrides = {}) => {
Cypress.log({
name: 'LoginWithAuth0'
});
const webAuth = new auth0.WebAuth({
domain: Cypress.env('auth_url'),
clientID: Cypress.env('auth_client_id'),
responseType: 'token id_token'
});
webAuth.client.login(
{
realm: 'Username-Password-Authentication',
username: Cypress.env('auth_username'),
password: Cypress.env('auth_password'),
audience: Cypress.env('auth_audience'),
scope: 'openid email profile'
},
function(err, authResult) {
window.sessionStorage.setItem('access_token', authResult.accessToken);
window.sessionStorage.setItem('id_token', authResult.idToken);
window.sessionStorage.setItem('expires_at', authResult.expiresIn * 1000 + new Date().getTime());
window.localStorage.setItem('loggedIn', 'true');
} else {
console.error('Problem logging into Auth0', err);
throw err;
}
}
);
});
authResult returns an object with the (I think) correct information:
{
accessToken: <access_token>
expiresIn: 86400
idToken: <id_token>
scope: "openid email profile read:current_user update:current_user_metadata delete:current_user_metadata create:current_user_metadata create:current_user_device_credentials delete:current_user_device_credentials update:current_user_identities"
tokenType: "Bearer"
}
The simple Cypress test using cy.loginAsAdmin();
-
describe('login', () => {
it('should successfully log into our app', () => {
cy.loginAsAdmin();
cy.visit('/dashboard') // only seen whilst logged in.
});
});
But to no luck - it still asks me to log in to the app.
I’ve noticed that the vue.js documentation for auth0 doesn’t mention storing any of the parts that made up authResult earlier in sessionStorage
/localStorage
. So i’m unsure if this is the method to get through the auth using these technologies.
Am I missing something? Is there an alternative flow for a Cypress/Vue mix?
Quick edit: Let me know if there’s any information required to help debug.
Many thanks in advance,
Ryan