@progressive-development/pd-spa-helper 0.1.85 → 0.1.87

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.
Files changed (42) hide show
  1. package/dist/src/InitApplicationData.d.ts +1 -1
  2. package/dist/src/InitApplicationData.js +2 -2
  3. package/dist/src/InitApplicationData.js.map +1 -1
  4. package/dist/src/PdSpaHelper.d.ts +8 -4
  5. package/dist/src/PdSpaHelper.js +19 -36
  6. package/dist/src/PdSpaHelper.js.map +1 -1
  7. package/dist/src/index.d.ts +3 -3
  8. package/dist/src/index.js +3 -3
  9. package/dist/src/index.js.map +1 -1
  10. package/dist/src/service-call-controller2.d.ts +1 -1
  11. package/dist/src/service-call-controller2.js +1 -1
  12. package/dist/src/service-call-controller2.js.map +1 -1
  13. package/dist/src/service-provider/firebase/auth.d.ts +5 -0
  14. package/dist/src/service-provider/firebase/auth.js +48 -0
  15. package/dist/src/service-provider/firebase/auth.js.map +1 -0
  16. package/dist/src/service-provider/firebase/firestore-client.d.ts +9 -0
  17. package/dist/src/service-provider/firebase/firestore-client.js +19 -0
  18. package/dist/src/service-provider/firebase/firestore-client.js.map +1 -0
  19. package/dist/src/service-provider/firebase/functions-client.d.ts +34 -0
  20. package/dist/src/service-provider/firebase/functions-client.js +70 -0
  21. package/dist/src/service-provider/firebase/functions-client.js.map +1 -0
  22. package/dist/src/service-provider/mock/auth.d.ts +4 -0
  23. package/dist/src/service-provider/mock/auth.js +50 -0
  24. package/dist/src/service-provider/mock/auth.js.map +1 -0
  25. package/dist/src/service-provider/service-provider-impl.d.ts +6 -0
  26. package/dist/src/service-provider/service-provider-impl.js +56 -0
  27. package/dist/src/service-provider/service-provider-impl.js.map +1 -0
  28. package/dist/src/tmpown/pd-login.js +2 -2
  29. package/dist/src/tmpown/pd-login.js.map +1 -1
  30. package/dist/tsconfig.tsbuildinfo +1 -1
  31. package/package.json +1 -1
  32. package/src/InitApplicationData.ts +2 -2
  33. package/src/PdSpaHelper.ts +33 -41
  34. package/src/index.ts +6 -6
  35. package/src/service-call-controller2.ts +1 -1
  36. package/src/service-provider/firebase/auth.ts +52 -0
  37. package/src/{firebase → service-provider/firebase}/functions-client.ts +1 -1
  38. package/src/service-provider/mock/auth.ts +68 -0
  39. package/src/service-provider/service-provider-impl.ts +68 -0
  40. package/src/tmpown/pd-login.ts +3 -2
  41. package/src/firebase/auth.ts +0 -30
  42. /package/src/{firebase → service-provider/firebase}/firestore-client.ts +0 -0
@@ -1,21 +1,21 @@
1
1
  import { FirebaseApp, FirebaseOptions, initializeApp } from 'firebase/app';
2
- import { getAuth, onAuthStateChanged, User } from 'firebase/auth';
3
2
  // TODO: Add SDKs for Firebase products that you want to use
4
3
  // https://firebase.google.com/docs/web/setup#available-libraries
5
4
 
6
5
 
7
6
  import { html, LitElement, css, CSSResultGroup, TemplateResult } from 'lit';
8
7
  import { router, navigator } from 'lit-element-router';
9
- import { property, query } from 'lit/decorators.js';
8
+ import { property } from 'lit/decorators.js';
10
9
 
11
10
  import '@progressive-development/pd-page/pd-menu.js';
12
11
  import '@progressive-development/pd-page/pd-footer.js';
13
12
 
14
13
  import { initApplicationServices } from './InitApplicationData.js';
15
14
 
16
- import { isAuthenticated, logout } from './firebase/auth.js';
15
+ // Refactor: move function definitions to general place (no firebase specific imports here)
16
+ import { FunctionDefinition, FunctionResult, FunctionsConfig } from './service-provider/firebase/functions-client.js';
17
+
17
18
  import { ServiceCallController } from './service-call-controller2.js';
