bt-core-app 1.2.4 → 1.2.6

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.
@@ -29,12 +29,20 @@ export interface CreateAuthOptions {
29
29
  demo?: BTDemo;
30
30
  /**expiry token date format. Defaults to 'd/MM/yyyy h:mm:ss a' */
31
31
  expiryTokenFormat?: string;
32
- /**retrieve the auth item */
33
- getAuthItem: (navName?: string | AuthItem) => AuthItem | null;
34
- /**retrieve the url to navigate to for the OAuth 2.0 process */
35
- getAuthQuery: (redirectPath?: string, state?: string) => string;
36
- /**sets current credentials on top of default function*/
37
- setCredentials?: (state: RemovableRef<any>, payload: any) => void;
32
+ /**OVERRIDES CORE DEFAULT. retrieve the auth item */
33
+ getAuthItem?: (navName?: string | AuthItem) => AuthItem | null;
34
+ /**OVERRIDES DEFAULT. the url to start the OAuth 2.0 process */
35
+ getAuthorizeUrl?: (redirectPath?: string, state?: string) => string;
36
+ /**OVERRIDES DEFAULT. use the given code and generate the url to convert to an access token */
37
+ getTokenUrl?: (code: string, redirect_uri: string, grant_type: string, client_id: string) => string;
38
+ /**OVERRIDES DEFAULT. */
39
+ getToken?: () => Promise<void>;
40
+ oauthGrantType?: string;
41
+ oauthClientID?: string;
42
+ /**sets current credentials on top of default function
43
+ * for processing the token payload and applying to state
44
+ */
45
+ processTokenPayload?: (state: RemovableRef<any>, payload: any) => void;
38
46
  /**suboptions */
39
47
  subscriptionOptions?: AuthSubscription[];
40
48
  }
