Files
dancefinder-app/plugins/auth.js
T

139 lines
4.2 KiB
JavaScript
Raw Normal View History

import createAuth0Client from '@auth0/auth0-spa-js'
import { reactive, toRefs } from '@vue/composition-api'
/** Define a default action to perform after authentication */
const DEFAULT_REDIRECT_CALLBACK = () =>
2020-01-25 14:59:14 +01:00
window.history.replaceState({}, document.title, window.location.pathname)
2020-01-25 14:59:14 +01:00
let instance
2020-04-06 10:52:55 +02:00
const params = new URL(window.location).searchParams
const domain = params.get('domain') || 'unbound.eu.auth0.com'
2020-06-21 19:06:41 +02:00
// eslint-disable-next-line import/prefer-default-export
2020-06-21 17:59:14 +02:00
export const useAuth = (onRedirectCallback = DEFAULT_REDIRECT_CALLBACK) => {
if (instance) {
return toRefs(instance)
}
const options = {
2020-01-25 14:59:14 +01:00
domain,
client_id: 'orQfnvCPUR5C3mJkKoiWLQHOVQsBn60e',
audience: 'http://dancefinder.unbound.se',
2020-01-25 14:59:14 +01:00
redirect_uri: window.location.origin
}
instance = reactive({
loading: false,
isAuthenticated: false,
user: {},
auth0Client: null,
popupOpen: false,
error: null,
/** Authenticates the user using a popup window */
loginWithPopup: async o => {
2020-01-25 14:59:14 +01:00
this.popupOpen = true
try {
await instance.auth0Client.then(client => client.loginWithPopup(o))
} catch (e) {
// eslint-disable-next-line
console.error(e)
} finally {
2020-01-25 14:59:14 +01:00
instance.popupOpen = false
}
2020-04-06 10:52:55 +02:00
instance.user = await instance.auth0Client.then(client =>
client.getUser()
)
2020-01-25 14:59:14 +01:00
instance.isAuthenticated = true
},
/** Handles the callback when logging in using a redirect */
handleRedirectCallback: async () => {
2020-01-25 14:59:14 +01:00
instance.loading = true
try {
2020-04-06 10:52:55 +02:00
await instance.auth0Client.then(client =>
client.handleRedirectCallback()
)
instance.user = await instance.auth0Client.then(client =>
client.getUser()
)
2020-01-25 14:59:14 +01:00
instance.isAuthenticated = true
} catch (e) {
2020-01-25 14:59:14 +01:00
instance.error = e
} finally {
2020-01-25 14:59:14 +01:00
instance.loading = false
}
},
/** Authenticates the user using the redirect method */
loginWithRedirect: o => {
return instance.auth0Client.then(client => client.loginWithRedirect(o))
},
/** Returns all the claims present in the ID token */
getIdTokenClaims: o => {
return instance.auth0Client.then(client => client.getIdTokenClaims(o))
},
/** Returns the access token. If the token is invalid or missing, a new one is retrieved */
getTokenSilently: o => {
return instance.auth0Client.then(client => {
return client.getTokenSilently(o)
})
},
/** Gets the access token using a popup window */
getTokenWithPopup: o => {
return instance.auth0Client.then(client => client.getTokenWithPopup(o))
},
/** Logs the user out and removes their session on the authorization server */
logout: o => {
return instance.auth0Client.then(client => client.logout(o))
}
})
const fetchUser = () => {
instance.auth0Client
.then(client => client.isAuthenticated())
.then(a => {
instance.isAuthenticated = a
2020-04-06 10:52:55 +02:00
instance.auth0Client.then(client =>
client.getUser().then(u => {
instance.user = u
instance.loading = false
})
)
2020-01-25 14:59:14 +01:00
})
}
2020-04-06 10:52:55 +02:00
// Create a new instance of the SDK client using members of the given options object
instance.auth0Client = createAuth0Client(options)
2020-04-06 10:52:55 +02:00
instance.auth0Client.then(client => {
instance.loading = true
try {
// If the user is returning to the app after authentication..
if (
window.location.search.includes('code=') &&
window.location.search.includes('state=')
) {
// handle the redirect and retrieve tokens
client
.handleRedirectCallback()
.then(appState => {
// Notify subscribers that the redirect callback has happened, passing the appState
// (useful for retrieving any pre-authentication state)
onRedirectCallback(appState)
// Initialize our internal authentication state
fetchUser()
})
2020-06-21 19:06:41 +02:00
// eslint-disable-next-line no-console
2020-04-06 10:52:55 +02:00
.catch(e => console.error('error handling redirect callback', e))
} else {
fetchUser()
}
2020-04-06 10:52:55 +02:00
} catch (e) {
instance.error = e
} finally {
instance.loading = false
}
})
return toRefs(instance)
}