@logto/vue 0.1.14 → 0.1.17
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 +109 -0
- package/lib/index.d.ts +1 -1
- package/lib/index.js +3 -3
- package/lib/plugin.d.ts +5 -1
- package/lib/plugin.js +4 -6
- package/package.json +3 -4
package/README.md
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# Logto Vue SDK
|
|
2
|
+
[](https://www.npmjs.com/package/@logto/vue)
|
|
3
|
+
[](https://github.com/logto-io/js/actions/workflows/main.yml)
|
|
4
|
+
[](https://app.codecov.io/gh/logto-io/js?branch=master)
|
|
5
|
+
|
|
6
|
+
The Logto Vue SDK written in TypeScript. Check out our [integration guide](https://docs.logto.io/integrate-sdk/vue) or [docs](https://docs.logto.io/sdk/vue) for more information.
|
|
7
|
+
|
|
8
|
+
We also provide [集成指南](https://docs.logto.io/zh-cn/integrate-sdk/vue) and [文档](https://docs.logto.io/zh-cn/sdk/vue) in Simplified Chinese.
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
### Using npm
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install @logto/vue
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
### Using yarn
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
yarn add @logto/vue
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### Using pnpm
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
pnpm install @logto/vue
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### Using CDN
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
<script src="https://logto.io/js/logto-sdk-vue/0.1.0/logto-sdk-vue.production.js" />
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Get Started
|
|
37
|
+
|
|
38
|
+
A sample project with the following code snippets can be found at [Vue Sample](https://github.com/logto-io/js/tree/master/packages/vue-sample)
|
|
39
|
+
|
|
40
|
+
Check out the source code and try it yourself. (We use [pnpm](https://pnpm.io/) for package management)
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
pnpm i && pnpm start
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Import Logto Vue plugin
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
import { createLogto, LogtoConfig } from '@logto/vue';
|
|
50
|
+
|
|
51
|
+
const config: LogtoConfig = {
|
|
52
|
+
appId: '<your-application-id>',
|
|
53
|
+
endpoint: '<your-logto-endpoint>'
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const app = createApp(App);
|
|
57
|
+
|
|
58
|
+
app.use(createLogto, config);
|
|
59
|
+
app.mount("#app");
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Setup your sign-in
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
import { useLogto } from "@logto/vue";
|
|
66
|
+
|
|
67
|
+
const { signIn } = useLogto();
|
|
68
|
+
const onClickSignIn = () => signIn(redirectUrl);
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
```html
|
|
72
|
+
<button @click="onClickSignIn">Sign In</button>
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Retrieve Auth Status
|
|
76
|
+
|
|
77
|
+
```ts
|
|
78
|
+
import { useLogto } from '@logto/vue';
|
|
79
|
+
|
|
80
|
+
const { isAuthenticated } = useLogto();
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
```html
|
|
84
|
+
<div v-if="!isAuthenticated">
|
|
85
|
+
<!-- E.g. navigate to the sign in page -->
|
|
86
|
+
</div>
|
|
87
|
+
<div v-else>
|
|
88
|
+
<!-- Do things when user is authenticated -->
|
|
89
|
+
</div>
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Sign out
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
import { useLogto } from "@logto/vue";
|
|
96
|
+
|
|
97
|
+
const { signOut } = useLogto();
|
|
98
|
+
const onClickSignOut = () => signOut('http://localhost:1234');
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
```html
|
|
102
|
+
<button @click="onClickSignOut">Sign Out</button>
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Resources
|
|
106
|
+
|
|
107
|
+
[](https://logto.io/)
|
|
108
|
+
[](https://docs.logto.io/docs/sdk/swift/)
|
|
109
|
+
[](https://discord.gg/UEPaF3j5e6)
|
package/lib/index.d.ts
CHANGED
|
@@ -73,7 +73,7 @@ export declare const useLogto: () => Logto;
|
|
|
73
73
|
*
|
|
74
74
|
* Use this in the setup script of your Callback page to make sure the injection works
|
|
75
75
|
*/
|
|
76
|
-
export declare const useHandleSignInCallback: (
|
|
76
|
+
export declare const useHandleSignInCallback: (callback?: (() => void) | undefined) => {
|
|
77
77
|
isLoading: Readonly<Ref<boolean>>;
|
|
78
78
|
isAuthenticated: Readonly<Ref<boolean>>;
|
|
79
79
|
error: Readonly<Ref<Error | undefined>>;
|
package/lib/index.js
CHANGED
|
@@ -92,17 +92,17 @@ exports.useLogto = useLogto;
|
|
|
92
92
|
*
|
|
93
93
|
* Use this in the setup script of your Callback page to make sure the injection works
|
|
94
94
|
*/
|
|
95
|
-
const useHandleSignInCallback = (
|
|
95
|
+
const useHandleSignInCallback = (callback) => {
|
|
96
96
|
const context = (0, vue_1.inject)(consts_1.contextInjectionKey);
|
|
97
97
|
if (!context) {
|
|
98
98
|
return (0, context_1.throwContextError)();
|
|
99
99
|
}
|
|
100
|
-
const currentPageUrl = window.location.href;
|
|
101
100
|
const { isAuthenticated, isLoading, logtoClient, error } = context;
|
|
102
101
|
const { handleSignInCallback } = (0, plugin_1.createPluginMethods)(context);
|
|
103
102
|
(0, vue_1.watchEffect)(() => {
|
|
103
|
+
const currentPageUrl = window.location.href;
|
|
104
104
|
if (!isAuthenticated.value && logtoClient.value?.isSignInRedirected(currentPageUrl)) {
|
|
105
|
-
void handleSignInCallback(currentPageUrl,
|
|
105
|
+
void handleSignInCallback(currentPageUrl, callback);
|
|
106
106
|
}
|
|
107
107
|
});
|
|
108
108
|
return {
|
package/lib/plugin.d.ts
CHANGED
|
@@ -11,6 +11,10 @@ export declare const createPluginMethods: (context: Context) => {
|
|
|
11
11
|
exp: number;
|
|
12
12
|
iat: number;
|
|
13
13
|
at_hash?: string | undefined;
|
|
14
|
+
name?: string | undefined;
|
|
15
|
+
username?: string | undefined;
|
|
16
|
+
avatar?: string | undefined;
|
|
17
|
+
role_names?: string[] | undefined;
|
|
14
18
|
} | undefined;
|
|
15
|
-
handleSignInCallback: (callbackUri: string,
|
|
19
|
+
handleSignInCallback: (callbackUri: string, callbackFunction?: (() => void) | undefined) => Promise<undefined>;
|
|
16
20
|
};
|
package/lib/plugin.js
CHANGED
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.createPluginMethods = void 0;
|
|
4
4
|
const context_1 = require("./context");
|
|
5
5
|
const createPluginMethods = (context) => {
|
|
6
|
-
const { logtoClient, setLoading, setError } = context;
|
|
6
|
+
const { logtoClient, setLoading, setError, setIsAuthenticated } = context;
|
|
7
7
|
const signIn = async (redirectUri) => {
|
|
8
8
|
if (!logtoClient.value) {
|
|
9
9
|
return (0, context_1.throwContextError)();
|
|
@@ -78,17 +78,15 @@ const createPluginMethods = (context) => {
|
|
|
78
78
|
setError(error, 'Unexpected error occurred while getting id token claims.');
|
|
79
79
|
}
|
|
80
80
|
};
|
|
81
|
-
const handleSignInCallback = async (callbackUri,
|
|
81
|
+
const handleSignInCallback = async (callbackUri, callbackFunction) => {
|
|
82
82
|
if (!logtoClient.value) {
|
|
83
83
|
return (0, context_1.throwContextError)();
|
|
84
84
|
}
|
|
85
85
|
try {
|
|
86
86
|
setLoading(true);
|
|
87
87
|
await logtoClient.value.handleSignInCallback(callbackUri);
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
// Moreover, since the location will be redirected, the isAuthenticated state will not matter any more.
|
|
91
|
-
window.location.assign(returnToPageUrl);
|
|
88
|
+
setIsAuthenticated(true);
|
|
89
|
+
callbackFunction?.();
|
|
92
90
|
}
|
|
93
91
|
catch (error) {
|
|
94
92
|
setError(error, 'Unexpected error occurred while handling sign in callback.');
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@logto/vue",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.17",
|
|
4
4
|
"main": "./lib/index.js",
|
|
5
5
|
"exports": "./lib/index.js",
|
|
6
6
|
"typings": "./lib/index.d.ts",
|
|
@@ -15,7 +15,6 @@
|
|
|
15
15
|
},
|
|
16
16
|
"scripts": {
|
|
17
17
|
"dev:tsc": "tsc -p tsconfig.build.json -w --preserveWatchOutput",
|
|
18
|
-
"preinstall": "npx only-allow pnpm",
|
|
19
18
|
"precommit": "lint-staged",
|
|
20
19
|
"build": "rm -rf lib/ && tsc -p tsconfig.build.json",
|
|
21
20
|
"lint": "eslint --ext .ts src",
|
|
@@ -24,7 +23,7 @@
|
|
|
24
23
|
"prepack": "pnpm test"
|
|
25
24
|
},
|
|
26
25
|
"dependencies": {
|
|
27
|
-
"@logto/browser": "^0.1.
|
|
26
|
+
"@logto/browser": "^0.1.17"
|
|
28
27
|
},
|
|
29
28
|
"devDependencies": {
|
|
30
29
|
"@jest/types": "^27.5.1",
|
|
@@ -61,5 +60,5 @@
|
|
|
61
60
|
"publishConfig": {
|
|
62
61
|
"access": "public"
|
|
63
62
|
},
|
|
64
|
-
"gitHead": "
|
|
63
|
+
"gitHead": "57e93385829eceb3c1b2ffb92b90901aa174a59a"
|
|
65
64
|
}
|