@@ -47,8 +55,9 @@ export interface BTAuth {
47
55
  credentials: any;
48
56
  doShow: (subcodes?: string[], permissions?: string[], action?: 'view' | 'edit') => boolean;
49
57
  doShowByNav: (navName?: string | AuthItem, includeChildren?: boolean) => boolean;
50
- getAuthUrl: (redirectPath?: string) => string;
58
+ getAuthorizeUrl: (redirectPath?: string) => string;
51
59
  getTimeZone: () => string;
60
+ getToken: () => Promise<void>;
52
61
  login: (redirectPath?: string) => void;
53
62
  logout: (navNameRedirect?: string) => void;
54
63
  setAuth: (jwtToken?: string) => void;
package/dist/core.d.ts CHANGED
@@ -1,19 +1,17 @@
1
- import { RemovableRef } from '@vueuse/core';
2
1
  import { CreateUrlOptions } from './composables/urls';
3
2
  import { NavigationItem } from './composables/navigation';
4
3
  import { BaseCosmeticTheme, UseCosmeticsOptions } from './composables/cosmetics';
5
- import { AuthSubscription } from './composables/auth';
4
+ import { CreateAuthOptions, AuthSubscription } from './composables/auth';
6
5
  import { App } from 'vue';
7
6
 
8
7
  export interface CoreApp {
9
8
  install(app: App): void;
10
9
  }
11
10
  export interface CreateCoreOptions extends UseCosmeticsOptions<BaseCosmeticTheme> {
11
+ auth: CreateAuthOptions;
12
12
  defaultCacheExpiryHours?: number;
13
- getAuthQuery: (redirectPath?: string, state?: string) => string;
14
13
  navItems?: NavigationItem[];
15
14
  presets: any;
16
- setCredentials?: (state: RemovableRef<any>, payload: any) => void;
17
15
  /**suboptions */
18
16
  subscriptionOptions?: AuthSubscription[];
19
17
  urls: CreateUrlOptions;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bt-core-app",
3
- "version": "1.2.4",
3
+ "version": "1.2.6",
4
4
  "description": "Core app tools for some basic features like navigation, authentication, server apis, and cosmetics",
5
5
  "homepage": "https://github.com/BlitzItTech/bt-core",
6
6
  "bugs": {
@@ -1,8 +1,8 @@
1
- import { isLengthyArray } from '../composables/helpers'
1
+ import { appendUrl, isLengthyArray } from '../composables/helpers'
2
2
  import { DateTime } from 'luxon'
3
3
  import { type RemovableRef, useStorage } from '@vueuse/core'
4
4
  import { type BTDemo } from '../composables/demo'
5
- import { useRouter } from 'vue-router'
5
+ import { useRoute, useRouter } from 'vue-router'
6
6
  import { toValue } from 'vue'
7
7
  import { useAuthUrl } from './urls'
8
8
 
@@ -41,12 +41,20 @@ export interface CreateAuthOptions {
41
41
  demo?: BTDemo,
42
42
  /**expiry token date format. Defaults to 'd/MM/yyyy h:mm:ss a' */
43
43
  expiryTokenFormat?: string
44
- /**retrieve the auth item */
45
- getAuthItem: (navName?: string | AuthItem) => AuthItem | null
46
- /**retrieve the url to navigate to for the OAuth 2.0 process */
47
- getAuthQuery: (redirectPath?: string, state?: string) => string
48
- /**sets current credentials on top of default function*/
49
- setCredentials?: (state: RemovableRef<any>, payload: any) => void
44
+ /**OVERRIDES CORE DEFAULT. retrieve the auth item */
45
+ getAuthItem?: (navName?: string | AuthItem) => AuthItem | null
46
+ /**OVERRIDES DEFAULT. the url to start the OAuth 2.0 process */
47
+ getAuthorizeUrl?: (redirectPath?: string, state?: string) => string
48
+ /**OVERRIDES DEFAULT. use the given code and generate the url to convert to an access token */
49
+ getTokenUrl?: (code: string, redirect_uri: string, grant_type: string, client_id: string) => string
50
+ /**OVERRIDES DEFAULT. */
51
+ getToken?: () => Promise<void>
52
+ oauthGrantType?: string
53
+ oauthClientID?: string
54
+ /**sets current credentials on top of default function
55
+ * for processing the token payload and applying to state
56
+ */
57
+ processTokenPayload?: (state: RemovableRef<any>, payload: any) => void
50
58
  /**suboptions */
51
59
  subscriptionOptions?: AuthSubscription[]
52
60
  }
@@ -60,8 +68,9 @@ export interface BTAuth {
60
68
  credentials: any
61
69
  doShow: (subcodes?: string[], permissions?: string[], action?: 'view' | 'edit') => boolean
62
70
  doShowByNav: (navName?: string | AuthItem, includeChildren?: boolean) => boolean
63
- getAuthUrl: (redirectPath?: string) => string
71
+ getAuthorizeUrl: (redirectPath?: string) => string
64
72
  getTimeZone: () => string
73
+ getToken: () => Promise<void>
65
74
  login: (redirectPath?: string) => void
66
75
  logout: (navNameRedirect?: string) => void
67
76
  setAuth: (jwtToken?: string) => void
@@ -83,7 +92,7 @@ export function createAuth(options: CreateAuthOptions): BTAuth {
83
92
  function canEdit(navName?: string): boolean {
84
93
  if (navName == null) return true
85
94
 
86
- var navItem = options.getAuthItem(navName);
95
+ var navItem = options.getAuthItem != null ? options.getAuthItem(navName) : null;
87
96
  if (navItem == null) return false
88
97
  if (navItem.requiresAuth === false) return true
89
98
 
@@ -125,7 +134,7 @@ export function createAuth(options: CreateAuthOptions): BTAuth {
125
134
  function canView(navName: string | undefined): boolean {
126
135
  if (navName == null) return true
127
136
 
128
- var navItem = options.getAuthItem(navName);
137
+ var navItem = options.getAuthItem != null ? options.getAuthItem(navName) : null;
129
138
  if (navItem == null) return false
130
139
  if (navItem.requiresAuth === false) return true
131
140
 
@@ -199,7 +208,7 @@ export function createAuth(options: CreateAuthOptions): BTAuth {
199
208
  }
200
209
 
201
210
  function doShowByNav(navName?: string | AuthItem, includeChildren: boolean = false) {
202
- var navItem = options.getAuthItem(navName);
211
+ var navItem = options.getAuthItem != null ? options.getAuthItem(navName) : null;
203
212
  let doShowRes = false;
204
213
 
205
214
  if (navItem == null) return false
@@ -222,9 +231,15 @@ export function createAuth(options: CreateAuthOptions): BTAuth {
222
231
  return doShowRes;
223
232
  }
224
233
 
225
- function getAuthUrl(redirectPath?: string) {
226
- return `${useAuthUrl()}?${options.getAuthQuery(redirectPath, authState.value)}`
227
- //return options.getAuthUrl(redirectPath, authState.value)
234
+ function getAuthorizeUrl(redirectPath?: string) {
235
+ options.getAuthorizeUrl ??= (redirectPath?: string, state?: string) => {
236
+ return appendUrl(useAuthUrl(), `authorize?response_type=code&client_id=${options.oauthClientID}&redirect_uri=${redirectPath}&state=${state}`)
237
+ }
238
+
239
+ if (options.getAuthorizeUrl != null)
240
+ return options.getAuthorizeUrl(redirectPath, authState.value) ?? ''
241
+
242
+ return ''
228
243
  }
229
244
 
230
245
  /**undefined or unfound is worth 0 */
@@ -289,11 +304,58 @@ export function createAuth(options: CreateAuthOptions): BTAuth {
289
304
  options.demo?.endDemo()
290
305
  if (tokenExpired()) {
291
306
  logout()
292
- window.location.href = getAuthUrl(redirectPath) //}response_type=code&client_id=appClient1&redirect_path=${redirectPath}` : getAuthUrl()
307
+ window.location.href = getAuthorizeUrl(redirectPath) //}response_type=code&client_id=appClient1&redirect_path=${redirectPath}` : getAuthUrl()
293
308
  }
294
309
  }
295
310
  }
296
311
 
312
+ /**defaults to find parameters from route query */
313
+ async function getToken(code?: string, state?: string, redirect_path?: string) {
314
+ options.getToken ??= async () => {
315
+ const route = useRoute()
316
+
317
+ let mCode = code ?? route.query?.code as string
318
+ let mState = state ?? route.query?.state as string
319
+ let mRedirect = redirect_path ?? route.query?.redirect_path as string
320
+ let mGrantType = options.oauthGrantType ?? 'authorization_token'
321
+ let mClientID = options.oauthClientID ?? ''
322
+
323
+ if (mCode == null || mState == null) {
324
+ throw new Error('Code and State required in OAuth token process')
325
+ }
326
+
327
+ if (mState != authState.value)
328
+ throw new Error('state does not match')
329
+
330
+ let url = ''
331
+
332
+ options.getTokenUrl ??= () => {
333
+ return appendUrl(useAuthUrl(), 'token')
334
+ }
335
+
336
+ url = options.getTokenUrl(mCode, mRedirect, mGrantType, mClientID)
337
+
338
+ const formData: any = {}
339
+ formData['grant_type'] = mGrantType
340
+ formData['code'] = mCode
341
+ formData['redirect_uri'] = `${window.location.origin}/authentication`
342
+ formData['client_id'] = mClientID
343
+
344
+ const res = await fetch(url, {
345
+ method: 'POST',
346
+ mode: 'cors',
347
+ cache: 'no-cache',
348
+ body: JSON.stringify(formData)
349
+ })
350
+
351
+ const data = await res.json()
352
+
353
+ setAuth(data.value?.access_token)
354
+ }
355
+
356
+ return await getToken(code, state, redirect_path)
357
+ }
358
+
297
359
  /**if not demoing, then will decrypt the jwtToken and attempt to set credentials */
298
360
  function setAuth(jwtToken?: string) {
299
361
  if (jwtToken == null) return
@@ -303,9 +365,8 @@ export function createAuth(options: CreateAuthOptions): BTAuth {
303
365
 
304
366
  setDefaultAuth(d)
305
367
 
306
- if (options.setCredentials != null) {
307
- options.setCredentials(state as RemovableRef<any>, d)
308
- }
368
+ if (options.processTokenPayload != null)
369
+ options.processTokenPayload(state, d)
309
370
  }
310
371
  }
311
372
 
@@ -341,7 +402,7 @@ export function createAuth(options: CreateAuthOptions): BTAuth {
341
402
  //what if no internet?
342
403
  if (mState.isLoggedIn && tokenExpired()) {
343
404
  logout()
344
- window.location.href = getAuthUrl()
405
+ window.location.href = getAuthorizeUrl()
345
406
  }
346
407
 
347
408
  return mState.isLoggedIn
@@ -367,8 +428,9 @@ export function createAuth(options: CreateAuthOptions): BTAuth {
367
428
  credentials: state.value,
368
429
  doShow,
369
430
  doShowByNav,
370
- getAuthUrl,
431
+ getAuthorizeUrl,
371
432
  getTimeZone,
433
+ getToken,
372
434
  login,
373
435
  logout,
374
436
  setAuth,
package/src/core.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { App } from 'vue'
2
2
  import { createApi } from './composables/api'
3
- import { createAuth, type AuthSubscription } from './composables/auth'
3
+ import { CreateAuthOptions, createAuth, type AuthSubscription } from './composables/auth'
4
4
  import { BaseCosmeticTheme, createCosmetics, UseCosmeticsOptions } from './composables/cosmetics'
5
5
  import { createDates } from './composables/dates'
6
6
  import { createDemo } from './composables/demo'
@@ -10,7 +10,6 @@ import { createPresets } from './composables/presets'
10
10
  import { createPWA } from './composables/pwa'
11
11
  import { createStoreBuilder } from './composables/stores'
12
12
  import { CreateUrlOptions, createUrl } from './composables/urls'
13
- import { RemovableRef } from '@vueuse/core'
14
13
 
15
14
 
16
15
  import BTBtn from './components/BT-Btn.vue'
@@ -36,11 +35,12 @@ export interface CoreApp {
36
35
  }
37
36
 
38
37
  export interface CreateCoreOptions extends UseCosmeticsOptions<BaseCosmeticTheme> {
38
+ auth: CreateAuthOptions
39
39
  defaultCacheExpiryHours?: number
40
- getAuthQuery: (redirectPath?: string, state?: string) => string
40
+ // getAuthQuery: (redirectPath?: string, state?: string) => string
41
41
  navItems?: NavigationItem[]
42
42
  presets: any
43
- setCredentials?: (state: RemovableRef<any>, payload: any) => void
43
+ // setCredentials?: (state: RemovableRef<any>, payload: any) => void
44
44
  /**suboptions */
45
45
  subscriptionOptions?: AuthSubscription[]
46
46
  urls: CreateUrlOptions
@@ -50,7 +50,6 @@ export function createCore(options: CreateCoreOptions): CoreApp {
50
50
 
51
51
  return {
52
52
  install(app: App) {
53
- // const core = this
54
53
 
55
54
  //register components
56
55
  app.component('bt-btn', BTBtn)
@@ -82,13 +81,18 @@ export function createCore(options: CreateCoreOptions): CoreApp {
82
81
 
83
82
  createPresets(options)
84
83
 
85
- const auth = createAuth({
86
- demo: demo,
87
- getAuthItem: navigation.findItem,
88
- getAuthQuery: options.getAuthQuery,
89
- setCredentials: options.setCredentials,
90
- subscriptionOptions: options.subscriptionOptions
91
- })
84
+ options.auth.demo ??= demo
85
+ options.auth.getAuthItem = navigation.findItem
86
+
87
+ const auth = createAuth(options.auth)
88
+
89
+ // {
90
+ // demo: demo,
91
+ // getAuthItem: navigation.findItem,
92
+ // getAuthorizeUrl: options.auth
93
+ // setCredentials: options.setCredentials,
94
+ // subscriptionOptions: options.subscriptionOptions
95
+ // })
92
96
 
93
97
  const api = createApi({
94
98
  auth: auth,