chore: add eslint and fix lint errors
This commit is contained in:
+55
-59
@@ -3,23 +3,23 @@ import { reactive, toRefs } from '@vue/composition-api'
|
||||
|
||||
/** Define a default action to perform after authentication */
|
||||
const DEFAULT_REDIRECT_CALLBACK = () =>
|
||||
window.history.replaceState({}, document.title, window.location.pathname);
|
||||
window.history.replaceState({}, document.title, window.location.pathname)
|
||||
|
||||
let instance;
|
||||
let instance
|
||||
|
||||
const params = (new URL(window.location)).searchParams
|
||||
const params = new URL(window.location).searchParams
|
||||
const domain = params.get('domain') || 'unbound.eu.auth0.com'
|
||||
|
||||
export const useAuth = (onRedirectCallback = DEFAULT_REDIRECT_CALLBACK) => {
|
||||
export default (onRedirectCallback = DEFAULT_REDIRECT_CALLBACK) => {
|
||||
if (instance) {
|
||||
return toRefs(instance)
|
||||
}
|
||||
|
||||
const options = {
|
||||
domain: domain,
|
||||
domain,
|
||||
client_id: 'orQfnvCPUR5C3mJkKoiWLQHOVQsBn60e',
|
||||
audience: 'http://dancefinder.unbound.se',
|
||||
redirect_uri: window.location.origin,
|
||||
redirect_uri: window.location.origin
|
||||
}
|
||||
|
||||
instance = reactive({
|
||||
@@ -31,95 +31,91 @@ export const useAuth = (onRedirectCallback = DEFAULT_REDIRECT_CALLBACK) => {
|
||||
error: null,
|
||||
/** Authenticates the user using a popup window */
|
||||
loginWithPopup: async o => {
|
||||
this.popupOpen = true;
|
||||
this.popupOpen = true
|
||||
|
||||
try {
|
||||
await instance.auth0Client.loginWithPopup(o);
|
||||
await instance.auth0Client.loginWithPopup(o)
|
||||
} catch (e) {
|
||||
// eslint-disable-next-line
|
||||
console.error(e);
|
||||
} finally {
|
||||
instance.popupOpen = false;
|
||||
instance.popupOpen = false
|
||||
}
|
||||
|
||||
instance.user = await instance.auth0Client.getUser();
|
||||
instance.isAuthenticated = true;
|
||||
instance.user = await instance.auth0Client.getUser()
|
||||
instance.isAuthenticated = true
|
||||
},
|
||||
/** Handles the callback when logging in using a redirect */
|
||||
handleRedirectCallback: async () => {
|
||||
instance.loading = true;
|
||||
instance.loading = true
|
||||
try {
|
||||
await instance.auth0Client.handleRedirectCallback();
|
||||
instance.user = await instance.auth0Client.getUser();
|
||||
instance.isAuthenticated = true;
|
||||
await instance.auth0Client.handleRedirectCallback()
|
||||
instance.user = await instance.auth0Client.getUser()
|
||||
instance.isAuthenticated = true
|
||||
} catch (e) {
|
||||
instance.error = e;
|
||||
instance.error = e
|
||||
} finally {
|
||||
instance.loading = false;
|
||||
instance.loading = false
|
||||
}
|
||||
},
|
||||
/** Authenticates the user using the redirect method */
|
||||
loginWithRedirect: o => {
|
||||
return instance.auth0Client.loginWithRedirect(o);
|
||||
return instance.auth0Client.loginWithRedirect(o)
|
||||
},
|
||||
/** Returns all the claims present in the ID token */
|
||||
getIdTokenClaims: o => {
|
||||
return instance.auth0Client.getIdTokenClaims(o);
|
||||
return instance.auth0Client.getIdTokenClaims(o)
|
||||
},
|
||||
/** Returns the access token. If the token is invalid or missing, a new one is retrieved */
|
||||
getTokenSilently: o => {
|
||||
return instance.auth0Client.getTokenSilently(o);
|
||||
return instance.auth0Client.getTokenSilently(o)
|
||||
},
|
||||
/** Gets the access token using a popup window */
|
||||
getTokenWithPopup: o => {
|
||||
return instance.auth0Client.getTokenWithPopup(o);
|
||||
return instance.auth0Client.getTokenWithPopup(o)
|
||||
},
|
||||
/** Logs the user out and removes their session on the authorization server */
|
||||
logout: o => {
|
||||
return instance.auth0Client.logout(o);
|
||||
return instance.auth0Client.logout(o)
|
||||
}
|
||||
})
|
||||
|
||||
const fetchUser = () => {
|
||||
instance.auth0Client.isAuthenticated()
|
||||
.then(a => {
|
||||
instance.isAuthenticated = a
|
||||
instance.auth0Client.getUser()
|
||||
.then(u => {
|
||||
instance.user = u
|
||||
instance.loading = false;
|
||||
});
|
||||
});
|
||||
}
|
||||
// Create a new instance of the SDK client using members of the given options object
|
||||
createAuth0Client(options)
|
||||
.then(client => {
|
||||
instance.loading = true
|
||||
instance.auth0Client = client
|
||||
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
|
||||
instance.auth0Client.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()
|
||||
})
|
||||
} else {
|
||||
fetchUser()
|
||||
}
|
||||
} catch (e) {
|
||||
instance.error = e;
|
||||
} finally {
|
||||
instance.auth0Client.isAuthenticated().then(a => {
|
||||
instance.isAuthenticated = a
|
||||
instance.auth0Client.getUser().then(u => {
|
||||
instance.user = u
|
||||
instance.loading = false
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
// Create a new instance of the SDK client using members of the given options object
|
||||
createAuth0Client(options).then(client => {
|
||||
instance.loading = true
|
||||
instance.auth0Client = client
|
||||
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
|
||||
instance.auth0Client.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()
|
||||
})
|
||||
} else {
|
||||
fetchUser()
|
||||
}
|
||||
} catch (e) {
|
||||
instance.error = e
|
||||
} finally {
|
||||
instance.loading = false
|
||||
}
|
||||
})
|
||||
|
||||
return toRefs(instance)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user