allauth-vue 0.4.3

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 ADDED
@@ -0,0 +1,5 @@
1
+ # AllAuth Vue
2
+
3
+ AllAuth Vue Wrapper
4
+
5
+ 旨在为 Vue 开发者提供简便的 AllAuth 集成方式。
@@ -0,0 +1,2 @@
1
+ export * from './lib/allauth'
2
+ export {}
@@ -0,0 +1,204 @@
1
+ import { inject as w, ref as T, watch as k, computed as m } from "vue";
2
+ function p(i, e, t, o) {
3
+ function s(r) {
4
+ return r instanceof t ? r : new t(function(n) {
5
+ n(r);
6
+ });
7
+ }
8
+ return new (t || (t = Promise))(function(r, n) {
9
+ function a(c) {
10
+ try {
11
+ u(o.next(c));
12
+ } catch (h) {
13
+ n(h);
14
+ }
15
+ }
16
+ function d(c) {
17
+ try {
18
+ u(o.throw(c));
19
+ } catch (h) {
20
+ n(h);
21
+ }
22
+ }
23
+ function u(c) {
24
+ c.done ? r(c.value) : s(c.value).then(a, d);
25
+ }
26
+ u((o = o.apply(i, e || [])).next());
27
+ });
28
+ }
29
+ const E = (i) => {
30
+ const o = window.screenX + (window.innerWidth - 400) / 2, s = window.screenY + (window.innerHeight - 600) / 2;
31
+ return window.open(i, "auth0:authorize:popup", `left=${o},top=${s},width=400,height=600,resizable,scrollbars=yes,status=1`);
32
+ }, f = "__ALLAUTH_", g = f + "ACCESS_TOKEN", l = f + "ID_TOKEN", A = {
33
+ load: function(i) {
34
+ return window.localStorage.getItem(i);
35
+ },
36
+ save: function(i, e) {
37
+ window.localStorage.setItem(i, e);
38
+ },
39
+ delete: function(i) {
40
+ window.localStorage.removeItem(i);
41
+ }
42
+ }, S = (i) => decodeURIComponent(atob(i).split("").map((e) => "%" + ("00" + e.charCodeAt(0).toString(16)).slice(-2)).join("")), _ = (i) => S(i.replace(/_/g, "/").replace(/-/g, "+")), R = (i) => {
43
+ const e = i.split("."), [t, o, s] = e;
44
+ if (e.length !== 3 || !t || !o || !s)
45
+ throw new Error("ID token could not be decoded");
46
+ const r = JSON.parse(_(o)), n = { __raw: i };
47
+ return Object.keys(r).forEach((a) => {
48
+ n[a] = r[a];
49
+ }), {
50
+ encoded: { header: t, payload: o, signature: s },
51
+ header: JSON.parse(_(t)),
52
+ claims: n
53
+ };
54
+ };
55
+ class y {
56
+ getIdToken() {
57
+ return this.idToken;
58
+ }
59
+ getAccessToken() {
60
+ return this.accessToken;
61
+ }
62
+ getUser() {
63
+ if (!this.idToken)
64
+ return null;
65
+ const { sub: e, name: t } = this.idToken.claims;
66
+ return { sub: e, name: t };
67
+ }
68
+ constructor(e) {
69
+ if (this.defaultOptions = {
70
+ storage: A
71
+ }, !e.allauth_domain)
72
+ throw new Error("allauth_domain 不能为空");
73
+ if (!e.app_domain)
74
+ throw new Error("app_domain 不能为空");
75
+ if (!e.allauth_api_endpoint)
76
+ throw new Error("allauth_api_endpoint 不能为空");
77
+ this.options = Object.assign(Object.assign({}, this.defaultOptions), e);
78
+ const t = this.options.storage.load(l), o = this.options.storage.load(g);
79
+ t && this._onIdTokenUpdated(t), this.accessToken = o;
80
+ }
81
+ login() {
82
+ window.location.assign(this._buildAuthorizeUrl());
83
+ }
84
+ logout() {
85
+ this.options.storage.delete(l), delete this.idToken;
86
+ }
87
+ loginWithPopup() {
88
+ return p(this, void 0, void 0, function* () {
89
+ const e = E("");
90
+ if (!e)
91
+ throw new Error("无法打开登录窗口: window.open 返回 `null`");
92
+ e.location.href = this._buildAuthorizeUrl();
93
+ });
94
+ }
95
+ _getRedirectUri() {
96
+ return location.href;
97
+ }
98
+ _buildAuthorizeUrl() {
99
+ const e = `${this.options.allauth_domain}`, t = this._getRedirectUri(), o = ["openid"], s = "code", r = Date.now();
100
+ let n = new URLSearchParams();
101
+ return o.forEach((a) => n.append("scope", a)), n.append("response_type", s), n.append("redirect_uri", t), n.append("client_id", this.options.client_id), n.append("state", r.toString()), `${e}?${n.toString()}`;
102
+ }
103
+ /**
104
+ * 在重定向到 Authorize EndPoint 后,
105
+ * AllAuth Provider 重定向到回调页
106
+ * 调用 `handleAuthroizeRedirectCallback` 去处理成功或失败的响应
107
+ * 如果响应成功,会继续鉴权流程,拿到 AccessToken 和 IdToken
108
+ */
109
+ handleAuthroizeRedirectCallback() {
110
+ return p(this, arguments, void 0, function* (e = location.href) {
111
+ const t = new URL(e), o = t.searchParams.get("code");
112
+ if (o) {
113
+ console.log("got code", o);
114
+ const s = yield this._requestToken(o);
115
+ console.log(s);
116
+ const { access_token: r, id_token: n, error: a } = s;
117
+ t.searchParams.delete("code"), this._updateToken({ accessToken: r, idToken: n }), location.replace(t);
118
+ }
119
+ return t;
120
+ });
121
+ }
122
+ _onIdTokenUpdated(e) {
123
+ let t;
124
+ try {
125
+ t = R(e), this.idToken = t, console.log("idToken", t);
126
+ } catch (o) {
127
+ console.error(o);
128
+ }
129
+ }
130
+ _updateToken({ idToken: e, accessToken: t }) {
131
+ this.accessToken = t, t ? this.options.storage.save(g, t) : (console.log("ACCESS_TOKEN Cleared"), this.options.storage.delete(g)), e ? (this.options.storage.save(l, e), this._onIdTokenUpdated(e)) : (console.log("ID_TOKEN Cleared"), this.options.storage.delete(l));
132
+ }
133
+ _requestToken(e) {
134
+ return p(this, void 0, void 0, function* () {
135
+ const t = `${this.options.allauth_api_endpoint}/token`;
136
+ return yield (yield this._postForm(t, {
137
+ grant_type: "authorization_code",
138
+ redirect_uri: this._getRedirectUri(),
139
+ code: e
140
+ })).json();
141
+ });
142
+ }
143
+ _postForm(e, t) {
144
+ const o = [];
145
+ for (var s in t) {
146
+ var r = encodeURIComponent(s), n = encodeURIComponent(t[s]);
147
+ o.push(r + "=" + n);
148
+ }
149
+ return fetch(e, {
150
+ method: "POST",
151
+ headers: {
152
+ "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
153
+ },
154
+ body: o.join("&")
155
+ });
156
+ }
157
+ }
158
+ function U(i) {
159
+ return {
160
+ allauth: {
161
+ install(e) {
162
+ const t = i.plugins || {}, o = new y(i), s = T(null);
163
+ (async () => (console.log("页面加载"), await o.handleAuthroizeRedirectCallback(), s.value = o.getUser()))();
164
+ function r(n) {
165
+ switch (n) {
166
+ case "login": {
167
+ const a = o.getAccessToken();
168
+ if (!a)
169
+ throw new Error("缺失 AccessToken");
170
+ for (const d of Object.values(t))
171
+ d.onauth({
172
+ event: "login",
173
+ accessToken: a
174
+ });
175
+ break;
176
+ }
177
+ }
178
+ }
179
+ k(s, (n, a) => {
180
+ console.log(n, a), n && !a ? r("login") : !n && a && r("logout");
181
+ }), e.provide("allauth", {
182
+ user: s,
183
+ accessToken: m(() => s.value && o.getAccessToken()),
184
+ login() {
185
+ o.login();
186
+ },
187
+ logout() {
188
+ o.logout(), s.value = o.getUser();
189
+ },
190
+ ...t
191
+ });
192
+ }
193
+ },
194
+ useAllAuth() {
195
+ const e = w("allauth");
196
+ if (typeof e > "u")
197
+ throw new Error("请先通过 .use(allauth) 注册 AllAuth 插件。");
198
+ return e;
199
+ }
200
+ };
201
+ }
202
+ export {
203
+ U as createAllAuth
204
+ };
@@ -0,0 +1,22 @@
1
+ import { AllAuthClientOptions, UserClaims } from 'allauth-spa-js';
2
+ import { AllAuthPlugin } from 'allauth-types';
3
+ import { ComputedRef, Ref, App } from 'vue';
4
+ type Plugins = {
5
+ [key: string]: AllAuthPlugin;
6
+ };
7
+ export type AllAuthVueClientOptions<P extends Plugins> = AllAuthClientOptions & {
8
+ plugins?: P;
9
+ };
10
+ export declare function createAllAuth<P extends Plugins>(options: AllAuthVueClientOptions<P>): {
11
+ allauth: {
12
+ install(app: App): void;
13
+ };
14
+ useAllAuth(): UseAllAuth & P;
15
+ };
16
+ export interface UseAllAuth {
17
+ user: Ref<UserClaims | null>;
18
+ accessToken: ComputedRef<UserClaims | null>;
19
+ login(): void;
20
+ logout(): void;
21
+ }
22
+ export {};
@@ -0,0 +1,5 @@
1
+ export declare const allauth: {
2
+ install(app: import('vue').App): void;
3
+ }, useAllAuth: () => import('../lib/allauth').UseAllAuth & {
4
+ [key: string]: import('allauth-types').AllAuthPlugin;
5
+ };
@@ -0,0 +1,2 @@
1
+ declare const _default: import('vite').UserConfig;
2
+ export default _default;
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "allauth-vue",
3
+ "version": "0.4.3",
4
+ "type": "module",
5
+ "main": "dist/allauth-vue.js",
6
+ "files": ["dist"],
7
+ "scripts": {
8
+ "dev": "vite",
9
+ "build": "vue-tsc -b && vite build",
10
+ "preview": "vite preview"
11
+ },
12
+ "dependencies": {
13
+ "allauth-spa-js": "^0.4.3",
14
+ "allauth-types": "^0.4.3",
15
+ "vue": "^3.5.25"
16
+ },
17
+ "devDependencies": {
18
+ "@types/node": "^24.10.1",
19
+ "@vitejs/plugin-vue": "^6.0.2",
20
+ "@vue/tsconfig": "^0.8.1",
21
+ "typescript": "~5.9.3",
22
+ "unplugin-dts": "1.0.0-beta.6",
23
+ "vite": "^7.3.1",
24
+ "vue-tsc": "^3.1.5"
25
+ }
26
+ }