Skip to content

Instantly share code, notes, and snippets.

@brw
Last active January 4, 2018 16:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brw/7f7359d70a5718becd112e6b1e8d3c52 to your computer and use it in GitHub Desktop.
Save brw/7f7359d70a5718becd112e6b1e8d3c52 to your computer and use it in GitHub Desktop.
Authentication with Auth0 using Horizon + Vue.js test
const horizon = Horizon({authType: 'token'})
new Vue({
el: '#app',
data: {
authenticated: false
},
ready() {
this.authenticated = horizon.hasAuthToken()
},
methods: {
signIn() {
if (!this.authenticated) {
horizon.authEndpoint('auth0').subscribe((endpoint) => {
window.location.replace(endpoint)
})
}
},
logUserInfo() {
if (this.authenticated) {
horizon.currentUser().fetch().subscribe((user) => console.log(JSON.stringify(user)))
}
},
signOut() {
Horizon.clearAuthTokens()
window.location.href = '/'
}
}
})
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<script src="/horizon/horizon.js"></script>
<script src="https://npmcdn.com/vue/dist/vue.min.js"></script>
</head>
<body>
<div id="app">
<button v-on:click="signIn()" v-show="!authenticated">Sign In</button>
<button v-on:click="signOut()" v-show="authenticated">Sign Out</button>
<button v-on:click="logUserInfo()" v-show="authenticated">Log User Info</button>
</div>
<script src="./app.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment