39 lines
899 B
JavaScript
39 lines
899 B
JavaScript
import {
|
|
ZitadelAuth
|
|
} from "./chunk-NFH7JLJM.js";
|
|
|
|
// src/adapters/vue.ts
|
|
import { reactive, toRefs } from "vue";
|
|
function useZitadelAuth(router, config) {
|
|
const auth = new ZitadelAuth(config);
|
|
const state = reactive({
|
|
isAuthenticated: false,
|
|
isLoading: true,
|
|
user: null,
|
|
error: null
|
|
});
|
|
auth.onAuthChange((s) => {
|
|
state.isAuthenticated = s.isAuthenticated;
|
|
state.isLoading = s.isLoading;
|
|
state.user = s.user;
|
|
state.error = s.error;
|
|
});
|
|
const initPromise = auth.init();
|
|
router.beforeEach(async (to) => {
|
|
if (to.path.includes("/auth/callback")) return true;
|
|
await initPromise;
|
|
if (auth.isAuthenticated) return true;
|
|
auth.login();
|
|
return false;
|
|
});
|
|
return {
|
|
...toRefs(state),
|
|
login: () => auth.login(),
|
|
logout: () => auth.logout(),
|
|
fetch: (url, init) => auth.fetch(url, init)
|
|
};
|
|
}
|
|
export {
|
|
useZitadelAuth
|
|
};
|