@logto/vue 1.1.2 → 2.0.0
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/lib/consts.cjs +7 -0
- package/lib/consts.js +1 -4
- package/lib/{context.mjs → context.cjs} +7 -4
- package/lib/context.js +4 -7
- package/lib/index.cjs +139 -0
- package/lib/index.js +30 -59
- package/lib/{plugin.mjs → plugin.cjs} +12 -10
- package/lib/plugin.d.ts +1 -1
- package/lib/plugin.js +10 -12
- package/package.json +22 -23
- package/lib/consts.mjs +0 -4
- package/lib/index.mjs +0 -106
package/lib/consts.cjs
ADDED
package/lib/consts.js
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
1
|
const logtoInjectionKey = '@logto/vue';
|
|
4
2
|
const contextInjectionKey = '@logto/vue:context';
|
|
5
3
|
|
|
6
|
-
|
|
7
|
-
exports.logtoInjectionKey = logtoInjectionKey;
|
|
4
|
+
export { contextInjectionKey, logtoInjectionKey };
|
|
@@ -1,14 +1,16 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var vue = require('vue');
|
|
2
4
|
|
|
3
5
|
const createContext = (client) => {
|
|
4
|
-
const context = toRefs(reactive({
|
|
6
|
+
const context = vue.toRefs(vue.reactive({
|
|
5
7
|
logtoClient: client,
|
|
6
8
|
isAuthenticated: false,
|
|
7
9
|
loadingCount: 1,
|
|
8
10
|
error: undefined,
|
|
9
11
|
}));
|
|
10
12
|
const { isAuthenticated, loadingCount, error } = context;
|
|
11
|
-
const isLoading = computed(() => loadingCount.value > 0);
|
|
13
|
+
const isLoading = vue.computed(() => loadingCount.value > 0);
|
|
12
14
|
/* eslint-disable @silverhand/fp/no-mutation */
|
|
13
15
|
const setError = (_error, fallbackErrorMessage) => {
|
|
14
16
|
if (_error instanceof Error) {
|
|
@@ -42,4 +44,5 @@ const throwContextError = () => {
|
|
|
42
44
|
throw new Error('Must install Logto plugin first.');
|
|
43
45
|
};
|
|
44
46
|
|
|
45
|
-
|
|
47
|
+
exports.createContext = createContext;
|
|
48
|
+
exports.throwContextError = throwContextError;
|
package/lib/context.js
CHANGED
|
@@ -1,16 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var vue = require('vue');
|
|
1
|
+
import { toRefs, reactive, computed } from 'vue';
|
|
4
2
|
|
|
5
3
|
const createContext = (client) => {
|
|
6
|
-
const context =
|
|
4
|
+
const context = toRefs(reactive({
|
|
7
5
|
logtoClient: client,
|
|
8
6
|
isAuthenticated: false,
|
|
9
7
|
loadingCount: 1,
|
|
10
8
|
error: undefined,
|
|
11
9
|
}));
|
|
12
10
|
const { isAuthenticated, loadingCount, error } = context;
|
|
13
|
-
const isLoading =
|
|
11
|
+
const isLoading = computed(() => loadingCount.value > 0);
|
|
14
12
|
/* eslint-disable @silverhand/fp/no-mutation */
|
|
15
13
|
const setError = (_error, fallbackErrorMessage) => {
|
|
16
14
|
if (_error instanceof Error) {
|
|
@@ -44,5 +42,4 @@ const throwContextError = () => {
|
|
|
44
42
|
throw new Error('Must install Logto plugin first.');
|
|
45
43
|
};
|
|
46
44
|
|
|
47
|
-
|
|
48
|
-
exports.throwContextError = throwContextError;
|
|
45
|
+
export { createContext, throwContextError };
|
package/lib/index.cjs
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var LogtoClient = require('@logto/browser');
|
|
4
|
+
var vue = require('vue');
|
|
5
|
+
var consts = require('./consts.cjs');
|
|
6
|
+
var context = require('./context.cjs');
|
|
7
|
+
var plugin = require('./plugin.cjs');
|
|
8
|
+
|
|
9
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
10
|
+
|
|
11
|
+
var LogtoClient__default = /*#__PURE__*/_interopDefault(LogtoClient);
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Creates the Logto Vue plugin
|
|
15
|
+
*
|
|
16
|
+
* ```ts
|
|
17
|
+
* import { createApp } from 'vue';
|
|
18
|
+
* import { createLogto } from '@logto/vue';
|
|
19
|
+
*
|
|
20
|
+
* const app = createApp(App);
|
|
21
|
+
* const app.use(createLogto, {
|
|
22
|
+
* appId: '<your-app-id>',
|
|
23
|
+
* endpoint: '<your-oidc-endpoint-domain>',
|
|
24
|
+
* });
|
|
25
|
+
*
|
|
26
|
+
* app.mount('#app');
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* Use this in your Vue root component to register the plugin
|
|
30
|
+
*/
|
|
31
|
+
const createLogto = {
|
|
32
|
+
install(app, config) {
|
|
33
|
+
const client = new LogtoClient__default.default(config);
|
|
34
|
+
const context$1 = context.createContext(client);
|
|
35
|
+
const pluginMethods = plugin.createPluginMethods(context$1);
|
|
36
|
+
const { isAuthenticated, isLoading, error } = context$1;
|
|
37
|
+
app.provide(consts.contextInjectionKey, context$1);
|
|
38
|
+
app.provide(consts.logtoInjectionKey, {
|
|
39
|
+
isAuthenticated: vue.readonly(isAuthenticated),
|
|
40
|
+
isLoading: vue.readonly(isLoading),
|
|
41
|
+
error: vue.readonly(error),
|
|
42
|
+
...pluginMethods,
|
|
43
|
+
});
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* A Vue composable method that provides the Logto reactive refs and auth methods.
|
|
48
|
+
*
|
|
49
|
+
* ```ts
|
|
50
|
+
* import { useLogto } from '@logto/vue';
|
|
51
|
+
*
|
|
52
|
+
* export default {
|
|
53
|
+
* setup() {
|
|
54
|
+
* const { isAuthenticated, signIn } = useLogto();
|
|
55
|
+
*
|
|
56
|
+
* return {
|
|
57
|
+
* isAuthenticated,
|
|
58
|
+
* onClickSignIn: () => {
|
|
59
|
+
* signIn('<your-redirect-uri>');
|
|
60
|
+
* },
|
|
61
|
+
* }
|
|
62
|
+
* }
|
|
63
|
+
* }
|
|
64
|
+
* ```
|
|
65
|
+
*
|
|
66
|
+
* Use this composable in the setup script of your Vue component to make sure the injection works
|
|
67
|
+
*/
|
|
68
|
+
const useLogto = () => {
|
|
69
|
+
const logto = vue.inject(consts.logtoInjectionKey);
|
|
70
|
+
if (!logto) {
|
|
71
|
+
return context.throwContextError();
|
|
72
|
+
}
|
|
73
|
+
return logto;
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* A Vue composable method that watches browser navigation and automatically handles the sign-in callback
|
|
77
|
+
*
|
|
78
|
+
* ```ts
|
|
79
|
+
* import { useLogto } from '@logto/vue';
|
|
80
|
+
* import { useHandleSignInCallback } from '@logto/vue';
|
|
81
|
+
*
|
|
82
|
+
* export default {
|
|
83
|
+
* setup() {
|
|
84
|
+
* useHandleSignInCallback();
|
|
85
|
+
* }
|
|
86
|
+
* }
|
|
87
|
+
* ```
|
|
88
|
+
*
|
|
89
|
+
* Use this in the setup script of your Callback page to make sure the injection works
|
|
90
|
+
*/
|
|
91
|
+
const useHandleSignInCallback = (callback) => {
|
|
92
|
+
const context$1 = vue.inject(consts.contextInjectionKey);
|
|
93
|
+
if (!context$1) {
|
|
94
|
+
return context.throwContextError();
|
|
95
|
+
}
|
|
96
|
+
const { isAuthenticated, isLoading, logtoClient, error } = context$1;
|
|
97
|
+
const { handleSignInCallback } = plugin.createPluginMethods(context$1);
|
|
98
|
+
vue.watchEffect(() => {
|
|
99
|
+
const currentPageUrl = window.location.href;
|
|
100
|
+
if (!isAuthenticated.value &&
|
|
101
|
+
logtoClient.value?.isSignInRedirected(currentPageUrl) &&
|
|
102
|
+
!isLoading.value) {
|
|
103
|
+
void handleSignInCallback(currentPageUrl, callback);
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
return {
|
|
107
|
+
isLoading: vue.readonly(isLoading),
|
|
108
|
+
isAuthenticated: vue.readonly(isAuthenticated),
|
|
109
|
+
error: vue.readonly(error),
|
|
110
|
+
};
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
Object.defineProperty(exports, 'LogtoClientError', {
|
|
114
|
+
enumerable: true,
|
|
115
|
+
get: function () { return LogtoClient.LogtoClientError; }
|
|
116
|
+
});
|
|
117
|
+
Object.defineProperty(exports, 'LogtoError', {
|
|
118
|
+
enumerable: true,
|
|
119
|
+
get: function () { return LogtoClient.LogtoError; }
|
|
120
|
+
});
|
|
121
|
+
Object.defineProperty(exports, 'OidcError', {
|
|
122
|
+
enumerable: true,
|
|
123
|
+
get: function () { return LogtoClient.OidcError; }
|
|
124
|
+
});
|
|
125
|
+
Object.defineProperty(exports, 'Prompt', {
|
|
126
|
+
enumerable: true,
|
|
127
|
+
get: function () { return LogtoClient.Prompt; }
|
|
128
|
+
});
|
|
129
|
+
Object.defineProperty(exports, 'ReservedScope', {
|
|
130
|
+
enumerable: true,
|
|
131
|
+
get: function () { return LogtoClient.ReservedScope; }
|
|
132
|
+
});
|
|
133
|
+
Object.defineProperty(exports, 'UserScope', {
|
|
134
|
+
enumerable: true,
|
|
135
|
+
get: function () { return LogtoClient.UserScope; }
|
|
136
|
+
});
|
|
137
|
+
exports.createLogto = createLogto;
|
|
138
|
+
exports.useHandleSignInCallback = useHandleSignInCallback;
|
|
139
|
+
exports.useLogto = useLogto;
|
package/lib/index.js
CHANGED
|
@@ -1,14 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
var plugin = require('./plugin.js');
|
|
8
|
-
|
|
9
|
-
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
10
|
-
|
|
11
|
-
var LogtoClient__default = /*#__PURE__*/_interopDefault(LogtoClient);
|
|
1
|
+
import LogtoClient from '@logto/browser';
|
|
2
|
+
export { LogtoClientError, LogtoError, OidcError, Prompt, ReservedScope, UserScope } from '@logto/browser';
|
|
3
|
+
import { readonly, inject, watchEffect } from 'vue';
|
|
4
|
+
import { contextInjectionKey, logtoInjectionKey } from './consts.js';
|
|
5
|
+
import { createContext, throwContextError } from './context.js';
|
|
6
|
+
import { createPluginMethods } from './plugin.js';
|
|
12
7
|
|
|
13
8
|
/**
|
|
14
9
|
* Creates the Logto Vue plugin
|
|
@@ -30,15 +25,15 @@ var LogtoClient__default = /*#__PURE__*/_interopDefault(LogtoClient);
|
|
|
30
25
|
*/
|
|
31
26
|
const createLogto = {
|
|
32
27
|
install(app, config) {
|
|
33
|
-
const client = new
|
|
34
|
-
const context
|
|
35
|
-
const pluginMethods =
|
|
36
|
-
const { isAuthenticated, isLoading, error } = context
|
|
37
|
-
app.provide(
|
|
38
|
-
app.provide(
|
|
39
|
-
isAuthenticated:
|
|
40
|
-
isLoading:
|
|
41
|
-
error:
|
|
28
|
+
const client = new LogtoClient(config);
|
|
29
|
+
const context = createContext(client);
|
|
30
|
+
const pluginMethods = createPluginMethods(context);
|
|
31
|
+
const { isAuthenticated, isLoading, error } = context;
|
|
32
|
+
app.provide(contextInjectionKey, context);
|
|
33
|
+
app.provide(logtoInjectionKey, {
|
|
34
|
+
isAuthenticated: readonly(isAuthenticated),
|
|
35
|
+
isLoading: readonly(isLoading),
|
|
36
|
+
error: readonly(error),
|
|
42
37
|
...pluginMethods,
|
|
43
38
|
});
|
|
44
39
|
},
|
|
@@ -66,9 +61,9 @@ const createLogto = {
|
|
|
66
61
|
* Use this composable in the setup script of your Vue component to make sure the injection works
|
|
67
62
|
*/
|
|
68
63
|
const useLogto = () => {
|
|
69
|
-
const logto =
|
|
64
|
+
const logto = inject(logtoInjectionKey);
|
|
70
65
|
if (!logto) {
|
|
71
|
-
return
|
|
66
|
+
return throwContextError();
|
|
72
67
|
}
|
|
73
68
|
return logto;
|
|
74
69
|
};
|
|
@@ -89,49 +84,25 @@ const useLogto = () => {
|
|
|
89
84
|
* Use this in the setup script of your Callback page to make sure the injection works
|
|
90
85
|
*/
|
|
91
86
|
const useHandleSignInCallback = (callback) => {
|
|
92
|
-
const context
|
|
93
|
-
if (!context
|
|
94
|
-
return
|
|
87
|
+
const context = inject(contextInjectionKey);
|
|
88
|
+
if (!context) {
|
|
89
|
+
return throwContextError();
|
|
95
90
|
}
|
|
96
|
-
const { isAuthenticated, isLoading, logtoClient, error } = context
|
|
97
|
-
const { handleSignInCallback } =
|
|
98
|
-
|
|
91
|
+
const { isAuthenticated, isLoading, logtoClient, error } = context;
|
|
92
|
+
const { handleSignInCallback } = createPluginMethods(context);
|
|
93
|
+
watchEffect(() => {
|
|
99
94
|
const currentPageUrl = window.location.href;
|
|
100
|
-
if (!isAuthenticated.value &&
|
|
95
|
+
if (!isAuthenticated.value &&
|
|
96
|
+
logtoClient.value?.isSignInRedirected(currentPageUrl) &&
|
|
97
|
+
!isLoading.value) {
|
|
101
98
|
void handleSignInCallback(currentPageUrl, callback);
|
|
102
99
|
}
|
|
103
100
|
});
|
|
104
101
|
return {
|
|
105
|
-
isLoading:
|
|
106
|
-
isAuthenticated:
|
|
107
|
-
error:
|
|
102
|
+
isLoading: readonly(isLoading),
|
|
103
|
+
isAuthenticated: readonly(isAuthenticated),
|
|
104
|
+
error: readonly(error),
|
|
108
105
|
};
|
|
109
106
|
};
|
|
110
107
|
|
|
111
|
-
|
|
112
|
-
enumerable: true,
|
|
113
|
-
get: function () { return LogtoClient.LogtoClientError; }
|
|
114
|
-
});
|
|
115
|
-
Object.defineProperty(exports, 'LogtoError', {
|
|
116
|
-
enumerable: true,
|
|
117
|
-
get: function () { return LogtoClient.LogtoError; }
|
|
118
|
-
});
|
|
119
|
-
Object.defineProperty(exports, 'OidcError', {
|
|
120
|
-
enumerable: true,
|
|
121
|
-
get: function () { return LogtoClient.OidcError; }
|
|
122
|
-
});
|
|
123
|
-
Object.defineProperty(exports, 'Prompt', {
|
|
124
|
-
enumerable: true,
|
|
125
|
-
get: function () { return LogtoClient.Prompt; }
|
|
126
|
-
});
|
|
127
|
-
Object.defineProperty(exports, 'ReservedScope', {
|
|
128
|
-
enumerable: true,
|
|
129
|
-
get: function () { return LogtoClient.ReservedScope; }
|
|
130
|
-
});
|
|
131
|
-
Object.defineProperty(exports, 'UserScope', {
|
|
132
|
-
enumerable: true,
|
|
133
|
-
get: function () { return LogtoClient.UserScope; }
|
|
134
|
-
});
|
|
135
|
-
exports.createLogto = createLogto;
|
|
136
|
-
exports.useHandleSignInCallback = useHandleSignInCallback;
|
|
137
|
-
exports.useLogto = useLogto;
|
|
108
|
+
export { createLogto, useHandleSignInCallback, useLogto };
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
var context = require('./context.cjs');
|
|
4
|
+
|
|
5
|
+
const createPluginMethods = (context$1) => {
|
|
6
|
+
const { logtoClient, setLoading, setError, setIsAuthenticated } = context$1;
|
|
5
7
|
const signIn = async (redirectUri, interactionMode) => {
|
|
6
8
|
if (!logtoClient.value) {
|
|
7
|
-
return throwContextError();
|
|
9
|
+
return context.throwContextError();
|
|
8
10
|
}
|
|
9
11
|
try {
|
|
10
12
|
setLoading(true);
|
|
@@ -16,7 +18,7 @@ const createPluginMethods = (context) => {
|
|
|
16
18
|
};
|
|
17
19
|
const signOut = async (postLogoutRedirectUri) => {
|
|
18
20
|
if (!logtoClient.value) {
|
|
19
|
-
return throwContextError();
|
|
21
|
+
return context.throwContextError();
|
|
20
22
|
}
|
|
21
23
|
try {
|
|
22
24
|
setLoading(true);
|
|
@@ -34,7 +36,7 @@ const createPluginMethods = (context) => {
|
|
|
34
36
|
};
|
|
35
37
|
const fetchUserInfo = async () => {
|
|
36
38
|
if (!logtoClient.value) {
|
|
37
|
-
return throwContextError();
|
|
39
|
+
return context.throwContextError();
|
|
38
40
|
}
|
|
39
41
|
try {
|
|
40
42
|
setLoading(true);
|
|
@@ -49,7 +51,7 @@ const createPluginMethods = (context) => {
|
|
|
49
51
|
};
|
|
50
52
|
const getAccessToken = async (resource) => {
|
|
51
53
|
if (!logtoClient.value) {
|
|
52
|
-
return throwContextError();
|
|
54
|
+
return context.throwContextError();
|
|
53
55
|
}
|
|
54
56
|
try {
|
|
55
57
|
setLoading(true);
|
|
@@ -64,7 +66,7 @@ const createPluginMethods = (context) => {
|
|
|
64
66
|
};
|
|
65
67
|
const getIdTokenClaims = async () => {
|
|
66
68
|
if (!logtoClient.value) {
|
|
67
|
-
return throwContextError();
|
|
69
|
+
return context.throwContextError();
|
|
68
70
|
}
|
|
69
71
|
try {
|
|
70
72
|
return await logtoClient.value.getIdTokenClaims();
|
|
@@ -75,7 +77,7 @@ const createPluginMethods = (context) => {
|
|
|
75
77
|
};
|
|
76
78
|
const handleSignInCallback = async (callbackUri, callbackFunction) => {
|
|
77
79
|
if (!logtoClient.value) {
|
|
78
|
-
return throwContextError();
|
|
80
|
+
return context.throwContextError();
|
|
79
81
|
}
|
|
80
82
|
try {
|
|
81
83
|
setLoading(true);
|
|
@@ -100,4 +102,4 @@ const createPluginMethods = (context) => {
|
|
|
100
102
|
};
|
|
101
103
|
};
|
|
102
104
|
|
|
103
|
-
|
|
105
|
+
exports.createPluginMethods = createPluginMethods;
|
package/lib/plugin.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { InteractionMode } from '@logto/browser';
|
|
2
|
-
import type { Context } from './context';
|
|
2
|
+
import type { Context } from './context.js';
|
|
3
3
|
export declare const createPluginMethods: (context: Context) => {
|
|
4
4
|
signIn: (redirectUri: string, interactionMode?: InteractionMode) => Promise<undefined>;
|
|
5
5
|
signOut: (postLogoutRedirectUri?: string) => Promise<undefined>;
|
package/lib/plugin.js
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
import { throwContextError } from './context.js';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const createPluginMethods = (context$1) => {
|
|
6
|
-
const { logtoClient, setLoading, setError, setIsAuthenticated } = context$1;
|
|
3
|
+
const createPluginMethods = (context) => {
|
|
4
|
+
const { logtoClient, setLoading, setError, setIsAuthenticated } = context;
|
|
7
5
|
const signIn = async (redirectUri, interactionMode) => {
|
|
8
6
|
if (!logtoClient.value) {
|
|
9
|
-
return
|
|
7
|
+
return throwContextError();
|
|
10
8
|
}
|
|
11
9
|
try {
|
|
12
10
|
setLoading(true);
|
|
@@ -18,7 +16,7 @@ const createPluginMethods = (context$1) => {
|
|
|
18
16
|
};
|
|
19
17
|
const signOut = async (postLogoutRedirectUri) => {
|
|
20
18
|
if (!logtoClient.value) {
|
|
21
|
-
return
|
|
19
|
+
return throwContextError();
|
|
22
20
|
}
|
|
23
21
|
try {
|
|
24
22
|
setLoading(true);
|
|
@@ -36,7 +34,7 @@ const createPluginMethods = (context$1) => {
|
|
|
36
34
|
};
|
|
37
35
|
const fetchUserInfo = async () => {
|
|
38
36
|
if (!logtoClient.value) {
|
|
39
|
-
return
|
|
37
|
+
return throwContextError();
|
|
40
38
|
}
|
|
41
39
|
try {
|
|
42
40
|
setLoading(true);
|
|
@@ -51,7 +49,7 @@ const createPluginMethods = (context$1) => {
|
|
|
51
49
|
};
|
|
52
50
|
const getAccessToken = async (resource) => {
|
|
53
51
|
if (!logtoClient.value) {
|
|
54
|
-
return
|
|
52
|
+
return throwContextError();
|
|
55
53
|
}
|
|
56
54
|
try {
|
|
57
55
|
setLoading(true);
|
|
@@ -66,7 +64,7 @@ const createPluginMethods = (context$1) => {
|
|
|
66
64
|
};
|
|
67
65
|
const getIdTokenClaims = async () => {
|
|
68
66
|
if (!logtoClient.value) {
|
|
69
|
-
return
|
|
67
|
+
return throwContextError();
|
|
70
68
|
}
|
|
71
69
|
try {
|
|
72
70
|
return await logtoClient.value.getIdTokenClaims();
|
|
@@ -77,7 +75,7 @@ const createPluginMethods = (context$1) => {
|
|
|
77
75
|
};
|
|
78
76
|
const handleSignInCallback = async (callbackUri, callbackFunction) => {
|
|
79
77
|
if (!logtoClient.value) {
|
|
80
|
-
return
|
|
78
|
+
return throwContextError();
|
|
81
79
|
}
|
|
82
80
|
try {
|
|
83
81
|
setLoading(true);
|
|
@@ -102,4 +100,4 @@ const createPluginMethods = (context$1) => {
|
|
|
102
100
|
};
|
|
103
101
|
};
|
|
104
102
|
|
|
105
|
-
|
|
103
|
+
export { createPluginMethods };
|
package/package.json
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@logto/vue",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"main": "./lib/index.
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./lib/index.cjs",
|
|
6
|
+
"module": "./lib/index.js",
|
|
7
|
+
"types": "./lib/index.d.ts",
|
|
6
8
|
"exports": {
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
+
"types": "./lib/index.d.ts",
|
|
10
|
+
"require": "./lib/index.cjs",
|
|
11
|
+
"import": "./lib/index.js",
|
|
12
|
+
"default": "./lib/index.js"
|
|
9
13
|
},
|
|
10
|
-
"module": "./lib/index.mjs",
|
|
11
|
-
"types": "./lib/index.d.ts",
|
|
12
14
|
"files": [
|
|
13
15
|
"lib"
|
|
14
16
|
],
|
|
@@ -18,23 +20,12 @@
|
|
|
18
20
|
"url": "https://github.com/logto-io/js.git",
|
|
19
21
|
"directory": "packages/vue"
|
|
20
22
|
},
|
|
21
|
-
"scripts": {
|
|
22
|
-
"dev:tsc": "tsc -p tsconfig.build.json -w --preserveWatchOutput",
|
|
23
|
-
"precommit": "lint-staged",
|
|
24
|
-
"check": "tsc --noEmit",
|
|
25
|
-
"build": "rm -rf lib/ && tsc -p tsconfig.build.json --noEmit && rollup -c",
|
|
26
|
-
"lint": "eslint --ext .ts src",
|
|
27
|
-
"test": "jest",
|
|
28
|
-
"test:coverage": "jest --silent --coverage",
|
|
29
|
-
"prepack": "pnpm test"
|
|
30
|
-
},
|
|
31
23
|
"dependencies": {
|
|
32
|
-
"@logto/browser": "^
|
|
24
|
+
"@logto/browser": "^2.0.0"
|
|
33
25
|
},
|
|
34
26
|
"devDependencies": {
|
|
35
|
-
"@
|
|
36
|
-
"@silverhand/
|
|
37
|
-
"@silverhand/ts-config": "^1.0.0",
|
|
27
|
+
"@silverhand/eslint-config": "^3.0.1",
|
|
28
|
+
"@silverhand/ts-config": "^3.0.0",
|
|
38
29
|
"@swc/core": "^1.3.50",
|
|
39
30
|
"@swc/jest": "^0.2.24",
|
|
40
31
|
"@types/jest": "^29.5.0",
|
|
@@ -67,5 +58,13 @@
|
|
|
67
58
|
"publishConfig": {
|
|
68
59
|
"access": "public"
|
|
69
60
|
},
|
|
70
|
-
"
|
|
71
|
-
|
|
61
|
+
"scripts": {
|
|
62
|
+
"dev:tsc": "tsc -p tsconfig.build.json -w --preserveWatchOutput",
|
|
63
|
+
"precommit": "lint-staged",
|
|
64
|
+
"check": "tsc --noEmit",
|
|
65
|
+
"build": "rm -rf lib/ && tsc -p tsconfig.build.json --noEmit && rollup -c",
|
|
66
|
+
"lint": "eslint --ext .ts src",
|
|
67
|
+
"test": "jest",
|
|
68
|
+
"test:coverage": "jest --silent --coverage"
|
|
69
|
+
}
|
|
70
|
+
}
|
package/lib/consts.mjs
DELETED
package/lib/index.mjs
DELETED
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
import LogtoClient from '@logto/browser';
|
|
2
|
-
export { LogtoClientError, LogtoError, OidcError, Prompt, ReservedScope, UserScope } from '@logto/browser';
|
|
3
|
-
import { readonly, inject, watchEffect } from 'vue';
|
|
4
|
-
import { contextInjectionKey, logtoInjectionKey } from './consts.mjs';
|
|
5
|
-
import { createContext, throwContextError } from './context.mjs';
|
|
6
|
-
import { createPluginMethods } from './plugin.mjs';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Creates the Logto Vue plugin
|
|
10
|
-
*
|
|
11
|
-
* ```ts
|
|
12
|
-
* import { createApp } from 'vue';
|
|
13
|
-
* import { createLogto } from '@logto/vue';
|
|
14
|
-
*
|
|
15
|
-
* const app = createApp(App);
|
|
16
|
-
* const app.use(createLogto, {
|
|
17
|
-
* appId: '<your-app-id>',
|
|
18
|
-
* endpoint: '<your-oidc-endpoint-domain>',
|
|
19
|
-
* });
|
|
20
|
-
*
|
|
21
|
-
* app.mount('#app');
|
|
22
|
-
* ```
|
|
23
|
-
*
|
|
24
|
-
* Use this in your Vue root component to register the plugin
|
|
25
|
-
*/
|
|
26
|
-
const createLogto = {
|
|
27
|
-
install(app, config) {
|
|
28
|
-
const client = new LogtoClient(config);
|
|
29
|
-
const context = createContext(client);
|
|
30
|
-
const pluginMethods = createPluginMethods(context);
|
|
31
|
-
const { isAuthenticated, isLoading, error } = context;
|
|
32
|
-
app.provide(contextInjectionKey, context);
|
|
33
|
-
app.provide(logtoInjectionKey, {
|
|
34
|
-
isAuthenticated: readonly(isAuthenticated),
|
|
35
|
-
isLoading: readonly(isLoading),
|
|
36
|
-
error: readonly(error),
|
|
37
|
-
...pluginMethods,
|
|
38
|
-
});
|
|
39
|
-
},
|
|
40
|
-
};
|
|
41
|
-
/**
|
|
42
|
-
* A Vue composable method that provides the Logto reactive refs and auth methods.
|
|
43
|
-
*
|
|
44
|
-
* ```ts
|
|
45
|
-
* import { useLogto } from '@logto/vue';
|
|
46
|
-
*
|
|
47
|
-
* export default {
|
|
48
|
-
* setup() {
|
|
49
|
-
* const { isAuthenticated, signIn } = useLogto();
|
|
50
|
-
*
|
|
51
|
-
* return {
|
|
52
|
-
* isAuthenticated,
|
|
53
|
-
* onClickSignIn: () => {
|
|
54
|
-
* signIn('<your-redirect-uri>');
|
|
55
|
-
* },
|
|
56
|
-
* }
|
|
57
|
-
* }
|
|
58
|
-
* }
|
|
59
|
-
* ```
|
|
60
|
-
*
|
|
61
|
-
* Use this composable in the setup script of your Vue component to make sure the injection works
|
|
62
|
-
*/
|
|
63
|
-
const useLogto = () => {
|
|
64
|
-
const logto = inject(logtoInjectionKey);
|
|
65
|
-
if (!logto) {
|
|
66
|
-
return throwContextError();
|
|
67
|
-
}
|
|
68
|
-
return logto;
|
|
69
|
-
};
|
|
70
|
-
/**
|
|
71
|
-
* A Vue composable method that watches browser navigation and automatically handles the sign-in callback
|
|
72
|
-
*
|
|
73
|
-
* ```ts
|
|
74
|
-
* import { useLogto } from '@logto/vue';
|
|
75
|
-
* import { useHandleSignInCallback } from '@logto/vue';
|
|
76
|
-
*
|
|
77
|
-
* export default {
|
|
78
|
-
* setup() {
|
|
79
|
-
* useHandleSignInCallback();
|
|
80
|
-
* }
|
|
81
|
-
* }
|
|
82
|
-
* ```
|
|
83
|
-
*
|
|
84
|
-
* Use this in the setup script of your Callback page to make sure the injection works
|
|
85
|
-
*/
|
|
86
|
-
const useHandleSignInCallback = (callback) => {
|
|
87
|
-
const context = inject(contextInjectionKey);
|
|
88
|
-
if (!context) {
|
|
89
|
-
return throwContextError();
|
|
90
|
-
}
|
|
91
|
-
const { isAuthenticated, isLoading, logtoClient, error } = context;
|
|
92
|
-
const { handleSignInCallback } = createPluginMethods(context);
|
|
93
|
-
watchEffect(() => {
|
|
94
|
-
const currentPageUrl = window.location.href;
|
|
95
|
-
if (!isAuthenticated.value && logtoClient.value?.isSignInRedirected(currentPageUrl)) {
|
|
96
|
-
void handleSignInCallback(currentPageUrl, callback);
|
|
97
|
-
}
|
|
98
|
-
});
|
|
99
|
-
return {
|
|
100
|
-
isLoading: readonly(isLoading),
|
|
101
|
-
isAuthenticated: readonly(isAuthenticated),
|
|
102
|
-
error: readonly(error),
|
|
103
|
-
};
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
export { createLogto, useHandleSignInCallback, useLogto };
|