@inzombieland/nuxt-common 1.16.24 → 1.16.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/module.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inzombieland/nuxt-common",
3
- "version": "1.16.24",
3
+ "version": "1.16.25",
4
4
  "configKey": "nuxt-common",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "0.6.0",
package/dist/module.mjs CHANGED
@@ -2,7 +2,7 @@ import { fileURLToPath } from 'node:url';
2
2
  import { defineNuxtModule, createResolver, addServerHandler, addImportsDir, addPlugin, addComponent } from '@nuxt/kit';
3
3
 
4
4
  const name = "@inzombieland/nuxt-common";
5
- const version = "1.16.24";
5
+ const version = "1.16.25";
6
6
 
7
7
  const module = defineNuxtModule({
8
8
  meta: {
@@ -1,14 +1,9 @@
1
1
  <script setup lang="ts">
2
2
  import { StyleProvider, Themes } from "@varlet/ui"
3
3
  import { useLocalStorage } from "@vueuse/core"
4
- import { useLocalePath } from "#i18n"
5
4
  import { useNuxtApp } from "#imports"
6
5
  import { onMounted, ref, watch } from "vue"
7
- import { useRouter } from "vue-router"
8
- import { getVisitorId } from "./api"
9
- import { useActiveSessions } from "./composables/use-active-sessions"
10
- import { useUserActions } from "./composables/use-user-actions"
11
- import { useSubscribe, useUser } from "./packages/api"
6
+ import { useUser } from "./packages/api"
12
7
  import bus from "./packages/api/bus"
13
8
  import type { ConfigProviderTheme } from "vant"
14
9
  import "@vant/touch-emulator"
@@ -16,34 +11,9 @@ import "@vant/touch-emulator"
16
11
  const theme = ref("")
17
12
  const appPreferencesTheme = useLocalStorage("app-preferences-theme", JSON.stringify({ value: "" }))
18
13
  const user = useUser()
19
- const activeSessions = useActiveSessions()
20
- const { fetchActiveSessions, logout } = useUserActions()
21
- const localePath = useLocalePath()
22
- const router = useRouter()
23
14
  const appLoading = ref(Boolean(user.value))
24
15
  const { $mobileApp } = useNuxtApp()
25
16
 
26
- useSubscribe("WS.SessionsUpdated", async (session: { status?: string; visitorId?: string; sessionId?: string }) => {
27
- if (user.value && activeSessions.value.length > 0) {
28
- activeSessions.value = [...activeSessions.value].map(activeSession => {
29
- if (activeSession.id === session?.sessionId) {
30
- activeSession.isOnline = session?.status === "Online"
31
- activeSession.signedOut = session?.status === "SignedOut"
32
- activeSession.dateTime = new Date()
33
- }
34
- return activeSession
35
- })
36
- }
37
- if (user.value && session?.status === "SignedOut" && session?.visitorId === getVisitorId()) {
38
- const data = await fetchActiveSessions()
39
- const isCurrentSession = data.sessions.some((s: any) => s.isCurrentSession && s.id === session?.sessionId)
40
- if (isCurrentSession) {
41
- await logout()
42
- await router.push({ path: localePath("/") })
43
- }
44
- }
45
- })
46
-
47
17
  onMounted(() => {
48
18
  watch(
49
19
  appPreferencesTheme,
@@ -27,5 +27,8 @@ declare const fetch: Fetch, getUser: () => Promise<import("src/runtime/packages/
27
27
  key: string;
28
28
  }>;
29
29
  logout: () => Promise<void>;
30
+ fetchActiveSessions: () => Promise<{
31
+ sessions: import("src/runtime/packages/api/types").ActiveSession[];
32
+ }>;
30
33
  };
31
34
  export { fetch, getUser, useApiFetch, userActions };
@@ -0,0 +1,26 @@
1
+ import type { ActiveSession } from "../types";
2
+ export declare function useActiveSessions(): import("vue").Ref<{
3
+ id: string;
4
+ visitorId: string;
5
+ device: string;
6
+ appName: string;
7
+ location: string;
8
+ ipAddress: string;
9
+ isCurrentSession?: boolean | undefined;
10
+ isOnline?: boolean | undefined;
11
+ signedOut?: boolean | undefined;
12
+ dateTime: Date;
13
+ firstSignIn: Date;
14
+ }[], ActiveSession[] | {
15
+ id: string;
16
+ visitorId: string;
17
+ device: string;
18
+ appName: string;
19
+ location: string;
20
+ ipAddress: string;
21
+ isCurrentSession?: boolean | undefined;
22
+ isOnline?: boolean | undefined;
23
+ signedOut?: boolean | undefined;
24
+ dateTime: Date;
25
+ firstSignIn: Date;
26
+ }[]>;
@@ -0,0 +1,14 @@
1
+ import { ref } from "vue";
2
+ import { useUserActions } from "../index.mjs";
3
+ const activeSessions = ref([]);
4
+ export function useActiveSessions() {
5
+ const userActions = useUserActions();
6
+ const fetchActiveSessions = async () => {
7
+ if (activeSessions.value.length === 0) {
8
+ const data = await userActions?.value.fetchActiveSessions();
9
+ activeSessions.value = data?.sessions ?? [];
10
+ }
11
+ };
12
+ fetchActiveSessions();
13
+ return activeSessions;
14
+ }
@@ -1,3 +1,4 @@
1
+ import { type Ref } from "vue";
1
2
  import type { ActiveSession, Fetch, FetchConfig, User } from "./types";
2
3
  export type { ActiveSession, Fetch, FetchConfig, User };
3
4
  export { useUser } from "./composables/use-user";
@@ -16,5 +17,33 @@ export declare function initApiFetch($fetch: Fetch, config: FetchConfig): {
16
17
  key: string;
17
18
  }>;
18
19
  logout: () => Promise<void>;
20
+ fetchActiveSessions: () => Promise<{
21
+ sessions: ActiveSession[];
22
+ }>;
19
23
  };
20
24
  };
25
+ export declare const useUserActions: () => Ref<{
26
+ signIn: (body: {
27
+ username: string;
28
+ password: string;
29
+ rememberMe: boolean;
30
+ }) => Promise<import("./types").GetUserResponse | {
31
+ key: string;
32
+ }>;
33
+ logout: () => Promise<void>;
34
+ fetchActiveSessions: () => Promise<{
35
+ sessions: ActiveSession[];
36
+ }>;
37
+ }, {
38
+ signIn: (body: {
39
+ username: string;
40
+ password: string;
41
+ rememberMe: boolean;
42
+ }) => Promise<import("./types").GetUserResponse | {
43
+ key: string;
44
+ }>;
45
+ logout: () => Promise<void>;
46
+ fetchActiveSessions: () => Promise<{
47
+ sessions: ActiveSession[];
48
+ }>;
49
+ }> | undefined;
@@ -1,4 +1,5 @@
1
1
  import { useLocalStorage } from "@vueuse/core";
2
+ import { ref } from "vue";
2
3
  import { createApiFetch } from "./api-client.mjs";
3
4
  import bus from "./bus.mjs";
4
5
  import { newCometClient } from "./comet-client.mjs";
@@ -12,13 +13,14 @@ export { useUser } from "./composables/use-user.mjs";
12
13
  export { useSubscribe } from "./composables/use-subscribe.mjs";
13
14
  export { getVisitor, getVisitorId } from "./get-visitor.mjs";
14
15
  const apiActions = useApiActions();
16
+ let userActions;
15
17
  export function initApiFetch($fetch, config) {
16
18
  const fetch = createApiFetch($fetch, config);
17
19
  const getUser = createApiGetUser(fetch, config);
18
20
  const getVisitorIdentifier = createApiGetVisitorIdentifier(config);
19
21
  const initApplication = createInitApplication();
20
22
  const refreshToken = createApiRefreshToken(fetch, config);
21
- const userActions = createApiUserActions(fetch, config, getUser);
23
+ userActions = ref(createApiUserActions(fetch, config, getUser));
22
24
  apiActions.value = {
23
25
  initialized: true,
24
26
  getVisitorIdentifier,
@@ -47,5 +49,8 @@ export function initApiFetch($fetch, config) {
47
49
  });
48
50
  }
49
51
  const useApiFetch = () => fetch;
50
- return { fetch, getUser, useApiFetch, userActions };
52
+ return { fetch, getUser, useApiFetch, userActions: userActions.value };
51
53
  }
54
+ export const useUserActions = () => {
55
+ return userActions;
56
+ };
@@ -5,8 +5,12 @@ import enUS from "vant/es/locale/lang/en-US";
5
5
  import ruRU from "vant/es/locale/lang/ru-RU";
6
6
  import { watch } from "vue";
7
7
  import { useI18n } from "vue-i18n";
8
+ import { useRouter } from "vue-router";
9
+ import { useActiveSessions } from "./composables/use-active-sessions.mjs";
10
+ import { useSubscribe } from "./composables/use-subscribe.mjs";
8
11
  import { useUser } from "./composables/use-user.mjs";
9
12
  import { once } from "./helpers/index.mjs";
13
+ import { getVisitorId, useUserActions } from "./index.mjs";
10
14
  function initApplication() {
11
15
  const user = useUser();
12
16
  const theme = useLocalStorage("app-preferences-theme", JSON.stringify({ value: "" }));
@@ -57,6 +61,33 @@ function initApplication() {
57
61
  { immediate: true }
58
62
  );
59
63
  setNotifyDefaultOptions({ position: "bottom" });
64
+ useSubscribe("WS.SessionsUpdated", async (session) => {
65
+ const user2 = useUser();
66
+ if (!user2.value) {
67
+ return;
68
+ }
69
+ const activeSessions = useActiveSessions();
70
+ if (activeSessions.value.length > 0) {
71
+ activeSessions.value = [...activeSessions.value].map((activeSession) => {
72
+ if (activeSession.id === session?.sessionId) {
73
+ activeSession.isOnline = session?.status === "Online";
74
+ activeSession.signedOut = session?.status === "SignedOut";
75
+ activeSession.dateTime = /* @__PURE__ */ new Date();
76
+ }
77
+ return activeSession;
78
+ });
79
+ }
80
+ if (session?.status === "SignedOut" && session?.visitorId === getVisitorId()) {
81
+ const userActions = useUserActions();
82
+ const data = await userActions?.value.fetchActiveSessions();
83
+ const isCurrentSession = data?.sessions.some((s) => s.isCurrentSession && s.id === session?.sessionId);
84
+ if (isCurrentSession) {
85
+ await userActions?.value.logout();
86
+ const router = useRouter();
87
+ await router.push({ path: "/" });
88
+ }
89
+ }
90
+ });
60
91
  }
61
92
  export const createInitApplication = once(() => {
62
93
  return () => initApplication();
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inzombieland/api",
3
- "version": "1.16.24",
3
+ "version": "1.16.25",
4
4
  "type": "module",
5
5
  "license": "ISC",
6
6
  "main": "./index.mjs",
@@ -1,4 +1,5 @@
1
- import type { Fetch, FetchConfig, GetUserResponse } from "./types";
1
+ import type { ActiveSession, Fetch, FetchConfig, GetUserResponse } from "./types";
2
+ export type UserActions = ReturnType<typeof createApiUserActions>;
2
3
  export declare const createApiUserActions: (fetch: Fetch, config: FetchConfig, getUser: () => Promise<GetUserResponse>) => {
3
4
  signIn: (body: {
4
5
  username: string;
@@ -8,4 +9,7 @@ export declare const createApiUserActions: (fetch: Fetch, config: FetchConfig, g
8
9
  key: string;
9
10
  }>;
10
11
  logout: () => Promise<void>;
12
+ fetchActiveSessions: () => Promise<{
13
+ sessions: ActiveSession[];
14
+ }>;
11
15
  };
@@ -29,6 +29,9 @@ export const createApiUserActions = once(
29
29
  } finally {
30
30
  flush();
31
31
  }
32
+ },
33
+ fetchActiveSessions: async () => {
34
+ return await fetch("/account/1/sessions");
32
35
  }
33
36
  };
34
37
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inzombieland/nuxt-common",
3
- "version": "1.16.24",
3
+ "version": "1.16.25",
4
4
  "license": "ISC",
5
5
  "type": "module",
6
6
  "exports": {