Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Commit

Permalink
add ready event (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
asafshen authored Feb 15, 2024
1 parent a130e12 commit c3e7407
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 19 deletions.
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ app.mount('#app');

```vue
<template>
<Descope flowId="my-flow-id" @error="handleError" @success="handleSuccess" />
<p v-if="isFlowLoading">Loading...</p>
<Descope
flowId="my-flow-id"
@success="handleSuccess"
@error="handleError"
@ready="handleReady"
/>
<!-- additional props -->
<!-- theme="dark" theme can be "light", "dark" or "os", which auto select a theme based on the OS theme. Default is "light" -->
<!-- v-bind:debug="true" debug can be set to true to enable debug mode -->
Expand All @@ -55,13 +61,20 @@ app.mount('#app');
<script setup>
import { Descope } from '@descope/vue-sdk';
import { ref } from 'vue';
const isFlowLoading = ref(true);
const handleSuccess = (e) => {
console.log('Logged in!', e);
};
const handleError = (e) => {
console.log('Could not log in', e);
};
const handleSuccess = (e) => {
console.log('Logged in!', e);
const handleReady = () => {
isFlowLoading.value = false;
};
// let tenantId = '<tenantId>'; // replace with your tenant ID
Expand Down Expand Up @@ -215,7 +228,7 @@ You can find an example Vue app in the [example folder](./example).
### Setup
To run the examples, set your `Project ID` by setting the `DESCOPE_PROJECT_ID` env var or directly
To run the examples, set your `Project ID` by setting the `VUE_APP_DESCOPE_PROJECT_ID` env var or directly
in the sample code.
Find your Project ID in the [Descope console](https://app.descope.com/settings/project).
Expand Down
4 changes: 4 additions & 0 deletions example/components/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ const { logout } = useDescope();
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
color: #2c3e50;
display: flex;
flex-direction: column;
justify-content: center;
}
.routes {
Expand Down
17 changes: 13 additions & 4 deletions example/components/Login.vue
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<!-- eslint-disable vue/multi-word-component-names -->
<template>
<div class="wrapper">
<p v-if="isLoading">Loading...</p>
<div class="login-wrapper">
<p v-if="isLoading || isFlowLoading">Loading...</p>
<div v-else-if="isAuthenticated">
<h1>You are authenticated</h1>
</div>
<Descope
:flowId="flowId"
@error="handleError"
@success="handleSuccess"
@error="handleError"
@ready="handleReady"
:errorTransformer="errorTransformer"
:form="form"
:client="client"
Expand All @@ -18,13 +19,19 @@

<script setup>
import { Descope, useSession } from '../../src';
import { ref } from 'vue';
import { useRouter } from 'vue-router';
const router = useRouter();
const isFlowLoading = ref(true);

const handleError = (e) => {
console.log('Got error', e);
};

const handleReady = () => {
isFlowLoading.value = false;
};

const handleSuccess = (e) => {
console.log('Logged in', e);
router.push({ path: '/' });
Expand All @@ -44,7 +51,9 @@ const client = { version: '1.0.1' }; // found in context key: client.version
</script>

<style>
.wrapper {
.login-wrapper {
margin: 20px;
align-self: center;
max-width: 500px;
}
</style>
4 changes: 2 additions & 2 deletions example/components/ManageUsers.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!-- eslint-disable vue/multi-word-component-names -->
<template>
<div class="wrapper">
<div class="manage-users-wrapper">
<h1>Manage Users</h1>
<UserManagement :tenant="tenant" />
</div>
Expand All @@ -13,7 +13,7 @@ const tenant = process.env.VUE_APP_DESCOPE_TENANT;
</script>

<style>
.wrapper {
.manage-users-wrapper {
margin: 20px;
}
</style>
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
},
"dependencies": {
"@descope/user-management-widget": "0.0.9",
"@descope/web-component": "3.8.1"
"@descope/web-component": "3.8.2"
},
"peerDependencies": {
"vue": ">=3"
Expand Down
5 changes: 4 additions & 1 deletion src/Descope.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
:client.attr="clientStr"
@success="onSuccess"
@error="onError"
@ready="onReady"
/>
</div>
</template>
Expand Down Expand Up @@ -86,7 +87,7 @@ const props = defineProps({
type: Object
}
});
const emit = defineEmits(['success', 'error']);
const emit = defineEmits(['success', 'error', 'ready']);
const { projectId, baseUrl } = useOptions();
const sdk = useDescope();

Expand All @@ -103,4 +104,6 @@ const onSuccess = async (e: CustomEvent) => {
};

const onError = (e: Event) => emit('error', e);

const onReady = (e: Event) => emit('ready', e);
</script>
9 changes: 9 additions & 0 deletions tests/Descope.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,13 @@ describe('Descope.vue', () => {
detail: 'test-error-payload'
});
});

it('emits an ready event when the DescopeWc component emits an ready event', async () => {
const wrapper = mount(Descope);
const descopeWc = wrapper.find('descope-wc');

await descopeWc.trigger('ready', {});

expect(wrapper.emitted('ready')).toBeTruthy();
});
});

0 comments on commit c3e7407

Please sign in to comment.