Skip to content

Instantly share code, notes, and snippets.

@dwilhel1
Created January 3, 2020 16:15
Show Gist options
  • Save dwilhel1/701993807d3f2a69a011e9a401da66eb to your computer and use it in GitHub Desktop.
Save dwilhel1/701993807d3f2a69a011e9a401da66eb to your computer and use it in GitHub Desktop.
Example workaround for auth0's authGuard with Cypress
import { getInstance } from './index';
// eslint-disable-next-line consistent-return
export const authGuard = (to, from, next) => {
const authService = getInstance();
// eslint-disable-next-line consistent-return
const fn = () => {
// If the user is authenticated, continue with the route
if (authService.isAuthenticated) {
return next();
}
// If Cypress is running, stub authentication
if (window.Cypress) {
authService.isAuthenticated = true;
return next();
}
authService.loginWithRedirect({
appState: { targetUrl: to.fullPath },
redirect_uri: `${window.location.origin}/callback`,
});
};
// If loading has already finished, check our auth state using `fn()`
if (!authService.loading) {
return fn();
}
// Watch for the loading property to change before we check isAuthenticated
// eslint-disable-next-line consistent-return
authService.$watch('loading', (loading) => {
if (loading === false) {
return fn();
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment