Created
January 3, 2020 16:15
-
-
Save dwilhel1/701993807d3f2a69a011e9a401da66eb to your computer and use it in GitHub Desktop.
Example workaround for auth0's authGuard with Cypress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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