18
- import { callFunction, FunctionDefinition, FunctionResult, FunctionsConfig } from './firebase/functions-client.js';
19
19
  import { initializeStore } from './store/mini-rx.store.js';
20
20
 
21
21
  import './router/AppMain.js';
@@ -28,6 +28,7 @@ import './tmpown/pd-panel.js';
28
28
  import { PdToast } from './tmpown/pd-toast.js';
29
29
  // eslint-disable-next-line import/no-duplicates
30
30
  import './tmpown/pd-toast.js';
31
+ import { authStateChangedImpl, isAuthenticatedImpl, setServiceProvider } from './service-provider/service-provider-impl.js';
31
32
 
32
33
 
33
34
  // Footer Text
@@ -65,6 +66,10 @@ export interface NavigationPage {
65
66
  hideMenu?: boolean
66
67
  }
67
68
 
69
+ export type ServiceProviderFirebase = "firebase";
70
+ export type ServiceProviderMock = "mock";
71
+ export type ServiceProviderConfiguration = ServiceProviderFirebase | ServiceProviderMock;
72
+
68
73
  export interface NavigationConfig {
69
74
  pages: Array<NavigationPage>,
70
75
  includeLogin: boolean
@@ -89,6 +94,7 @@ export interface StoreConfig {
89
94
  }
90
95
 
91
96
  export interface AppConfiguration {
97
+ serviceProvider?: ServiceProviderConfiguration,
92
98
  navigationConfigParam: NavigationConfig;
93
99
  storeConfigParam?: StoreConfig;
94
100
  firebaseConfig?: FirebaseOptions;
@@ -113,7 +119,7 @@ const transformRoutes = () => {
113
119
  unauthenticated: {
114
120
  name: 'login',
115
121
  },
116
- authenticate: () => isAuthenticated(),
122
+ authenticate: () => isAuthenticatedImpl(),
117
123
  } : undefined;
118
124
 
119
125
  let generatedRoutes:Array<any> = [];
@@ -147,6 +153,9 @@ const transformRoutes = () => {
147
153
  * @param functionsConfig
148
154
  */
149
155
  export const startInit = (config: AppConfiguration) => {
156
+
157
+ // set service provider
158
+ setServiceProvider(config.serviceProvider);
150
159
 
151
160
  // init mini-rx store
152
161
  if (config.storeConfigParam) {
@@ -198,7 +207,7 @@ export abstract class PdSpaHelper extends router(navigator(LitElement)) {
198
207
  * Properties needed for login/profile data.
199
208
  */
200
209
  @property({ type: Object, state: true })
201
- _user: User | undefined;
210
+ _user: unknown | undefined;
202
211
 
203
212
  @property({ type: Object, state: true })
204
213
  _profile: any | undefined;
@@ -278,11 +287,17 @@ export abstract class PdSpaHelper extends router(navigator(LitElement)) {
278
287
  transition: background-color 1s;
279
288
  }
280
289
 
281
- .default-main {
290
+ /* needed for pd-spa-padding-top, only for pages with teaser, check if refactor possible... */
291
+ .default-main-padding {
282
292
  padding-top: calc(var(--pd-menu-height) + var(--pd-spa-padding-top, 120px));
283
293
  transition: padding-top 1s;
284
294
  }
285
295
 
296
+ .default-main {
297
+ padding-top: calc(var(--pd-menu-height) + 10px);
298
+ transition: padding-top 1s;
299
+ }
300
+
286
301
  .default-main-withteaser {
287
302
  padding-top: calc(var(--pd-menu-height) + var(--pd-teaser-height, 380px) + 10px);
288
303
  transition: padding-top 1s;
@@ -350,43 +365,19 @@ export abstract class PdSpaHelper extends router(navigator(LitElement)) {
350
365
  this.params = params;
351
366
  this.query = query;
352
367
  }
353
-
368
+
369
+
354
370
  /**
355
371
  * Call to activate onAuthState change with profile request callbacks.
372
+ * Only implemented for firebase provider.
356
373
  */
357
374
  activateLoginHandler() {
358
- console.log("Activate Login Handler");
359
- // activate firebase auth handler
360
- const auth = getAuth();
361
- onAuthStateChanged(auth, user => {
362
- if (user) {
363
- this._user = user;
364
- if (postLoginFunc) {
365
- callFunction(postLoginFunc, postLoginFunc.param)
366
- .then((result:FunctionResult) => {
367
- this._profile = result.resultData;
368
- // redirect if login in the meantime is available
369
- // => else stay on the login page after reload/url navigation
370
- if (this.route === "login" && postLoginFunc?.redirect) {
371
- this.navigate(postLoginFunc.redirect);
372
- }
373
-
374
- // new event, could/should replace redirect
375
- document.dispatchEvent(new CustomEvent("user-login-event"));
376
-
377
- }).catch(() => {
378
- if (postLoginFunc?.logoutOnFail) {
379
- logout();
380
- this._user = undefined;
381
- this._profile = undefined;
382
- }
383
- });
384
- }
385
- } else {
386
- this._user = undefined;
387
- this._profile = undefined;
388
- }
389
- });
375
+ console.log("Activate Login Handler");
376
+ const callback = (user: unknown) => {
377
+ console.log("My callback user:", user);
378
+ this._user = user;
379
+ };
380
+ authStateChangedImpl(callback);
390
381
  }
391
382
 
392
383
  render() {
@@ -424,7 +415,8 @@ export abstract class PdSpaHelper extends router(navigator(LitElement)) {
424
415
  // eslint-disable-next-line class-methods-use-this
425
416
  private _getMainClass(hideTeaser: boolean, pageConf: NavigationPage): string {
426
417
  if (hideTeaser) {
427
- return pageConf.mainClass ? pageConf.mainClass : "default-main";
418
+ // eslint-disable-next-line no-nested-ternary
419
+ return pageConf.mainClass ? pageConf.mainClass : (pageConf.withTeaser ? "default-main-padding" : "default-main");
428
420
  }
429
421
  return "default-main-withteaser";
430
422
  }
package/src/index.ts CHANGED
@@ -34,17 +34,17 @@ export {
34
34
  FunctionParam,
35
35
  FunctionsConfig,
36
36
  callFunction
37
- } from './firebase/functions-client.js'
37
+ } from './service-provider/firebase/functions-client.js'
38
38
 
39
39
  export {
40
40
  getDB,
41
- } from './firebase/firestore-client.js'
41
+ } from './service-provider/firebase/firestore-client.js'
42
42
 
43
43
  export {
44
- login,
45
- logout,
46
- isAuthenticated
47
- } from './firebase/auth.js'
44
+ loginImpl,
45
+ logoutImpl,
46
+ isAuthenticatedImpl
47
+ } from './service-provider/service-provider-impl.js'
48
48
 
49
49
  export { pdStore } from './store/mini-rx.store.js';
50
50
 
@@ -1,7 +1,7 @@
1
1
  import {ReactiveControllerHost} from 'lit';
2
2
  import {initialState, Task} from '@lit-labs/task';
3
3
 
4
- import { FunctionDefinition, FunctionResult, initController } from './firebase/functions-client.js';
4
+ import { FunctionDefinition, FunctionResult, initController } from './service-provider/firebase/functions-client.js';
5
5
 
6
6
  export interface ServiceControllerInput {
7
7
  promise: Promise<FunctionResult>,
@@ -0,0 +1,52 @@
1
+ import { getAuth, onAuthStateChanged, signInWithEmailAndPassword, signOut, User } from 'firebase/auth';
2
+
3
+ export const logout = async (): Promise<boolean> => {
4
+ const auth = getAuth();
5
+ try {
6
+ await signOut(auth);
7
+ return true;
8
+ } catch (error) {
9
+ return false;
10
+ }
11
+ };
12
+
13
+ export const isAuthenticated = (): boolean =>
14
+ getAuth().currentUser !== null;
15
+
16
+
17
+ export const authStateChanged = (callback: Function) => {
18
+ onAuthStateChanged(getAuth(), (user) => callback(user));
19
+ /* Alter code aus init in pd-spa-helper, hier falsch, noch refactorieren
20
+ if (user) {
21
+ this._user = user;
22
+ if (postLoginFunc) {
23
+ callFunction(postLoginFunc, postLoginFunc.param)
24
+ .then((result:FunctionResult) => {
25
+ this._profile = result.resultData;
26
+ // redirect if login in the meantime is available
27
+ // => else stay on the login page after reload/url navigation
28
+ if (this.route === "login" && postLoginFunc?.redirect) {
29
+ this.navigate(postLoginFunc.redirect);
30
+ }
31
+ // new event, could/should replace redirect
32
+ document.dispatchEvent(new CustomEvent("user-login-event"));
33
+ }).catch(() => {
34
+ if (postLoginFunc?.logoutOnFail) {
35
+ logout();
36
+ this._user = undefined;
37
+ this._profile = undefined;
38
+ }
39
+ });
40
+ } else {
41
+ this._user = undefined;
42
+ this._profile = undefined;
43
+ }
44
+ }
45
+ */
46
+ }
47
+
48
+ export const login = async (user:string, sec:string): Promise<User> => {
49
+ const auth = getAuth();
50
+ const credentials = await signInWithEmailAndPassword(auth, user, sec);
51
+ return credentials.user;
52
+ }
@@ -1,7 +1,7 @@
1
1
  import { FirebaseApp } from "firebase/app";
2
2
  import { getFunctions, HttpsCallable, httpsCallable } from "firebase/functions";
3
3
  import { TemplateResult } from "lit";
4
- import { ServiceCallController } from "../service-call-controller2.js";
4
+ import { ServiceCallController } from "../../service-call-controller2.js";
5
5
 
6
6
  export interface FunctionDefinition {
7
7
  name: string,
@@ -0,0 +1,68 @@
1
+
2
+ /*
3
+ * Only for temporary development:
4
+ * Set to true/false if the user should hold only in session (login after each reload)
5
+ * or in the localstore (login until the local store entry is invalid or not available).
6
+ */
7
+ const LOCALSTORE_ACTIV = true;
8
+ const LOCALSTORE_ITEM_KEY = 'pdUserLogin';
9
+ const HARDCODED_USER = 'mock';
10
+ const HARDCODED_PWD = 'mock';
11
+
12
+
13
+
14
+ let loginAvailable: boolean | undefined;
15
+
16
+
17
+ const checkLocalStore = () => {
18
+ const userItem = localStorage.getItem(LOCALSTORE_ITEM_KEY);
19
+ return userItem === 'true';
20
+ }
21
+
22
+
23
+ export const logoutMock = async (): Promise<boolean> => {
24
+ console.log("Do MOCK logout");
25
+ loginAvailable = undefined;
26
+ if (LOCALSTORE_ACTIV) {
27
+ localStorage.removeItem(LOCALSTORE_ITEM_KEY);
28
+ }
29
+ return Promise.resolve(true);
30
+ };
31
+
32
+ export const isAuthenticatedMock = (): boolean => {
33
+ if (loginAvailable === undefined) {
34
+ console.log('MOCK: check authentication');
35
+ loginAvailable = LOCALSTORE_ACTIV ? checkLocalStore() : false;
36
+ }
37
+ return loginAvailable;
38
+ }
39
+
40
+
41
+ export const authStateChangedMock = (callback: Function) => {
42
+ console.log("Not implemented for MOCK");
43
+ }
44
+
45
+ export const loginMock = async (user:string, sec:string): Promise<unknown> => {
46
+
47
+ console.log('MOCK: do authentication for user: ', user);
48
+ let valid = false;
49
+ if (user === HARDCODED_USER && sec === HARDCODED_PWD) {
50
+ if (LOCALSTORE_ACTIV) {
51
+ localStorage.setItem(LOCALSTORE_ITEM_KEY, 'true');
52
+ }
53
+ valid = true;
54
+ loginAvailable = true;
55
+ }
56
+
57
+ if (valid) {
58
+ return Promise.resolve({
59
+ uid: "123124"
60
+ });
61
+ }
62
+
63
+ throw new Error("Invalid login");
64
+ }
65
+
66
+
67
+
68
+
@@ -0,0 +1,68 @@
1
+ import { ServiceProviderConfiguration } from "../PdSpaHelper.js";
2
+
3
+ import { authStateChanged, isAuthenticated, login, logout } from "./firebase/auth.js";
4
+ import { authStateChangedMock, isAuthenticatedMock, loginMock, logoutMock } from "./mock/auth.js";
5
+
6
+
7
+ let provider: ServiceProviderConfiguration | undefined;
8
+
9
+
10
+ const throwUndefinedProviderError = () => {
11
+ throw new Error("Undefined service provider, please check configuration. 'firebase' or 'mock' allowed here.");
12
+ }
13
+
14
+ export const setServiceProvider = (providerParam?: ServiceProviderConfiguration) => {
15
+ provider = providerParam;
16
+ }
17
+
18
+ export const authStateChangedImpl = (
19
+ callback: Function
20
+ ) => {
21
+ if (provider === "firebase") {
22
+ authStateChanged(callback);
23
+ } else if (provider === "mock") {
24
+ authStateChangedMock(callback);
25
+ } else {
26
+ throwUndefinedProviderError();
27
+ }
28
+ }
29
+
30
+ export const isAuthenticatedImpl = () => {
31
+ if (provider === "firebase") {
32
+ return isAuthenticated();
33
+ }
34
+ if (provider === "mock") {
35
+ return isAuthenticatedMock();
36
+ }
37
+ return throwUndefinedProviderError();
38
+ }
39
+
40
+ export const loginImpl = (user: string, sec: string) => {
41
+
42
+ if (!user) {
43
+ const returnError = new Error("empty user");
44
+ return Promise.reject(returnError);
45
+ }
46
+ if (!sec) {
47
+ const returnError = new Error("empty password");
48
+ return Promise.reject(returnError);
49
+ }
50
+
51
+ if (provider === "firebase") {
52
+ return login(user, sec);
53
+ }
54
+ if (provider === "mock") {
55
+ return loginMock(user, sec);
56
+ }
57
+ return throwUndefinedProviderError();
58
+ }
59
+
60
+ export const logoutImpl = () => {
61
+ if (provider === "firebase") {
62
+ return logout();
63
+ }
64
+ if (provider === "mock") {
65
+ return logoutMock();
66
+ }
67
+ return throwUndefinedProviderError();
68
+ }
@@ -6,7 +6,8 @@ import '@progressive-development/pd-forms/pd-form-row.js';
6
6
  import '@progressive-development/pd-forms/pd-input.js';
7
7
  import '@progressive-development/pd-forms/pd-button.js';
8
8
 
9
- import { login } from '../firebase/auth.js';
9
+ import { loginImpl } from '../service-provider/service-provider-impl.js';
10
+
10
11
 
11
12
  @customElement("pd-login")
12
13
  export class PdLogin extends LitElement {
@@ -92,7 +93,7 @@ export class PdLogin extends LitElement {
92
93
  const elMail = this.shadowRoot?.getElementById('eMail') as HTMLInputElement;
93
94
  const elSec = this.shadowRoot?.getElementById('pWord') as HTMLInputElement;
94
95
 
95
- login(elMail?.value, elSec?.value)
96
+ loginImpl(elMail?.value, elSec?.value)
96
97
  .then(() => {
97
98
  this.dispatchEvent(new CustomEvent("login-success"));
98
99
  }).catch(error => {
@@ -1,30 +0,0 @@
1
- import { getAuth, signInWithEmailAndPassword, signOut, User } from 'firebase/auth';
2
-
3
- export const logout = async (): Promise<boolean> => {
4
- const auth = getAuth();
5
- try {
6
- await signOut(auth);
7
- return true;
8
- } catch (error) {
9
- return false;
10
- }
11
- };
12
-
13
- export const isAuthenticated = (): boolean =>
14
- getAuth().currentUser !== null;
15
-
16
- export const login = async (user:string, sec:string): Promise<User> => {
17
- if (!user) {
18
- const returnError = new Error("empty user");
19
- return Promise.reject(returnError);
20
- }
21
- if (!sec) {
22
- // error.eType = 'sec';
23
- // error.logMsg = 'password required';
24
- const returnError = new Error("empty password");
25
- return Promise.reject(returnError);
26
- }
27
- const auth = getAuth();
28
- const credentials = await signInWithEmailAndPassword(auth, user, sec);
29
- return credentials.user;
30
- }