Added Frontend
This commit is contained in:
168
frontend/stores/auth.ts
Normal file
168
frontend/stores/auth.ts
Normal file
@@ -0,0 +1,168 @@
|
||||
import { defineStore } from "pinia"
|
||||
import router from "#app/plugins/router";
|
||||
import {Preferences} from "@capacitor/preferences";
|
||||
|
||||
export const useAuthStore = defineStore("auth", {
|
||||
state: () => ({
|
||||
user: null as null | { user_id: string; email: string; tenant_id?: string; role?: string },
|
||||
profile: null as null | any,
|
||||
tenants: [] as { tenant_id: string; role: string; tenants: { id: string; name: string } }[],
|
||||
permissions: [] as string[],
|
||||
activeTenant: null as any,
|
||||
activeTenantData: null as any,
|
||||
loading: true as boolean,
|
||||
notLoggedIn: true,
|
||||
}),
|
||||
|
||||
actions: {
|
||||
async persist(token) {
|
||||
if(useCapacitor().getIsNative()) {
|
||||
console.log("On Native")
|
||||
await Preferences.set({
|
||||
key:"token",
|
||||
value: token,
|
||||
})
|
||||
useCookie("token").value = token // persistieren
|
||||
|
||||
} else {
|
||||
console.log("On Web")
|
||||
useCookie("token").value = token // persistieren
|
||||
|
||||
}
|
||||
},
|
||||
|
||||
async initStore() {
|
||||
console.log("Auth initStore")
|
||||
await this.fetchMe()
|
||||
|
||||
if(this.activeTenant > 0) {
|
||||
this.loading = false
|
||||
if(useCapacitor().getIsNative()) {
|
||||
navigateTo("/mobile")
|
||||
} else {
|
||||
navigateTo("/")
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
async init(token=null) {
|
||||
console.log("Auth init")
|
||||
await this.fetchMe(token)
|
||||
|
||||
const tempStore = useTempStore()
|
||||
|
||||
if(this.profile.temp_config) tempStore.setStoredTempConfig(this.profile.temp_config)
|
||||
if(this.activeTenant > 0) {
|
||||
this.loading = false
|
||||
if(useCapacitor().getIsNative()) {
|
||||
navigateTo("/mobile")
|
||||
} else {
|
||||
navigateTo("/")
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
async login(email: string, password: string) {
|
||||
try {
|
||||
console.log("Auth login")
|
||||
const { token } = await useNuxtApp().$api("/auth/login", {
|
||||
method: "POST",
|
||||
body: { email, password }
|
||||
})
|
||||
console.log("Token: " + token)
|
||||
await this.fetchMe(token)
|
||||
} catch (e) {
|
||||
console.log("login error:" + e)
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
async logout() {
|
||||
console.log("Auth logout")
|
||||
try {
|
||||
await useNuxtApp().$api("/auth/logout", { method: "POST" })
|
||||
} catch (e) {
|
||||
console.error("Logout fehlgeschlagen:", e)
|
||||
}
|
||||
this.user = null
|
||||
this.permissions = []
|
||||
this.profile = null
|
||||
this.activeTenant = null
|
||||
this.tenants = []
|
||||
if(useCapacitor().getIsNative()) {
|
||||
await Preferences.remove({ key: 'token' });
|
||||
useCookie("token").value = null
|
||||
} else {
|
||||
useCookie("token").value = null
|
||||
}
|
||||
|
||||
navigateTo("/login")
|
||||
},
|
||||
|
||||
async fetchMe(jwt= null) {
|
||||
console.log("Auth fetchMe")
|
||||
const tempStore = useTempStore()
|
||||
|
||||
try {
|
||||
const me = await useNuxtApp().$api("/api/me", {
|
||||
headers: { Authorization: `Bearer ${jwt}`,
|
||||
context: {
|
||||
jwt
|
||||
}}
|
||||
})
|
||||
console.log(me)
|
||||
this.user = me.user
|
||||
this.permissions = me.permissions
|
||||
this.tenants = me.tenants
|
||||
|
||||
this.tenants.sort(function (a, b) {
|
||||
if (a.id < b.id) return -1
|
||||
if (a.id > b.id) return 1
|
||||
})
|
||||
|
||||
this.profile = me.profile
|
||||
|
||||
if(this.profile.temp_config) tempStore.setStoredTempConfig(this.profile.temp_config)
|
||||
|
||||
if(me.activeTenant > 0) {
|
||||
this.activeTenant = me.activeTenant
|
||||
this.activeTenantData = me.tenants.find(i => i.id === me.activeTenant)
|
||||
|
||||
} else {
|
||||
|
||||
}
|
||||
|
||||
|
||||
} catch (err: any) {
|
||||
if (err?.response?.status === 401) this.logout()
|
||||
}
|
||||
},
|
||||
|
||||
async switchTenant(tenant_id: string) {
|
||||
console.log("Auth switchTenant")
|
||||
this.loading = true
|
||||
const res = await useNuxtApp().$api("/api/tenant/switch", {
|
||||
method: "POST",
|
||||
body: { tenant_id }
|
||||
})
|
||||
console.log(res)
|
||||
|
||||
const {token} = res
|
||||
|
||||
if(useCapacitor().getIsNative()) {
|
||||
await Preferences.set({
|
||||
key:"token",
|
||||
value: token,
|
||||
})
|
||||
} else {
|
||||
useCookie("token").value = token // persistieren
|
||||
|
||||
}
|
||||
await this.init(token)
|
||||
},
|
||||
|
||||
hasPermission(key: string) {
|
||||
return this.permissions.includes(key)
|
||||
}
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user