Skip to content

Commit a92a3de

Browse files
docs(): documented usage with vue-router
1 parent 5ad9d99 commit a92a3de

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

README.md

+49
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,55 @@ app.use(vueKeycloak, async () => {
8888
})
8989
```
9090

91+
### Use with vue-router
92+
If you need to wait for authentication to complete before proceeding with your Vue app setup, for instance, because you are using the `vue-router` package and need to initialize the router only after the authentication process is completed, you should initialize your app in the following way:
93+
94+
**router/index.ts**
95+
```typescript
96+
import { createRouter, createWebHistory } from 'vue-router'
97+
98+
const routes = [ /* Your routes */ ]
99+
100+
const initRouter = () => {
101+
const history = createWebHistory(import.meta.env.BASE_URL)
102+
return createRouter({ history, routes })
103+
}
104+
105+
export { initRouter }
106+
```
107+
108+
**main.ts**
109+
```typescript
110+
import { createApp } from 'vue'
111+
import { vueKeycloak } from '@josempgon/vue-keycloak'
112+
import App from './App.vue'
113+
import { initRouter } from './router'
114+
115+
const app = createApp(App)
116+
117+
await vueKeycloak.install(app, {
118+
config: {
119+
url: 'http://keycloak-server/auth',
120+
realm: 'myrealm',
121+
clientId: 'myapp',
122+
},
123+
})
124+
125+
app.use(initRouter())
126+
app.mount('#app')
127+
```
128+
129+
If you are building for a browser that does not support [Top-level await](https://caniuse.com/mdn-javascript_operators_await_top_level), you should wrap the Vue plugin and router initialization in an async [IIFE](https://developer.mozilla.org/en-US/docs/Glossary/IIFE):
130+
131+
```typescript
132+
(async () => {
133+
await vueKeycloak.install(app, options);
134+
135+
app.use(initRouter());
136+
app.mount('#app');
137+
})();
138+
```
139+
91140
## Use Token
92141

93142
A helper function is exported to manage the access token.

0 commit comments

Comments
 (0)