@sfxcode/nuxt-mongocamp-server 1.0.9 → 1.0.11

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.d.mts CHANGED
@@ -3,6 +3,8 @@ import * as _nuxt_schema from '@nuxt/schema';
3
3
  interface ModuleOptions {
4
4
  url: string;
5
5
  paginationSize?: number;
6
+ refreshToken: boolean;
7
+ tokenRefreshIntervall: number;
6
8
  }
7
9
  declare const _default: _nuxt_schema.NuxtModule<ModuleOptions>;
8
10
 
package/dist/module.d.ts CHANGED
@@ -3,6 +3,8 @@ import * as _nuxt_schema from '@nuxt/schema';
3
3
  interface ModuleOptions {
4
4
  url: string;
5
5
  paginationSize?: number;
6
+ refreshToken: boolean;
7
+ tokenRefreshIntervall: number;
6
8
  }
7
9
  declare const _default: _nuxt_schema.NuxtModule<ModuleOptions>;
8
10
 
package/dist/module.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sfxcode/nuxt-mongocamp-server",
3
- "version": "1.0.9",
3
+ "version": "1.0.11",
4
4
  "configKey": "mongocamp",
5
5
  "compatibility": {
6
6
  "nuxt": "^3.0.0"
package/dist/module.mjs CHANGED
@@ -534,7 +534,7 @@ function createConsola(options = {}) {
534
534
  const consola = createConsola();
535
535
 
536
536
  const name = "@sfxcode/nuxt-mongocamp-server";
537
- const version = "1.0.9";
537
+ const version = "1.0.11";
538
538
 
539
539
  const module = defineNuxtModule({
540
540
  meta: {
@@ -552,11 +552,16 @@ const module = defineNuxtModule({
552
552
  if (!options.paginationSize || options.paginationSize < 10) {
553
553
  options.paginationSize = 500;
554
554
  }
555
+ if (!options.tokenRefreshIntervall || options.tokenRefreshIntervall < 5e3) {
556
+ options.tokenRefreshIntervall = 5e3;
557
+ }
555
558
  nuxt.options.runtimeConfig.public.mongocamp = defu$1(
556
559
  nuxt.options.runtimeConfig.public.mongocamp,
557
560
  {
558
561
  url: options.url,
559
- paginationSize: options.paginationSize
562
+ paginationSize: options.paginationSize,
563
+ refreshToken: options.refreshToken,
564
+ tokenRefreshIntervall: options.tokenRefreshIntervall
560
565
  }
561
566
  );
562
567
  const { resolve } = createResolver(import.meta.url);
@@ -2,6 +2,7 @@ import type { UserProfile } from '../api';
2
2
  export declare function useMongocampAuth(): {
3
3
  login: (loginId: string, loginPassword: string) => Promise<UserProfile>;
4
4
  logout: () => void;
5
+ isAuthenticated: () => Promise<import("../api").JsonValueBoolean>;
5
6
  isLoggedIn: import("vue").ComputedRef<boolean>;
6
7
  userRoles: import("vue").ComputedRef<string[]>;
7
8
  userGrants: import("vue").ComputedRef<import("../api").Grant[]>;
@@ -1,21 +1,39 @@
1
1
  import { computed } from "vue";
2
+ import { useRuntimeConfig } from "#app";
2
3
  import { useMongocampApi } from "./mongocampApi.mjs";
3
4
  import { useMongocampStorage } from "./mongocampStorage.mjs";
4
5
  import { useMongocampUser } from "./mongocampUser.mjs";
5
6
  export function useMongocampAuth() {
7
+ const config = useRuntimeConfig();
8
+ const refreshToken = config.public.mongocamp?.refreshToken;
9
+ const tokenRefreshIntervall = config.public.mongocamp?.tokenRefreshIntervall;
6
10
  const state = useMongocampStorage();
7
11
  const { authApi } = useMongocampApi();
8
12
  const user = useMongocampUser();
13
+ let updateTokenInterval;
9
14
  async function isAuthenticated() {
10
15
  return await authApi.isAuthenticated();
11
16
  }
17
+ async function updateToken() {
18
+ try {
19
+ const result = await authApi.refreshToken();
20
+ updateUserState(result);
21
+ } catch (e) {
22
+ }
23
+ }
24
+ function updateUserState(result) {
25
+ state.value.token = result.authToken;
26
+ state.value.profile = result.userProfile;
27
+ user.value = result.userProfile;
28
+ }
12
29
  const login = async (loginId, loginPassword) => {
13
30
  logout();
14
31
  const login2 = { userId: loginId, password: loginPassword };
15
32
  const result = await authApi.login({ login: login2 });
16
- state.value.token = result.authToken;
17
- state.value.profile = result.userProfile;
18
- user.value = result.userProfile;
33
+ updateUserState(result);
34
+ if (refreshToken) {
35
+ updateTokenInterval = window.setInterval(updateToken, tokenRefreshIntervall);
36
+ }
19
37
  return result.userProfile;
20
38
  };
21
39
  const logout = () => {
@@ -23,9 +41,20 @@ export function useMongocampAuth() {
23
41
  const profile = { user: "", isAdmin: false };
24
42
  state.value.profile = profile;
25
43
  user.value = profile;
44
+ if (refreshToken) {
45
+ window.clearInterval(updateTokenInterval);
46
+ }
26
47
  };
27
- const isLoggedIn = computed(() => state.value?.token?.length > 0);
48
+ const isLoggedIn = computed(() => {
49
+ const result = state.value?.token?.length > 0;
50
+ if (result && refreshToken && !updateTokenInterval) {
51
+ updateTokenInterval = window.setInterval(updateToken, tokenRefreshIntervall);
52
+ } else if (!result && updateTokenInterval) {
53
+ window.clearInterval(updateTokenInterval);
54
+ }
55
+ return result;
56
+ });
28
57
  const userRoles = computed(() => state.value.profile.roles ?? []);
29
58
  const userGrants = computed(() => state.value.profile.grants ?? []);
30
- return { login, logout, isLoggedIn, userRoles, userGrants };
59
+ return { login, logout, isAuthenticated, isLoggedIn, userRoles, userGrants };
31
60
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sfxcode/nuxt-mongocamp-server",
3
- "version": "1.0.9",
3
+ "version": "1.0.11",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "exports": {