Rework to use marbas and authentik instead of poketbase (#1)
Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
47
src/auth.ts
Normal file
47
src/auth.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import log from "loglevel";
|
||||
import { Log, User, UserManager } from "oidc-client-ts";
|
||||
import { defineStore } from "pinia";
|
||||
import { computed, ref } from "vue";
|
||||
|
||||
Log.setLogger(log);
|
||||
|
||||
export const useAuthStore = defineStore('auth', () => {
|
||||
const userManager = new UserManager({
|
||||
authority: import.meta.env.VITE_AUTH_AUTHORITY,
|
||||
client_id: import.meta.env.VITE_AUTH_CLIENT_ID,
|
||||
client_secret: import.meta.env.VITE_AUTH_CLIENT_SECRET,
|
||||
redirect_uri: import.meta.env.VITE_AUTH_REDIRECT_URI,
|
||||
scope: import.meta.env.VITE_AUTH_SCOPE
|
||||
});
|
||||
|
||||
const user = ref<User>();
|
||||
const isLoggedIn = computed(() => !!user.value);
|
||||
const accessToken = computed(() => user.value?.access_token);
|
||||
const username = computed(() => user.value?.profile.name ?? "");
|
||||
const userId = computed(() => user.value?.profile.sub ?? "");
|
||||
|
||||
const redirect = async () => {
|
||||
await userManager.signinRedirect();
|
||||
log.info("Redirecting to login page");
|
||||
}
|
||||
const login = async () => {
|
||||
await userManager.signinCallback();
|
||||
log.debug("Logged in");
|
||||
}
|
||||
const logout = async () => {
|
||||
await userManager.signoutRedirect();
|
||||
log.debug("Logged out");
|
||||
}
|
||||
|
||||
const setUser = (u?: User | null) => {
|
||||
if (u) {
|
||||
user.value = u;
|
||||
log.debug("User loaded", u.profile.name);
|
||||
} else {
|
||||
user.value = undefined;
|
||||
}
|
||||
}
|
||||
userManager.events.addUserLoaded(setUser);
|
||||
userManager.getUser().then(setUser);
|
||||
return { redirect, login, logout, isLoggedIn, accessToken, username, userId };
|
||||
});
|
||||
Reference in New Issue
Block a user