forked from ytgov/signing-authority
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauth0-plugin.js
More file actions
133 lines (112 loc) · 3.18 KB
/
auth0-plugin.js
File metadata and controls
133 lines (112 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* External Modules
*/
import Vue from "vue";
import { createAuth0Client } from "@auth0/auth0-spa-js";
import { secureDelete, secureGet, securePut, securePost } from "@/store/jwt";
import { getAuthConfig } from "./getAuthConfig";
import { apiConfigUrl } from "@/config";
/**
* Vue.js Instance Definition
*/
let instance;
export const getInstance = () => instance;
/**
* Vue.js Instance Initialization
*/
export const useAuth0 = ({
onRedirectCallback = () => {
window.history.replaceState({}, document.title, window.location.pathname);
},
redirectUri = window.location.origin,
...pluginOptions //eslint-disable-line no-unused-vars
}) => {
if (instance) return instance;
instance = new Vue({
data() {
return {
auth0Client: null,
isLoading: true,
isAuthenticated: false,
user: {},
error: null,
options: {},
targetUrl: undefined,
};
},
methods: {
async handleRedirectCallback() {
this.isLoading = true;
console.log("REDIRECT CALLBACK: ", this.redirectUri);
try {
await this.auth0Client.handleRedirectCallback();
this.user = await this.auth0Client.getUser();
this.isAuthenticated = true;
} catch (error) {
this.error = error;
} finally {
this.isLoading = false;
}
},
loginWithRedirect(options) {
return this.auth0Client.loginWithRedirect(options);
},
logout(options) {
return this.auth0Client.logout(options);
},
getTokenSilently(o) {
return this.auth0Client.getTokenSilently(o);
},
get(url) {
return secureGet(url);
},
put(url, body, config) {
return securePut(url, body, config);
},
post(url, body, config) {
return securePost(url, body, config);
},
delete(url) {
return secureDelete(url);
},
},
async created() {
this.options = await getAuthConfig(apiConfigUrl);
this.auth0Client = await createAuth0Client({
//...this.options,
clientId: this.options.clientId,
domain: this.options.domain,
// ...pluginOptions,
authorizationParams: {
redirect_uri: redirectUri,
audience: this.options.audience,
},
});
try {
if (window.location.search.includes("code=") && window.location.search.includes("state=")) {
const { appState } = await this.auth0Client.handleRedirectCallback();
if (appState && appState.targetUrl) this.targetUrl = appState.targetUrl;
onRedirectCallback(appState);
}
} catch (error) {
this.error = error;
} finally {
this.isAuthenticated = await this.auth0Client.isAuthenticated();
this.user = await this.auth0Client.getUser();
//set the access token in the auth store
//await this.getTokenSilently();
//store.commit("auth/setToken", token);
this.isLoading = false;
}
},
});
return instance;
};
/**
* Vue.js Plugin Definition
*/
export const Auth0Plugin = {
install(Vue, options) {
Vue.prototype.$auth = useAuth0(options);
},
};