vuethenticate 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -0
- package/dist/composables/useAuth.d.ts.map +1 -1
- package/dist/index.esm.js +187 -167
- package/dist/index.umd.js +1 -1
- package/dist/types/auth.d.ts +1 -0
- package/dist/types/auth.d.ts.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
@@ -237,6 +237,7 @@ The main composable for authentication state management.
|
|
237
237
|
| `signOut` | `(state?: TState) => Promise<void>` | Sign out user with optional state |
|
238
238
|
| `silentRenew` | `() => Promise<void>` | Manually renew token |
|
239
239
|
| `clearError` | `() => void` | Clear current error |
|
240
|
+
| `cleanup` | `() => void` | Manual cleanup of UserManager resources (rarely needed) |
|
240
241
|
|
241
242
|
### `<AuthCallback>`
|
242
243
|
|
@@ -291,6 +292,26 @@ function handleSilentError(error) {
|
|
291
292
|
|
292
293
|
> **Note**: This component is primarily used in an iframe or popup for silent token renewal. It should be placed on a minimal page with no other content.
|
293
294
|
|
295
|
+
## Advanced Usage
|
296
|
+
|
297
|
+
### Manual Cleanup
|
298
|
+
|
299
|
+
The `useAuth` composable includes a `cleanup()` function for manual resource cleanup. **This is rarely needed** as the library manages resources automatically.
|
300
|
+
|
301
|
+
```typescript
|
302
|
+
const auth = useAuth(config);
|
303
|
+
|
304
|
+
// Only use in special scenarios like testing or custom cleanup requirements
|
305
|
+
auth.cleanup();
|
306
|
+
```
|
307
|
+
|
308
|
+
**When you might need cleanup:**
|
309
|
+
- Unit testing scenarios where you need to reset state between tests
|
310
|
+
- Dynamic configuration changes (though creating a new instance is usually better)
|
311
|
+
- Custom cleanup logic in very specific edge cases
|
312
|
+
|
313
|
+
**Note:** The cleanup function removes event listeners and clears the UserManager instance from the internal registry. Normal application usage should never require calling this function.
|
314
|
+
|
294
315
|
## License
|
295
316
|
|
296
317
|
MIT
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"useAuth.d.ts","sourceRoot":"","sources":["../../src/composables/useAuth.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAC/D,OAAO,
|
1
|
+
{"version":3,"file":"useAuth.d.ts","sourceRoot":"","sources":["../../src/composables/useAuth.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAC/D,OAAO,EAAoB,KAAK,GAAG,EAAwB,MAAM,KAAK,CAAC;AACvE,OAAO,EAAE,KAAK,IAAI,EAAe,MAAM,gBAAgB,CAAC;AAwNxD;;;;GAIG;AACH,eAAO,MAAM,OAAO,GAAI,MAAM,GAAG,OAAO,EACpC,QAAQ,UAAU,KACnB,aAAa,CAAC,MAAM,CAsNtB,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,gBAAgB,GAAI,MAAM,GAAG,OAAO;4BAenC,MAAM,KACb,OAAO,CAAC;QAAE,IAAI,EAAE,IAAI,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;kCAiBC,MAAM,KAAG,OAAO,CAAC,IAAI,CAAC;;;CAapE,CAAC"}
|
package/dist/index.esm.js
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
import {
|
2
|
-
import { WebStorageStateStore as
|
3
|
-
function
|
1
|
+
import { ref as m, computed as I, watch as W, defineComponent as M, onMounted as R, createElementBlock as _, openBlock as L, Fragment as $, renderSlot as b, createCommentVNode as C, unref as F, createElementVNode as S, toDisplayString as P } from "vue";
|
2
|
+
import { WebStorageStateStore as j, UserManager as G } from "oidc-client-ts";
|
3
|
+
function H(e) {
|
4
4
|
const r = typeof window < "u" ? window.location.origin : "";
|
5
5
|
return {
|
6
6
|
authority: e.authority,
|
@@ -10,7 +10,7 @@ function O(e) {
|
|
10
10
|
redirect_uri: e.redirectUri ?? `${r}/auth/callback`,
|
11
11
|
scope: e.scope ?? "openid profile",
|
12
12
|
response_type: e.responseType ?? "code",
|
13
|
-
userStore: e.storage ? new
|
13
|
+
userStore: e.storage ? new j({ store: J(e.storage) }) : void 0,
|
14
14
|
automaticSilentRenew: e.automaticSilentRenew ?? !0,
|
15
15
|
silent_redirect_uri: e.silentRedirectUri ?? `${r}/auth/silent-callback`,
|
16
16
|
post_logout_redirect_uri: e.postLogoutRedirectUri ?? r,
|
@@ -18,21 +18,21 @@ function O(e) {
|
|
18
18
|
accessTokenExpiringNotificationTimeInSeconds: 60
|
19
19
|
};
|
20
20
|
}
|
21
|
-
function
|
21
|
+
function J(e) {
|
22
22
|
if (typeof window > "u")
|
23
|
-
return
|
23
|
+
return T();
|
24
24
|
switch (e) {
|
25
25
|
case "localStorage":
|
26
26
|
return window.localStorage;
|
27
27
|
case "sessionStorage":
|
28
28
|
return window.sessionStorage;
|
29
29
|
case "memory":
|
30
|
-
return
|
30
|
+
return T();
|
31
31
|
default:
|
32
32
|
return window.localStorage;
|
33
33
|
}
|
34
34
|
}
|
35
|
-
function
|
35
|
+
function T() {
|
36
36
|
const e = /* @__PURE__ */ new Map();
|
37
37
|
return {
|
38
38
|
getItem: (r) => e.get(r) ?? null,
|
@@ -45,37 +45,51 @@ function C() {
|
|
45
45
|
key: (r) => Array.from(e.keys())[r] ?? null
|
46
46
|
};
|
47
47
|
}
|
48
|
-
const
|
48
|
+
const E = /* @__PURE__ */ new Map(), Q = (e) => {
|
49
|
+
try {
|
50
|
+
const { userManager: r, eventCallbacks: t } = e;
|
51
|
+
t.userLoaded && r.events.removeUserLoaded(t.userLoaded), t.userUnloaded && r.events.removeUserUnloaded(t.userUnloaded), t.accessTokenExpired && r.events.removeAccessTokenExpired(
|
52
|
+
t.accessTokenExpired
|
53
|
+
), t.accessTokenExpiring && r.events.removeAccessTokenExpiring(
|
54
|
+
t.accessTokenExpiring
|
55
|
+
), t.silentRenewError && r.events.removeSilentRenewError(
|
56
|
+
t.silentRenewError
|
57
|
+
), r.stopSilentRenew(), r.clearStaleState();
|
58
|
+
} catch (r) {
|
59
|
+
console.warn("Error during UserManager cleanup:", r);
|
60
|
+
}
|
61
|
+
}, K = (e) => `${e.authority}_${e.clientId}`, X = () => E.size === 0 ? null : (E.size === 1 || console.warn(
|
49
62
|
"Multiple auth instances detected. Callback components may not work as expected. Consider using a single auth instance or specify config explicitly."
|
50
|
-
), Array.from(
|
51
|
-
const r =
|
63
|
+
), Array.from(E.values())[0] || null), Y = (e) => {
|
64
|
+
const r = K(e), t = E.get(r);
|
52
65
|
if (t)
|
53
66
|
return t;
|
54
|
-
const
|
67
|
+
const l = Z(e), a = m(!1), o = m(!1), n = m(null), s = m(!1), p = m(null), y = I(() => !!n.value && !n.value.expired), d = I(() => {
|
55
68
|
var h;
|
56
|
-
return ((h =
|
57
|
-
}),
|
69
|
+
return ((h = n.value) == null ? void 0 : h.access_token) || null;
|
70
|
+
}), w = I(() => {
|
58
71
|
var h;
|
59
|
-
return !!((h =
|
60
|
-
}),
|
61
|
-
userManager:
|
62
|
-
isInitialized:
|
72
|
+
return !!((h = n.value) != null && h.expired);
|
73
|
+
}), k = {
|
74
|
+
userManager: l,
|
75
|
+
isInitialized: a,
|
63
76
|
isInitializing: o,
|
64
|
-
user:
|
65
|
-
isLoading:
|
66
|
-
error:
|
67
|
-
isAuthenticated:
|
68
|
-
accessToken:
|
69
|
-
isExpired:
|
77
|
+
user: n,
|
78
|
+
isLoading: s,
|
79
|
+
error: p,
|
80
|
+
isAuthenticated: y,
|
81
|
+
accessToken: d,
|
82
|
+
isExpired: w,
|
83
|
+
eventCallbacks: {}
|
70
84
|
};
|
71
|
-
return
|
72
|
-
},
|
73
|
-
const r =
|
74
|
-
return new
|
75
|
-
},
|
76
|
-
const
|
77
|
-
return t.value =
|
78
|
-
},
|
85
|
+
return E.set(r, k), k;
|
86
|
+
}, Z = (e) => {
|
87
|
+
const r = H(e);
|
88
|
+
return new G(r);
|
89
|
+
}, f = (e, r, t, l) => {
|
90
|
+
const a = e instanceof Error ? e : new Error(r);
|
91
|
+
return t.value = a, l == null || l(a), a;
|
92
|
+
}, ee = (e) => {
|
79
93
|
var r, t;
|
80
94
|
if (!((r = e.authority) != null && r.trim()))
|
81
95
|
throw new Error("AuthConfig: authority is required");
|
@@ -86,162 +100,168 @@ const p = /* @__PURE__ */ new Map(), T = (e) => `${e.authority}_${e.clientId}`,
|
|
86
100
|
} catch {
|
87
101
|
throw new Error("AuthConfig: authority must be a valid URL");
|
88
102
|
}
|
89
|
-
},
|
103
|
+
}, z = async (e, r = 1e4) => {
|
90
104
|
if (!e.isInitialized.value)
|
91
|
-
return new Promise((t,
|
92
|
-
const
|
93
|
-
|
94
|
-
}, r), o =
|
105
|
+
return new Promise((t, l) => {
|
106
|
+
const a = setTimeout(() => {
|
107
|
+
l(new Error("Authentication initialization timeout"));
|
108
|
+
}, r), o = W(
|
95
109
|
e.isInitialized,
|
96
|
-
(
|
97
|
-
|
110
|
+
(n) => {
|
111
|
+
n && (clearTimeout(a), o(), t());
|
98
112
|
},
|
99
113
|
{ immediate: !0 }
|
100
114
|
);
|
101
115
|
});
|
102
|
-
},
|
103
|
-
|
104
|
-
const r =
|
116
|
+
}, ne = (e) => {
|
117
|
+
ee(e);
|
118
|
+
const r = Y(e), {
|
105
119
|
userManager: t,
|
106
|
-
isInitialized:
|
107
|
-
isInitializing:
|
120
|
+
isInitialized: l,
|
121
|
+
isInitializing: a,
|
108
122
|
user: o,
|
109
|
-
isLoading:
|
110
|
-
error:
|
111
|
-
isAuthenticated:
|
112
|
-
accessToken:
|
113
|
-
isExpired:
|
114
|
-
} = r,
|
115
|
-
if (!(
|
123
|
+
isLoading: n,
|
124
|
+
error: s,
|
125
|
+
isAuthenticated: p,
|
126
|
+
accessToken: y,
|
127
|
+
isExpired: d
|
128
|
+
} = r, w = K(e), k = async () => {
|
129
|
+
if (!(a.value || l.value))
|
116
130
|
try {
|
117
|
-
|
118
|
-
const
|
119
|
-
if (
|
120
|
-
if (!
|
121
|
-
o.value =
|
131
|
+
a.value = !0, n.value = !0, s.value = null;
|
132
|
+
const c = await t.getUser();
|
133
|
+
if (c)
|
134
|
+
if (!c.expired)
|
135
|
+
o.value = c;
|
122
136
|
else if (e.automaticSilentRenew !== !1)
|
123
137
|
try {
|
124
|
-
await
|
125
|
-
} catch (
|
138
|
+
await x();
|
139
|
+
} catch (i) {
|
126
140
|
console.warn(
|
127
141
|
"Silent renewal failed during initialization:",
|
128
|
-
|
142
|
+
i
|
129
143
|
), await t.removeUser(), o.value = null;
|
130
144
|
}
|
131
145
|
else
|
132
146
|
await t.removeUser(), o.value = null;
|
133
|
-
} catch (
|
134
|
-
|
135
|
-
|
147
|
+
} catch (c) {
|
148
|
+
f(
|
149
|
+
c,
|
136
150
|
"Failed to initialize authentication",
|
137
|
-
|
151
|
+
s,
|
138
152
|
e.onError
|
139
153
|
);
|
140
154
|
} finally {
|
141
|
-
|
155
|
+
n.value = !1, a.value = !1, l.value = !0;
|
142
156
|
}
|
143
|
-
}, h = async (
|
157
|
+
}, h = async (c) => {
|
144
158
|
try {
|
145
|
-
|
146
|
-
} catch (
|
147
|
-
|
159
|
+
n.value = !0, s.value = null, await t.signinRedirect({ state: c });
|
160
|
+
} catch (i) {
|
161
|
+
f(i, "Sign in failed", s, e.onError);
|
148
162
|
} finally {
|
149
|
-
|
163
|
+
n.value = !1;
|
150
164
|
}
|
151
|
-
},
|
165
|
+
}, q = async (c) => {
|
152
166
|
try {
|
153
|
-
|
154
|
-
} catch (
|
155
|
-
|
167
|
+
n.value = !0, s.value = null, o.value = null, await t.signoutRedirect({ state: c });
|
168
|
+
} catch (i) {
|
169
|
+
f(i, "Sign out failed", s, e.onError);
|
156
170
|
} finally {
|
157
|
-
|
171
|
+
n.value = !1;
|
158
172
|
}
|
159
|
-
},
|
173
|
+
}, x = async () => {
|
160
174
|
try {
|
161
|
-
|
162
|
-
const
|
163
|
-
o.value =
|
164
|
-
} catch (
|
165
|
-
throw
|
166
|
-
|
175
|
+
n.value = !0, s.value = null;
|
176
|
+
const c = await t.signinSilent();
|
177
|
+
o.value = c;
|
178
|
+
} catch (c) {
|
179
|
+
throw f(
|
180
|
+
c,
|
167
181
|
"Silent renewal failed",
|
168
|
-
|
182
|
+
s,
|
169
183
|
e.onError
|
170
184
|
);
|
171
185
|
} finally {
|
172
|
-
|
186
|
+
n.value = !1;
|
173
187
|
}
|
174
|
-
},
|
175
|
-
|
176
|
-
},
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
188
|
+
}, B = () => {
|
189
|
+
s.value = null;
|
190
|
+
}, O = (c, i) => {
|
191
|
+
const { userManager: g, user: A, error: D, eventCallbacks: v } = c;
|
192
|
+
v.userLoaded = (u) => {
|
193
|
+
var U;
|
194
|
+
A.value = u, (U = i.onUserLoaded) == null || U.call(i, u);
|
195
|
+
}, v.userUnloaded = () => {
|
196
|
+
var u;
|
197
|
+
A.value = null, (u = i.onUserUnloaded) == null || u.call(i);
|
198
|
+
}, v.accessTokenExpired = () => {
|
199
|
+
var u;
|
200
|
+
(u = i.onAccessTokenExpired) == null || u.call(i);
|
201
|
+
}, v.accessTokenExpiring = () => {
|
202
|
+
var u;
|
203
|
+
(u = i.onAccessTokenExpiring) == null || u.call(i);
|
204
|
+
}, v.silentRenewError = async (u) => {
|
205
|
+
console.warn("Silent renewal failed:", u);
|
191
206
|
try {
|
192
|
-
await
|
193
|
-
} catch (
|
207
|
+
await g.removeUser(), A.value = null;
|
208
|
+
} catch (U) {
|
194
209
|
console.error(
|
195
210
|
"Failed to remove user after silent renewal error:",
|
196
|
-
|
211
|
+
U
|
197
212
|
);
|
198
213
|
}
|
199
|
-
|
200
|
-
|
214
|
+
f(
|
215
|
+
u,
|
201
216
|
"Silent renewal failed",
|
202
|
-
|
203
|
-
|
217
|
+
D,
|
218
|
+
i.onError
|
204
219
|
);
|
205
|
-
})
|
220
|
+
}, g.events.addUserLoaded(v.userLoaded), g.events.addUserUnloaded(v.userUnloaded), g.events.addAccessTokenExpired(
|
221
|
+
v.accessTokenExpired
|
222
|
+
), g.events.addAccessTokenExpiring(
|
223
|
+
v.accessTokenExpiring
|
224
|
+
), g.events.addSilentRenewError(v.silentRenewError);
|
225
|
+
}, V = () => {
|
226
|
+
Q(r), E.delete(w);
|
206
227
|
};
|
207
|
-
return !
|
208
|
-
p.delete(c);
|
209
|
-
}), {
|
228
|
+
return !l.value && !a.value && (k(), O(r, e)), {
|
210
229
|
user: o,
|
211
|
-
isAuthenticated:
|
212
|
-
isLoading:
|
213
|
-
error:
|
214
|
-
accessToken:
|
215
|
-
isExpired:
|
230
|
+
isAuthenticated: p,
|
231
|
+
isLoading: n,
|
232
|
+
error: s,
|
233
|
+
accessToken: y,
|
234
|
+
isExpired: d,
|
216
235
|
signIn: h,
|
217
|
-
signOut:
|
218
|
-
silentRenew:
|
219
|
-
clearError:
|
236
|
+
signOut: q,
|
237
|
+
silentRenew: x,
|
238
|
+
clearError: B,
|
239
|
+
cleanup: V
|
220
240
|
};
|
221
|
-
},
|
222
|
-
const e =
|
241
|
+
}, N = () => {
|
242
|
+
const e = X();
|
223
243
|
if (!e)
|
224
244
|
throw new Error(
|
225
245
|
"Authentication not initialized. Make sure useAuth() is called in a parent component first."
|
226
246
|
);
|
227
247
|
return {
|
228
|
-
processCallback: async (
|
229
|
-
await
|
230
|
-
const
|
248
|
+
processCallback: async (l) => {
|
249
|
+
await z(e);
|
250
|
+
const a = l || window.location.href, o = await e.userManager.signinRedirectCallback(a);
|
231
251
|
return {
|
232
252
|
user: o,
|
233
253
|
state: o.state
|
234
254
|
};
|
235
255
|
},
|
236
|
-
processSilentCallback: async (
|
237
|
-
await
|
238
|
-
const
|
239
|
-
await e.userManager.signinSilentCallback(
|
256
|
+
processSilentCallback: async (l) => {
|
257
|
+
await z(e);
|
258
|
+
const a = l || window.location.href;
|
259
|
+
await e.userManager.signinSilentCallback(a);
|
240
260
|
},
|
241
261
|
isInitialized: e.isInitialized,
|
242
262
|
isInitializing: e.isInitializing
|
243
263
|
};
|
244
|
-
},
|
264
|
+
}, ae = /* @__PURE__ */ M({
|
245
265
|
__name: "AuthCallback",
|
246
266
|
props: {
|
247
267
|
onSuccess: { type: Function },
|
@@ -249,67 +269,67 @@ const p = /* @__PURE__ */ new Map(), T = (e) => `${e.authority}_${e.clientId}`,
|
|
249
269
|
},
|
250
270
|
emits: ["success", "error"],
|
251
271
|
setup(e, { emit: r }) {
|
252
|
-
const t = e,
|
253
|
-
var
|
272
|
+
const t = e, l = r, { processCallback: a, isInitializing: o } = N(), n = m(!0), s = m(null), p = async () => {
|
273
|
+
var y, d;
|
254
274
|
try {
|
255
|
-
const
|
256
|
-
(
|
257
|
-
} catch (
|
258
|
-
const
|
259
|
-
|
275
|
+
const w = await a();
|
276
|
+
(y = t.onSuccess) == null || y.call(t, w.user, w.state), l("success", w.user, w.state);
|
277
|
+
} catch (w) {
|
278
|
+
const k = w instanceof Error ? w : new Error("Callback processing failed");
|
279
|
+
s.value = k, (d = t.onError) == null || d.call(t, k), l("error", k);
|
260
280
|
} finally {
|
261
|
-
|
281
|
+
n.value = !1;
|
262
282
|
}
|
263
283
|
};
|
264
|
-
return
|
265
|
-
|
266
|
-
}), (
|
267
|
-
(
|
268
|
-
|
269
|
-
]) :
|
270
|
-
|
284
|
+
return R(() => {
|
285
|
+
p();
|
286
|
+
}), (y, d) => (L(), _($, null, [
|
287
|
+
(F(o) || n.value) && !s.value ? b(y.$slots, "default", { key: 0 }, () => [
|
288
|
+
d[0] || (d[0] = S("p", null, "Processing authentication...", -1))
|
289
|
+
]) : C("", !0),
|
290
|
+
s.value ? b(y.$slots, "error", {
|
271
291
|
key: 1,
|
272
|
-
error:
|
292
|
+
error: s.value
|
273
293
|
}, () => [
|
274
|
-
|
275
|
-
|
276
|
-
|
294
|
+
S("div", null, [
|
295
|
+
d[1] || (d[1] = S("h2", null, "Authentication Error", -1)),
|
296
|
+
S("p", null, P(s.value.message), 1)
|
277
297
|
])
|
278
|
-
]) :
|
298
|
+
]) : C("", !0)
|
279
299
|
], 64));
|
280
300
|
}
|
281
|
-
}),
|
301
|
+
}), se = /* @__PURE__ */ M({
|
282
302
|
__name: "SilentCallback",
|
283
303
|
props: {
|
284
304
|
onError: { type: Function }
|
285
305
|
},
|
286
306
|
setup(e) {
|
287
|
-
const r = e, { processSilentCallback: t, isInitializing:
|
288
|
-
var
|
307
|
+
const r = e, { processSilentCallback: t, isInitializing: l } = N(), a = m(null), o = async () => {
|
308
|
+
var n;
|
289
309
|
try {
|
290
310
|
await t();
|
291
|
-
} catch (
|
292
|
-
const
|
293
|
-
|
311
|
+
} catch (s) {
|
312
|
+
const p = s instanceof Error ? s : new Error("Silent callback processing failed");
|
313
|
+
a.value = p, (n = r.onError) == null || n.call(r, p), console.error("Silent callback error:", p);
|
294
314
|
}
|
295
315
|
};
|
296
|
-
return
|
316
|
+
return R(() => {
|
297
317
|
o();
|
298
|
-
}), (
|
299
|
-
|
300
|
-
|
301
|
-
]) :
|
302
|
-
|
318
|
+
}), (n, s) => (L(), _($, null, [
|
319
|
+
F(l) && !a.value ? b(n.$slots, "default", { key: 0 }, () => [
|
320
|
+
s[0] || (s[0] = S("p", null, "Processing silent renewal...", -1))
|
321
|
+
]) : C("", !0),
|
322
|
+
a.value ? b(n.$slots, "error", {
|
303
323
|
key: 1,
|
304
|
-
error:
|
324
|
+
error: a.value
|
305
325
|
}, () => [
|
306
|
-
|
307
|
-
]) :
|
326
|
+
S("p", null, "Silent renewal error: " + P(a.value.message), 1)
|
327
|
+
]) : C("", !0)
|
308
328
|
], 64));
|
309
329
|
}
|
310
330
|
});
|
311
331
|
export {
|
312
|
-
|
313
|
-
|
314
|
-
|
332
|
+
ae as AuthCallback,
|
333
|
+
se as SilentCallback,
|
334
|
+
ne as useAuth
|
315
335
|
};
|
package/dist/index.umd.js
CHANGED
@@ -1 +1 @@
|
|
1
|
-
(function(
|
1
|
+
(function(m,t){typeof exports=="object"&&typeof module<"u"?t(exports,require("vue"),require("oidc-client-ts")):typeof define=="function"&&define.amd?define(["exports","vue","oidc-client-ts"],t):(m=typeof globalThis<"u"?globalThis:m||self,t(m.Vuethenticate={},m.Vue,m.OidcClientTs))})(this,function(m,t,U){"use strict";function z(e){const r=typeof window<"u"?window.location.origin:"";return{authority:e.authority,client_id:e.clientId,client_secret:e.clientSecret,client_authentication:e.clientAuthentication??"client_secret_basic",redirect_uri:e.redirectUri??`${r}/auth/callback`,scope:e.scope??"openid profile",response_type:e.responseType??"code",userStore:e.storage?new U.WebStorageStateStore({store:R(e.storage)}):void 0,automaticSilentRenew:e.automaticSilentRenew??!0,silent_redirect_uri:e.silentRedirectUri??`${r}/auth/silent-callback`,post_logout_redirect_uri:e.postLogoutRedirectUri??r,includeIdTokenInSilentRenew:!0,accessTokenExpiringNotificationTimeInSeconds:60}}function R(e){if(typeof window>"u")return A();switch(e){case"localStorage":return window.localStorage;case"sessionStorage":return window.sessionStorage;case"memory":return A();default:return window.localStorage}}function A(){const e=new Map;return{getItem:r=>e.get(r)??null,setItem:(r,n)=>e.set(r,n),removeItem:r=>e.delete(r),clear:()=>e.clear(),get length(){return e.size},key:r=>Array.from(e.keys())[r]??null}}const f=new Map,_=e=>{try{const{userManager:r,eventCallbacks:n}=e;n.userLoaded&&r.events.removeUserLoaded(n.userLoaded),n.userUnloaded&&r.events.removeUserUnloaded(n.userUnloaded),n.accessTokenExpired&&r.events.removeAccessTokenExpired(n.accessTokenExpired),n.accessTokenExpiring&&r.events.removeAccessTokenExpiring(n.accessTokenExpiring),n.silentRenewError&&r.events.removeSilentRenewError(n.silentRenewError),r.stopSilentRenew(),r.clearStaleState()}catch(r){console.warn("Error during UserManager cleanup:",r)}},T=e=>`${e.authority}_${e.clientId}`,L=()=>f.size===0?null:(f.size===1||console.warn("Multiple auth instances detected. Callback components may not work as expected. Consider using a single auth instance or specify config explicitly."),Array.from(f.values())[0]||null),V=e=>{const r=T(e),n=f.get(r);if(n)return n;const l=N(e),s=t.ref(!1),c=t.ref(!1),a=t.ref(null),i=t.ref(!1),k=t.ref(null),E=t.computed(()=>!!a.value&&!a.value.expired),w=t.computed(()=>{var S;return((S=a.value)==null?void 0:S.access_token)||null}),p=t.computed(()=>{var S;return!!((S=a.value)!=null&&S.expired)}),h={userManager:l,isInitialized:s,isInitializing:c,user:a,isLoading:i,error:k,isAuthenticated:E,accessToken:w,isExpired:p,eventCallbacks:{}};return f.set(r,h),h},N=e=>{const r=z(e);return new U.UserManager(r)},v=(e,r,n,l)=>{const s=e instanceof Error?e:new Error(r);return n.value=s,l==null||l(s),s},$=e=>{var r,n;if(!((r=e.authority)!=null&&r.trim()))throw new Error("AuthConfig: authority is required");if(!((n=e.clientId)!=null&&n.trim()))throw new Error("AuthConfig: clientId is required");try{new URL(e.authority)}catch{throw new Error("AuthConfig: authority must be a valid URL")}},I=async(e,r=1e4)=>{if(!e.isInitialized.value)return new Promise((n,l)=>{const s=setTimeout(()=>{l(new Error("Authentication initialization timeout"))},r),c=t.watch(e.isInitialized,a=>{a&&(clearTimeout(s),c(),n())},{immediate:!0})})},F=e=>{$(e);const r=V(e),{userManager:n,isInitialized:l,isInitializing:s,user:c,isLoading:a,error:i,isAuthenticated:k,accessToken:E,isExpired:w}=r,p=T(e),h=async()=>{if(!(s.value||l.value))try{s.value=!0,a.value=!0,i.value=null;const u=await n.getUser();if(u)if(!u.expired)c.value=u;else if(e.automaticSilentRenew!==!1)try{await x()}catch(o){console.warn("Silent renewal failed during initialization:",o),await n.removeUser(),c.value=null}else await n.removeUser(),c.value=null}catch(u){v(u,"Failed to initialize authentication",i,e.onError)}finally{a.value=!1,s.value=!1,l.value=!0}},S=async u=>{try{a.value=!0,i.value=null,await n.signinRedirect({state:u})}catch(o){v(o,"Sign in failed",i,e.onError)}finally{a.value=!1}},B=async u=>{try{a.value=!0,i.value=null,c.value=null,await n.signoutRedirect({state:u})}catch(o){v(o,"Sign out failed",i,e.onError)}finally{a.value=!1}},x=async()=>{try{a.value=!0,i.value=null;const u=await n.signinSilent();c.value=u}catch(u){throw v(u,"Silent renewal failed",i,e.onError)}finally{a.value=!1}},O=()=>{i.value=null},K=(u,o)=>{const{userManager:g,user:b,error:D,eventCallbacks:y}=u;y.userLoaded=d=>{var C;b.value=d,(C=o.onUserLoaded)==null||C.call(o,d)},y.userUnloaded=()=>{var d;b.value=null,(d=o.onUserUnloaded)==null||d.call(o)},y.accessTokenExpired=()=>{var d;(d=o.onAccessTokenExpired)==null||d.call(o)},y.accessTokenExpiring=()=>{var d;(d=o.onAccessTokenExpiring)==null||d.call(o)},y.silentRenewError=async d=>{console.warn("Silent renewal failed:",d);try{await g.removeUser(),b.value=null}catch(C){console.error("Failed to remove user after silent renewal error:",C)}v(d,"Silent renewal failed",D,o.onError)},g.events.addUserLoaded(y.userLoaded),g.events.addUserUnloaded(y.userUnloaded),g.events.addAccessTokenExpired(y.accessTokenExpired),g.events.addAccessTokenExpiring(y.accessTokenExpiring),g.events.addSilentRenewError(y.silentRenewError)},j=()=>{_(r),f.delete(p)};return!l.value&&!s.value&&(h(),K(r,e)),{user:c,isAuthenticated:k,isLoading:a,error:i,accessToken:E,isExpired:w,signIn:S,signOut:B,silentRenew:x,clearError:O,cleanup:j}},M=()=>{const e=L();if(!e)throw new Error("Authentication not initialized. Make sure useAuth() is called in a parent component first.");return{processCallback:async l=>{await I(e);const s=l||window.location.href,c=await e.userManager.signinRedirectCallback(s);return{user:c,state:c.state}},processSilentCallback:async l=>{await I(e);const s=l||window.location.href;await e.userManager.signinSilentCallback(s)},isInitialized:e.isInitialized,isInitializing:e.isInitializing}},P=t.defineComponent({__name:"AuthCallback",props:{onSuccess:{type:Function},onError:{type:Function}},emits:["success","error"],setup(e,{emit:r}){const n=e,l=r,{processCallback:s,isInitializing:c}=M(),a=t.ref(!0),i=t.ref(null),k=async()=>{var E,w;try{const p=await s();(E=n.onSuccess)==null||E.call(n,p.user,p.state),l("success",p.user,p.state)}catch(p){const h=p instanceof Error?p:new Error("Callback processing failed");i.value=h,(w=n.onError)==null||w.call(n,h),l("error",h)}finally{a.value=!1}};return t.onMounted(()=>{k()}),(E,w)=>(t.openBlock(),t.createElementBlock(t.Fragment,null,[(t.unref(c)||a.value)&&!i.value?t.renderSlot(E.$slots,"default",{key:0},()=>[w[0]||(w[0]=t.createElementVNode("p",null,"Processing authentication...",-1))]):t.createCommentVNode("",!0),i.value?t.renderSlot(E.$slots,"error",{key:1,error:i.value},()=>[t.createElementVNode("div",null,[w[1]||(w[1]=t.createElementVNode("h2",null,"Authentication Error",-1)),t.createElementVNode("p",null,t.toDisplayString(i.value.message),1)])]):t.createCommentVNode("",!0)],64))}}),q=t.defineComponent({__name:"SilentCallback",props:{onError:{type:Function}},setup(e){const r=e,{processSilentCallback:n,isInitializing:l}=M(),s=t.ref(null),c=async()=>{var a;try{await n()}catch(i){const k=i instanceof Error?i:new Error("Silent callback processing failed");s.value=k,(a=r.onError)==null||a.call(r,k),console.error("Silent callback error:",k)}};return t.onMounted(()=>{c()}),(a,i)=>(t.openBlock(),t.createElementBlock(t.Fragment,null,[t.unref(l)&&!s.value?t.renderSlot(a.$slots,"default",{key:0},()=>[i[0]||(i[0]=t.createElementVNode("p",null,"Processing silent renewal...",-1))]):t.createCommentVNode("",!0),s.value?t.renderSlot(a.$slots,"error",{key:1,error:s.value},()=>[t.createElementVNode("p",null,"Silent renewal error: "+t.toDisplayString(s.value.message),1)]):t.createCommentVNode("",!0)],64))}});m.AuthCallback=P,m.SilentCallback=q,m.useAuth=F,Object.defineProperty(m,Symbol.toStringTag,{value:"Module"})});
|
package/dist/types/auth.d.ts
CHANGED
@@ -31,6 +31,7 @@ export interface AuthMethods<TState = unknown> {
|
|
31
31
|
signOut: (state?: TState) => Promise<void>;
|
32
32
|
silentRenew: () => Promise<void>;
|
33
33
|
clearError: () => void;
|
34
|
+
cleanup: () => void;
|
34
35
|
}
|
35
36
|
export interface AuthCallbackMethods<TState = unknown> {
|
36
37
|
processCallback: (url?: string) => Promise<{
|
package/dist/types/auth.d.ts.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../src/types/auth.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,KAAK,CAAC;AAC/B,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAE3C,MAAM,WAAW,UAAU;IAEvB,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,oBAAoB,CAAC,EAAE,qBAAqB,GAAG,oBAAoB,CAAC;IAEpE,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,YAAY,CAAC,EAAE,MAAM,CAAC;IAEtB,OAAO,CAAC,EAAE,cAAc,GAAG,gBAAgB,GAAG,QAAQ,CAAC;IACvD,oBAAoB,CAAC,EAAE,OAAO,CAAC;IAC/B,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,qBAAqB,CAAC,EAAE,MAAM,CAAC;IAE/B,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;IACjC,YAAY,CAAC,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,IAAI,CAAC;IACpC,cAAc,CAAC,EAAE,MAAM,IAAI,CAAC;IAC5B,oBAAoB,CAAC,EAAE,MAAM,IAAI,CAAC;IAClC,qBAAqB,CAAC,EAAE,MAAM,IAAI,CAAC;CACtC;AAED,MAAM,WAAW,SAAS;IACtB,IAAI,EAAE,GAAG,CAAC,IAAI,GAAG,IAAI,CAAC,CAAC;IACvB,eAAe,EAAE,GAAG,CAAC,OAAO,CAAC,CAAC;IAC9B,SAAS,EAAE,GAAG,CAAC,OAAO,CAAC,CAAC;IACxB,KAAK,EAAE,GAAG,CAAC,KAAK,GAAG,IA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