Implemented Native Token Storage other than Cookies
This commit is contained in:
@@ -1,10 +1,19 @@
|
|||||||
|
import {Preferences} from "@capacitor/preferences";
|
||||||
|
|
||||||
export default defineNuxtPlugin(() => {
|
export default defineNuxtPlugin(() => {
|
||||||
const api = $fetch.create({
|
const api = $fetch.create({
|
||||||
baseURL: /*"http://localhost:3100"*/ "https://backend.fedeo.io",
|
baseURL: /*"http://192.168.1.227:3100"*/ "https://backend.fedeo.io",
|
||||||
credentials: "include",
|
credentials: "include",
|
||||||
onRequest({ options }) {
|
async onRequest({options}) {
|
||||||
// Token aus Cookie holen
|
// Token aus Cookie holen
|
||||||
let token = useCookie("token").value
|
let token: string | null | undefined = ""
|
||||||
|
if (await useCapacitor().getIsNative()) {
|
||||||
|
const {value} = await Preferences.get({key: 'token'});
|
||||||
|
token = value
|
||||||
|
} else {
|
||||||
|
token = useCookie("token").value
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Falls im Request explizit ein anderer JWT übergeben wird → diesen verwenden
|
// Falls im Request explizit ein anderer JWT übergeben wird → diesen verwenden
|
||||||
if (options.context && (options.context as any).jwt) {
|
if (options.context && (options.context as any).jwt) {
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { defineStore } from "pinia"
|
import { defineStore } from "pinia"
|
||||||
import router from "#app/plugins/router";
|
import router from "#app/plugins/router";
|
||||||
|
import {Preferences} from "@capacitor/preferences";
|
||||||
|
|
||||||
export const useAuthStore = defineStore("auth", {
|
export const useAuthStore = defineStore("auth", {
|
||||||
state: () => ({
|
state: () => ({
|
||||||
@@ -23,7 +24,15 @@ export const useAuthStore = defineStore("auth", {
|
|||||||
method: "POST",
|
method: "POST",
|
||||||
body: { email, password }
|
body: { email, password }
|
||||||
})
|
})
|
||||||
useCookie("token").value = token // persistieren
|
if(await useCapacitor().getIsNative()) {
|
||||||
|
await Preferences.set({
|
||||||
|
key:"token",
|
||||||
|
value: token,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
useCookie("token").value = token // persistieren
|
||||||
|
|
||||||
|
}
|
||||||
await this.fetchMe(token)
|
await this.fetchMe(token)
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -38,7 +47,13 @@ export const useAuthStore = defineStore("auth", {
|
|||||||
this.profile = null
|
this.profile = null
|
||||||
this.activeTenant = null
|
this.activeTenant = null
|
||||||
this.tenants = []
|
this.tenants = []
|
||||||
useCookie("token").value = null
|
if(useCapacitor().getIsNative()) {
|
||||||
|
await Preferences.remove({ key: 'token' });
|
||||||
|
|
||||||
|
} else {
|
||||||
|
useCookie("token").value = null
|
||||||
|
}
|
||||||
|
|
||||||
navigateTo("/login")
|
navigateTo("/login")
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -79,11 +94,23 @@ export const useAuthStore = defineStore("auth", {
|
|||||||
|
|
||||||
async switchTenant(tenant_id: string) {
|
async switchTenant(tenant_id: string) {
|
||||||
this.loading = true
|
this.loading = true
|
||||||
const { token } = await useNuxtApp().$api("/api/tenant/switch", {
|
const res = await useNuxtApp().$api("/api/tenant/switch", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: { tenant_id }
|
body: { tenant_id }
|
||||||
})
|
})
|
||||||
useCookie("token").value = token
|
console.log(res)
|
||||||
|
|
||||||
|
const {token} = res
|
||||||
|
|
||||||
|
if(await useCapacitor().getIsNative()) {
|
||||||
|
await Preferences.set({
|
||||||
|
key:"token",
|
||||||
|
value: token,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
useCookie("token").value = token // persistieren
|
||||||
|
|
||||||
|
}
|
||||||
await this.init(token)
|
await this.init(token)
|
||||||
navigateTo("/")
|
navigateTo("/")
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user