@functionalcms/svelte-components 0.6.0 → 0.7.0

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.
@@ -0,0 +1,17 @@
1
+ /// <reference types="svelte" />
2
+ export type AuthConfig = {
3
+ tenant: string;
4
+ tenantId: string;
5
+ policy: string;
6
+ scopes: Array<string>;
7
+ clientId: string;
8
+ redirectUrl: string;
9
+ };
10
+ export declare const authStore: {
11
+ isAuthenticated: import("svelte/store").Readable<boolean>;
12
+ userId: import("svelte/store").Readable<string>;
13
+ accessToken: import("svelte/store").Readable<string>;
14
+ initialise: (config: AuthConfig) => Promise<void>;
15
+ signIn: () => Promise<void>;
16
+ signOut: () => Promise<void>;
17
+ };
@@ -0,0 +1,65 @@
1
+ import { readonly, writable } from "svelte/store";
2
+ import { PublicClientApplication } from "@azure/msal-browser";
3
+ const isAuthenticated = writable(false);
4
+ const accessToken = writable('');
5
+ const userId = writable('');
6
+ let msal;
7
+ let _config;
8
+ const getMSALConfig = (config) => {
9
+ return {
10
+ auth: {
11
+ clientId: config.clientId,
12
+ authority: `https://${config.tenant}.b2clogin.com/${config.tenant}.onmicrosoft.com/${config.policy}`,
13
+ knownAuthorities: [`${config.tenant}.b2clogin.com`],
14
+ redirectUri: config.redirectUrl
15
+ },
16
+ cache: {
17
+ cacheLocation: "sessionStorage",
18
+ storeAuthStateInCookie: false, // If you wish to store cache items in cookies as well as browser cache, set this to "true".
19
+ },
20
+ };
21
+ };
22
+ const getLoginRequest = (config) => {
23
+ return {
24
+ scopes: ["openid", "offline_access", config.clientId, ...config.scopes]
25
+ };
26
+ };
27
+ const getTokenRequest = (config, account) => {
28
+ return {
29
+ scopes: ["openid", "offline_access", config.clientId, ...config.scopes],
30
+ forceRefresh: false,
31
+ account: account,
32
+ };
33
+ };
34
+ const handleToken = (token) => {
35
+ if (token !== null) {
36
+ isAuthenticated.set(true);
37
+ accessToken.set(token.accessToken);
38
+ userId.set(token.username);
39
+ }
40
+ };
41
+ const logUsingCookies = async (config) => {
42
+ const accounts = msal.getAllAccounts();
43
+ if (accounts.length > 0) {
44
+ const token = await msal.acquireTokenSilent(getTokenRequest(config, accounts[0]));
45
+ handleToken(token);
46
+ }
47
+ };
48
+ export const authStore = {
49
+ isAuthenticated: readonly(isAuthenticated),
50
+ userId: readonly(userId),
51
+ accessToken: readonly(accessToken),
52
+ initialise: async (config) => {
53
+ msal = new PublicClientApplication(getMSALConfig(config));
54
+ await msal.initialize();
55
+ await logUsingCookies(config);
56
+ _config = config;
57
+ },
58
+ signIn: async () => {
59
+ const loginResponse = await msal.loginPopup(getLoginRequest(_config));
60
+ handleToken(loginResponse);
61
+ },
62
+ signOut: async () => {
63
+ await msal.logoutPopup({});
64
+ },
65
+ };
package/dist/index.d.ts CHANGED
@@ -2,4 +2,5 @@ import Box from './components/Box.svelte';
2
2
  import Hero from './components/Hero.svelte';
3
3
  import Header from './components/Header.svelte';
4
4
  import Footer from './components/Footer.svelte';
5
- export { Box, Hero, Header, Footer };
5
+ import { authStore } from './authStore';
6
+ export { Box, Hero, Header, Footer, authStore };
package/dist/index.js CHANGED
@@ -4,10 +4,12 @@ import Box from './components/Box.svelte';
4
4
  import Hero from './components/Hero.svelte';
5
5
  import Header from './components/Header.svelte';
6
6
  import Footer from './components/Footer.svelte';
7
+ import { authStore } from './authStore';
7
8
 
8
9
  export {
9
10
  Box,
10
11
  Hero,
11
12
  Header,
12
- Footer
13
+ Footer,
14
+ authStore
13
15
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@functionalcms/svelte-components",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -30,7 +30,8 @@
30
30
  "!dist/**/*.spec.*"
31
31
  ],
32
32
  "peerDependencies": {
33
- "svelte": "^4.0.0"
33
+ "svelte": "^4.0.0",
34
+ "@azure/msal-browser": "^3.3.0"
34
35
  },
35
36
  "devDependencies": {
36
37
  "@sveltejs/adapter-auto": "^2.0.0",