@sowell/quasar-app-extension-auth 0.2.1 → 0.2.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sowell/quasar-app-extension-auth",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "A Quasar app extension to manage Authentication",
5
5
  "main": "src/index.js",
6
6
  "repository": "https://github.com/sowell-ai/quasar.extension.auth",
@@ -9,8 +9,10 @@ export default boot(({ router, store }: BootInterface) => {
9
9
  } = prompts;
10
10
 
11
11
  router.beforeEach((to, from, next) => {
12
- const token = store.state.auth.token;
12
+ let isValid = store.getters["token/getIsValid"]
13
+ let token = store.getters["token/getRaw"]
13
14
  if (
15
+ !isValid &&
14
16
  !token &&
15
17
  to.path !== LOCAL_LOGIN_ROUTE &&
16
18
  to.path !== LOCAL_CHECK_CODE_ROUTE
package/src/boot/store.ts CHANGED
@@ -1,10 +1,10 @@
1
1
  //@ts-ignore
2
2
  import { boot } from "quasar/wrappers";
3
3
  import { BootInterface } from "../interfaces";
4
- import authModule from "../store";
4
+ import tokenModule from "../store";
5
5
 
6
6
  export default boot(({ app, store }: BootInterface) => {
7
7
  // Register store module
8
- store.registerModule("auth", authModule);
8
+ store.registerModule("token", tokenModule);
9
9
  app.provide("$store", store);
10
10
  });
@@ -136,11 +136,11 @@ export default defineComponent({
136
136
  } = $prompts();
137
137
  const $q = useQuasar();
138
138
  const { push, replace } = $router();
139
- const { state, dispatch } = $store();
139
+ const { dispatch, getters } = $store();
140
140
  const { t } = useI18n();
141
141
 
142
142
  onMounted(() => {
143
- if (state.auth.token) {
143
+ if (getters["token/getRaw"]) {
144
144
  replace(LOCAL_SUCCESS_REDIRECTION_ROUTE);
145
145
  }
146
146
  });
@@ -188,11 +188,7 @@ export default defineComponent({
188
188
  post(AUTH_SERVER_SIGNING_ROUTE, data)
189
189
  .then((response: AxiosResponse) => {
190
190
  const token = response.data.jwt;
191
- dispatch("auth/updateToken", token);
192
- const payload = parseJwt(token);
193
- const user = { roles: payload.roles };
194
- dispatch("auth/updateUser", user);
195
-
191
+ dispatch("token/updateRaw", token);
196
192
  push(LOCAL_SUCCESS_REDIRECTION_ROUTE);
197
193
  })
198
194
  .catch((error: AxiosError) => {
@@ -214,10 +210,6 @@ export default defineComponent({
214
210
  );
215
211
  };
216
212
 
217
- const parseJwt = (token: string) => {
218
- return JSON.parse(window.atob(token.split(".")[1]));
219
- };
220
-
221
213
  return {
222
214
  email,
223
215
  password,
@@ -0,0 +1,11 @@
1
+ export interface payload {
2
+ company_hp?: number
3
+ exp?: number
4
+ gm_key: string
5
+ roles: string[] | []
6
+ sub: number | null
7
+ }
8
+ export interface tokenStateInterface {
9
+ raw: string
10
+ payload: payload
11
+ }
@@ -1,2 +1,2 @@
1
1
  export * from "./Boot";
2
- export * from "./AuthState";
2
+ export * from "./TokenStateInterface";
@@ -1,16 +1,56 @@
1
- import { Module } from "vuex";
2
- import state from "./state";
3
- import actions from "./actions";
4
- import getters from "./getters";
5
- import mutations from "./mutations";
6
- import { AuthStateInterface } from "../interfaces";
1
+ import { Module, GetterTree, MutationTree, ActionTree } from "vuex";
2
+ import { tokenStateInterface, payload } from "../interfaces";
7
3
 
8
- const authModule: Module<AuthStateInterface, any> = {
4
+ const state: tokenStateInterface = {
5
+ raw: "",
6
+ payload: {
7
+ company_hp: undefined,
8
+ exp: undefined,
9
+ gm_key: "",
10
+ roles: [],
11
+ sub: null
12
+ }
13
+ }
14
+
15
+ const getters: GetterTree<tokenStateInterface, any> = {
16
+ getRaw(state) {
17
+ return state.raw
18
+ },
19
+ getPayload(state) {
20
+ return state.payload
21
+ },
22
+ getIsValid(state) {
23
+ if (state.payload.exp) {
24
+ return new Date(state.payload.exp * 1000) >= new Date() ? true : false;
25
+ } else {
26
+ return false
27
+ }
28
+ }
29
+ }
30
+
31
+ const mutations: MutationTree<tokenStateInterface> = {
32
+ setRaw(state, value: string) {
33
+ state.raw = value
34
+ },
35
+ setPayload(state, value: payload) {
36
+ state.payload = {...value}
37
+ }
38
+ }
39
+
40
+ const actions: ActionTree<tokenStateInterface, any> = {
41
+ updateRaw({ commit }, value: string) {
42
+ commit("setRaw", value);
43
+ const parseJwt = JSON.parse(window.atob(value.split(".")[1]));
44
+ commit("setPayload", parseJwt)
45
+ }
46
+ }
47
+
48
+ const tokenModule: Module<tokenStateInterface, any> = {
9
49
  namespaced: true,
10
- actions,
11
50
  getters,
12
51
  mutations,
13
- state,
14
- };
52
+ actions,
53
+ state
54
+ }
15
55
 
16
- export default authModule;
56
+ export default tokenModule;
@@ -1,7 +0,0 @@
1
- export interface UserInterface {
2
- roles: string[];
3
- }
4
- export interface AuthStateInterface {
5
- token: string | null;
6
- user: UserInterface;
7
- }
@@ -1,13 +0,0 @@
1
- import { ActionTree } from "vuex";
2
- import { AuthStateInterface, UserInterface } from "../interfaces";
3
-
4
- const actions: ActionTree<AuthStateInterface, any> = {
5
- updateToken({ commit }, value: string) {
6
- commit("setToken", value);
7
- },
8
- updateUser({ commit }, value: UserInterface) {
9
- commit("setUser", value);
10
- },
11
- };
12
-
13
- export default actions;
@@ -1,13 +0,0 @@
1
- import { GetterTree } from "vuex";
2
- import { AuthStateInterface } from "../interfaces";
3
-
4
- const getters: GetterTree<AuthStateInterface, any> = {
5
- getToken(state) {
6
- return state.token;
7
- },
8
- getUser(state) {
9
- return state.user;
10
- },
11
- };
12
-
13
- export default getters;
@@ -1,13 +0,0 @@
1
- import { MutationTree } from "vuex";
2
- import { AuthStateInterface, UserInterface } from "../interfaces";
3
-
4
- const mutation: MutationTree<AuthStateInterface> = {
5
- setToken(state, value: string) {
6
- return (state.token = value);
7
- },
8
- setUser(state, value: UserInterface) {
9
- return (state.user = value);
10
- },
11
- };
12
-
13
- export default mutation;
@@ -1,12 +0,0 @@
1
- import { AuthStateInterface } from "../interfaces";
2
-
3
- function state(): AuthStateInterface {
4
- return {
5
- token: "",
6
- user: {
7
- roles: [],
8
- },
9
- };
10
- }
11
-
12
- export default state;