@react-keycloak-refork/core 6.0.0 → 8.0.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.
- package/LICENSE.md +24 -24
- package/README.md +149 -150
- package/lib/context.d.ts +23 -24
- package/lib/context.js +23 -23
- package/lib/index.d.ts +3 -3
- package/lib/index.js +4 -4
- package/lib/provider.d.ts +127 -132
- package/lib/provider.js +146 -145
- package/lib/provider.js.map +1 -1
- package/lib/types.d.ts +80 -80
- package/lib/types.js +1 -1
- package/lib-commonjs/context.d.ts +23 -24
- package/lib-commonjs/context.js +26 -27
- package/lib-commonjs/context.js.map +1 -1
- package/lib-commonjs/index.d.ts +3 -3
- package/lib-commonjs/index.js +20 -20
- package/lib-commonjs/provider.d.ts +127 -132
- package/lib-commonjs/provider.js +185 -175
- package/lib-commonjs/provider.js.map +1 -1
- package/lib-commonjs/types.d.ts +80 -80
- package/lib-commonjs/types.js +2 -2
- package/package.json +11 -7
package/lib/provider.d.ts
CHANGED
|
@@ -1,132 +1,127 @@
|
|
|
1
|
-
import * as React from 'react';
|
|
2
|
-
import { ReactNode } from 'react';
|
|
3
|
-
import { IAuthContextProps } from './context';
|
|
4
|
-
import { AuthClient, AuthClientError, AuthClientEvent, AuthClientInitOptions, AuthClientTokens } from './types';
|
|
5
|
-
/**
|
|
6
|
-
* Props that can be passed to AuthProvider
|
|
7
|
-
*/
|
|
8
|
-
export type AuthProviderProps<T extends AuthClient> = {
|
|
9
|
-
/**
|
|
10
|
-
* The single AuthClient instance to be used by your application.
|
|
11
|
-
*/
|
|
12
|
-
authClient: T;
|
|
13
|
-
/**
|
|
14
|
-
* The single AuthClient instance to be used by your application.
|
|
15
|
-
*/
|
|
16
|
-
children: React.JSXElementConstructor<{
|
|
17
|
-
children: React.ReactElement | ReactNode | Element;
|
|
18
|
-
}> | any;
|
|
19
|
-
/**
|
|
20
|
-
* A flag to enable automatic token refresh. Defaults to true.
|
|
21
|
-
* This is useful if you need to disable it (not recommended).
|
|
22
|
-
*
|
|
23
|
-
* @default true
|
|
24
|
-
*/
|
|
25
|
-
autoRefreshToken?: boolean;
|
|
26
|
-
/**
|
|
27
|
-
* The config to be used when initializing AuthClient instance.
|
|
28
|
-
*/
|
|
29
|
-
initOptions?: AuthClientInitOptions;
|
|
30
|
-
/**
|
|
31
|
-
* An optional loading check function to customize LoadingComponent display condition.
|
|
32
|
-
* Return `true` to display LoadingComponent, `false` to hide it.
|
|
33
|
-
*
|
|
34
|
-
* @param authClient the current AuthClient instance.
|
|
35
|
-
*
|
|
36
|
-
* @returns {boolean} Set to true to display LoadingComponent, false to hide it.
|
|
37
|
-
*/
|
|
38
|
-
isLoadingCheck?: (authClient: T) => boolean;
|
|
39
|
-
/**
|
|
40
|
-
* An optional component to display while AuthClient instance is being initialized.
|
|
41
|
-
*/
|
|
42
|
-
LoadingComponent?: JSX.Element;
|
|
43
|
-
/**
|
|
44
|
-
* An optional function to receive AuthClient events as they happen.
|
|
45
|
-
*/
|
|
46
|
-
onEvent?: (eventType: AuthClientEvent, error?: AuthClientError) => void;
|
|
47
|
-
/**
|
|
48
|
-
* An optional function to receive AuthClient tokens when changed.
|
|
49
|
-
*
|
|
50
|
-
* @param {AuthClientTokens} tokens The current AuthClient tokens set.
|
|
51
|
-
*/
|
|
52
|
-
onTokens?: (tokens: AuthClientTokens) => void;
|
|
53
|
-
};
|
|
54
|
-
type AuthProviderState = {
|
|
55
|
-
initialized: boolean;
|
|
56
|
-
isAuthenticated: boolean;
|
|
57
|
-
isLoading: boolean;
|
|
58
|
-
};
|
|
59
|
-
/**
|
|
60
|
-
* Create an AuthProvider component to wrap a React app with, it will take care of common AuthClient
|
|
61
|
-
* lifecycle handling (such as initialization and token refresh).
|
|
62
|
-
*
|
|
63
|
-
* @param AuthContext the Auth context to be used by the created AuthProvider
|
|
64
|
-
*
|
|
65
|
-
* @returns the AuthProvider component
|
|
66
|
-
*/
|
|
67
|
-
export declare function createAuthProvider<T extends AuthClient>(AuthContext: React.Context<IAuthContextProps<T>>): {
|
|
68
|
-
new (props: AuthProviderProps<T>): {
|
|
69
|
-
state: {
|
|
70
|
-
initialized: boolean;
|
|
71
|
-
isAuthenticated: boolean;
|
|
72
|
-
isLoading: boolean;
|
|
73
|
-
};
|
|
74
|
-
componentDidMount(): void;
|
|
75
|
-
componentDidUpdate({ authClient: prevAuthClient, initOptions: prevInitOptions, }: AuthProviderProps<T>): void;
|
|
76
|
-
init(): void;
|
|
77
|
-
onError: (event: AuthClientEvent) => (error?: AuthClientError) => void;
|
|
78
|
-
updateState: (event: AuthClientEvent) => () => void;
|
|
79
|
-
refreshToken: (event: AuthClientEvent) => () => void;
|
|
80
|
-
render(): React.JSX.Element;
|
|
81
|
-
context: unknown;
|
|
82
|
-
setState<K extends keyof AuthProviderState>(state: AuthProviderState | ((prevState: Readonly<AuthProviderState>, props: Readonly<AuthProviderProps<T>>) => AuthProviderState | Pick<AuthProviderState, K> | null) | Pick<AuthProviderState, K> | null, callback?: (() => void) | undefined): void;
|
|
83
|
-
forceUpdate(callback?: (() => void) | undefined): void;
|
|
84
|
-
readonly props: Readonly<AuthProviderProps<T>>;
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
UNSAFE_componentWillUpdate?(nextProps: Readonly<AuthProviderProps<T>>, nextState: Readonly<AuthProviderState>, nextContext: any): void;
|
|
129
|
-
};
|
|
130
|
-
contextType?: React.Context<any> | undefined;
|
|
131
|
-
};
|
|
132
|
-
export default createAuthProvider;
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { ReactNode } from 'react';
|
|
3
|
+
import { IAuthContextProps } from './context';
|
|
4
|
+
import { AuthClient, AuthClientError, AuthClientEvent, AuthClientInitOptions, AuthClientTokens } from './types';
|
|
5
|
+
/**
|
|
6
|
+
* Props that can be passed to AuthProvider
|
|
7
|
+
*/
|
|
8
|
+
export type AuthProviderProps<T extends AuthClient> = {
|
|
9
|
+
/**
|
|
10
|
+
* The single AuthClient instance to be used by your application.
|
|
11
|
+
*/
|
|
12
|
+
authClient: T;
|
|
13
|
+
/**
|
|
14
|
+
* The single AuthClient instance to be used by your application.
|
|
15
|
+
*/
|
|
16
|
+
children: React.JSXElementConstructor<{
|
|
17
|
+
children: React.ReactElement | ReactNode | Element;
|
|
18
|
+
}> | any;
|
|
19
|
+
/**
|
|
20
|
+
* A flag to enable automatic token refresh. Defaults to true.
|
|
21
|
+
* This is useful if you need to disable it (not recommended).
|
|
22
|
+
*
|
|
23
|
+
* @default true
|
|
24
|
+
*/
|
|
25
|
+
autoRefreshToken?: boolean;
|
|
26
|
+
/**
|
|
27
|
+
* The config to be used when initializing AuthClient instance.
|
|
28
|
+
*/
|
|
29
|
+
initOptions?: AuthClientInitOptions;
|
|
30
|
+
/**
|
|
31
|
+
* An optional loading check function to customize LoadingComponent display condition.
|
|
32
|
+
* Return `true` to display LoadingComponent, `false` to hide it.
|
|
33
|
+
*
|
|
34
|
+
* @param authClient the current AuthClient instance.
|
|
35
|
+
*
|
|
36
|
+
* @returns {boolean} Set to true to display LoadingComponent, false to hide it.
|
|
37
|
+
*/
|
|
38
|
+
isLoadingCheck?: (authClient: T) => boolean;
|
|
39
|
+
/**
|
|
40
|
+
* An optional component to display while AuthClient instance is being initialized.
|
|
41
|
+
*/
|
|
42
|
+
LoadingComponent?: React.JSX.Element;
|
|
43
|
+
/**
|
|
44
|
+
* An optional function to receive AuthClient events as they happen.
|
|
45
|
+
*/
|
|
46
|
+
onEvent?: (eventType: AuthClientEvent, error?: AuthClientError) => void;
|
|
47
|
+
/**
|
|
48
|
+
* An optional function to receive AuthClient tokens when changed.
|
|
49
|
+
*
|
|
50
|
+
* @param {AuthClientTokens} tokens The current AuthClient tokens set.
|
|
51
|
+
*/
|
|
52
|
+
onTokens?: (tokens: AuthClientTokens) => void;
|
|
53
|
+
};
|
|
54
|
+
type AuthProviderState = {
|
|
55
|
+
initialized: boolean;
|
|
56
|
+
isAuthenticated: boolean;
|
|
57
|
+
isLoading: boolean;
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* Create an AuthProvider component to wrap a React app with, it will take care of common AuthClient
|
|
61
|
+
* lifecycle handling (such as initialization and token refresh).
|
|
62
|
+
*
|
|
63
|
+
* @param AuthContext the Auth context to be used by the created AuthProvider
|
|
64
|
+
*
|
|
65
|
+
* @returns the AuthProvider component
|
|
66
|
+
*/
|
|
67
|
+
export declare function createAuthProvider<T extends AuthClient>(AuthContext: React.Context<IAuthContextProps<T>>): {
|
|
68
|
+
new (props: AuthProviderProps<T>): {
|
|
69
|
+
state: {
|
|
70
|
+
initialized: boolean;
|
|
71
|
+
isAuthenticated: boolean;
|
|
72
|
+
isLoading: boolean;
|
|
73
|
+
};
|
|
74
|
+
componentDidMount(): void;
|
|
75
|
+
componentDidUpdate({ authClient: prevAuthClient, initOptions: prevInitOptions, }: AuthProviderProps<T>): void;
|
|
76
|
+
init(): void;
|
|
77
|
+
onError: (event: AuthClientEvent) => (error?: AuthClientError) => void;
|
|
78
|
+
updateState: (event: AuthClientEvent) => () => void;
|
|
79
|
+
refreshToken: (event: AuthClientEvent) => () => void;
|
|
80
|
+
render(): React.JSX.Element;
|
|
81
|
+
context: unknown;
|
|
82
|
+
setState<K extends keyof AuthProviderState>(state: AuthProviderState | ((prevState: Readonly<AuthProviderState>, props: Readonly<AuthProviderProps<T>>) => AuthProviderState | Pick<AuthProviderState, K> | null) | Pick<AuthProviderState, K> | null, callback?: (() => void) | undefined): void;
|
|
83
|
+
forceUpdate(callback?: (() => void) | undefined): void;
|
|
84
|
+
readonly props: Readonly<AuthProviderProps<T>>;
|
|
85
|
+
shouldComponentUpdate?(nextProps: Readonly<AuthProviderProps<T>>, nextState: Readonly<AuthProviderState>, nextContext: any): boolean;
|
|
86
|
+
componentWillUnmount?(): void;
|
|
87
|
+
componentDidCatch?(error: Error, errorInfo: React.ErrorInfo): void;
|
|
88
|
+
getSnapshotBeforeUpdate?(prevProps: Readonly<AuthProviderProps<T>>, prevState: Readonly<AuthProviderState>): any;
|
|
89
|
+
componentWillMount?(): void;
|
|
90
|
+
UNSAFE_componentWillMount?(): void;
|
|
91
|
+
componentWillReceiveProps?(nextProps: Readonly<AuthProviderProps<T>>, nextContext: any): void;
|
|
92
|
+
UNSAFE_componentWillReceiveProps?(nextProps: Readonly<AuthProviderProps<T>>, nextContext: any): void;
|
|
93
|
+
componentWillUpdate?(nextProps: Readonly<AuthProviderProps<T>>, nextState: Readonly<AuthProviderState>, nextContext: any): void;
|
|
94
|
+
UNSAFE_componentWillUpdate?(nextProps: Readonly<AuthProviderProps<T>>, nextState: Readonly<AuthProviderState>, nextContext: any): void;
|
|
95
|
+
};
|
|
96
|
+
new (props: AuthProviderProps<T>, context: any): {
|
|
97
|
+
state: {
|
|
98
|
+
initialized: boolean;
|
|
99
|
+
isAuthenticated: boolean;
|
|
100
|
+
isLoading: boolean;
|
|
101
|
+
};
|
|
102
|
+
componentDidMount(): void;
|
|
103
|
+
componentDidUpdate({ authClient: prevAuthClient, initOptions: prevInitOptions, }: AuthProviderProps<T>): void;
|
|
104
|
+
init(): void;
|
|
105
|
+
onError: (event: AuthClientEvent) => (error?: AuthClientError) => void;
|
|
106
|
+
updateState: (event: AuthClientEvent) => () => void;
|
|
107
|
+
refreshToken: (event: AuthClientEvent) => () => void;
|
|
108
|
+
render(): React.JSX.Element;
|
|
109
|
+
context: unknown;
|
|
110
|
+
setState<K extends keyof AuthProviderState>(state: AuthProviderState | ((prevState: Readonly<AuthProviderState>, props: Readonly<AuthProviderProps<T>>) => AuthProviderState | Pick<AuthProviderState, K> | null) | Pick<AuthProviderState, K> | null, callback?: (() => void) | undefined): void;
|
|
111
|
+
forceUpdate(callback?: (() => void) | undefined): void;
|
|
112
|
+
readonly props: Readonly<AuthProviderProps<T>>;
|
|
113
|
+
shouldComponentUpdate?(nextProps: Readonly<AuthProviderProps<T>>, nextState: Readonly<AuthProviderState>, nextContext: any): boolean;
|
|
114
|
+
componentWillUnmount?(): void;
|
|
115
|
+
componentDidCatch?(error: Error, errorInfo: React.ErrorInfo): void;
|
|
116
|
+
getSnapshotBeforeUpdate?(prevProps: Readonly<AuthProviderProps<T>>, prevState: Readonly<AuthProviderState>): any;
|
|
117
|
+
componentWillMount?(): void;
|
|
118
|
+
UNSAFE_componentWillMount?(): void;
|
|
119
|
+
componentWillReceiveProps?(nextProps: Readonly<AuthProviderProps<T>>, nextContext: any): void;
|
|
120
|
+
UNSAFE_componentWillReceiveProps?(nextProps: Readonly<AuthProviderProps<T>>, nextContext: any): void;
|
|
121
|
+
componentWillUpdate?(nextProps: Readonly<AuthProviderProps<T>>, nextState: Readonly<AuthProviderState>, nextContext: any): void;
|
|
122
|
+
UNSAFE_componentWillUpdate?(nextProps: Readonly<AuthProviderProps<T>>, nextState: Readonly<AuthProviderState>, nextContext: any): void;
|
|
123
|
+
};
|
|
124
|
+
contextType?: React.Context<any> | undefined;
|
|
125
|
+
propTypes?: any;
|
|
126
|
+
};
|
|
127
|
+
export default createAuthProvider;
|
package/lib/provider.js
CHANGED
|
@@ -1,146 +1,147 @@
|
|
|
1
|
-
var __extends = (this && this.__extends) || (function () {
|
|
2
|
-
var extendStatics = function (d, b) {
|
|
3
|
-
extendStatics = Object.setPrototypeOf ||
|
|
4
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
5
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
6
|
-
return extendStatics(d, b);
|
|
7
|
-
};
|
|
8
|
-
return function (d, b) {
|
|
9
|
-
if (typeof b !== "function" && b !== null)
|
|
10
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
11
|
-
extendStatics(d, b);
|
|
12
|
-
function __() { this.constructor = d; }
|
|
13
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
14
|
-
};
|
|
15
|
-
})();
|
|
16
|
-
var __assign = (this && this.__assign) || function () {
|
|
17
|
-
__assign = Object.assign || function(t) {
|
|
18
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
19
|
-
s = arguments[i];
|
|
20
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
21
|
-
t[p] = s[p];
|
|
22
|
-
}
|
|
23
|
-
return t;
|
|
24
|
-
};
|
|
25
|
-
return __assign.apply(this, arguments);
|
|
26
|
-
};
|
|
27
|
-
import
|
|
28
|
-
import
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
_this
|
|
51
|
-
_this.
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
var
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
if
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
var
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
prevAuthClient.
|
|
106
|
-
prevAuthClient.
|
|
107
|
-
prevAuthClient.
|
|
108
|
-
prevAuthClient.
|
|
109
|
-
prevAuthClient.
|
|
110
|
-
prevAuthClient.
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
authClient.
|
|
122
|
-
authClient.
|
|
123
|
-
authClient.
|
|
124
|
-
authClient.
|
|
125
|
-
authClient.
|
|
126
|
-
authClient.
|
|
127
|
-
authClient
|
|
128
|
-
|
|
129
|
-
.
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
var
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
1
|
+
var __extends = (this && this.__extends) || (function () {
|
|
2
|
+
var extendStatics = function (d, b) {
|
|
3
|
+
extendStatics = Object.setPrototypeOf ||
|
|
4
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
5
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
6
|
+
return extendStatics(d, b);
|
|
7
|
+
};
|
|
8
|
+
return function (d, b) {
|
|
9
|
+
if (typeof b !== "function" && b !== null)
|
|
10
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
11
|
+
extendStatics(d, b);
|
|
12
|
+
function __() { this.constructor = d; }
|
|
13
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
14
|
+
};
|
|
15
|
+
})();
|
|
16
|
+
var __assign = (this && this.__assign) || function () {
|
|
17
|
+
__assign = Object.assign || function(t) {
|
|
18
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
19
|
+
s = arguments[i];
|
|
20
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
21
|
+
t[p] = s[p];
|
|
22
|
+
}
|
|
23
|
+
return t;
|
|
24
|
+
};
|
|
25
|
+
return __assign.apply(this, arguments);
|
|
26
|
+
};
|
|
27
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
28
|
+
import * as React from 'react';
|
|
29
|
+
import isEqual from 'react-fast-compare';
|
|
30
|
+
/**
|
|
31
|
+
* Create an AuthProvider component to wrap a React app with, it will take care of common AuthClient
|
|
32
|
+
* lifecycle handling (such as initialization and token refresh).
|
|
33
|
+
*
|
|
34
|
+
* @param AuthContext the Auth context to be used by the created AuthProvider
|
|
35
|
+
*
|
|
36
|
+
* @returns the AuthProvider component
|
|
37
|
+
*/
|
|
38
|
+
export function createAuthProvider(AuthContext) {
|
|
39
|
+
var defaultInitOptions = {
|
|
40
|
+
onLoad: 'check-sso',
|
|
41
|
+
};
|
|
42
|
+
var initialState = {
|
|
43
|
+
initialized: false,
|
|
44
|
+
isAuthenticated: false,
|
|
45
|
+
isLoading: true,
|
|
46
|
+
};
|
|
47
|
+
return /** @class */ (function (_super) {
|
|
48
|
+
__extends(KeycloakProvider, _super);
|
|
49
|
+
function KeycloakProvider() {
|
|
50
|
+
var _this = _super !== null && _super.apply(this, arguments) || this;
|
|
51
|
+
_this.state = __assign({}, initialState);
|
|
52
|
+
_this.onError = function (event) { return function (error) {
|
|
53
|
+
var onEvent = _this.props.onEvent;
|
|
54
|
+
// Notify Events listener
|
|
55
|
+
onEvent && onEvent(event, error);
|
|
56
|
+
}; };
|
|
57
|
+
_this.updateState = function (event) { return function () {
|
|
58
|
+
var _a = _this.props, authClient = _a.authClient, onEvent = _a.onEvent, onTokens = _a.onTokens, isLoadingCheck = _a.isLoadingCheck;
|
|
59
|
+
var _b = _this.state, prevInitialized = _b.initialized, prevAuthenticated = _b.isAuthenticated, prevLoading = _b.isLoading;
|
|
60
|
+
// Notify Events listener
|
|
61
|
+
onEvent && onEvent(event);
|
|
62
|
+
// Check Loading state
|
|
63
|
+
var isLoading = isLoadingCheck ? isLoadingCheck(authClient) : false;
|
|
64
|
+
// Check if user is authenticated
|
|
65
|
+
var isAuthenticated = isUserAuthenticated(authClient);
|
|
66
|
+
// Avoid double-refresh if state hasn't changed
|
|
67
|
+
if (!prevInitialized ||
|
|
68
|
+
isAuthenticated !== prevAuthenticated ||
|
|
69
|
+
isLoading !== prevLoading) {
|
|
70
|
+
_this.setState({
|
|
71
|
+
initialized: true,
|
|
72
|
+
isAuthenticated: isAuthenticated,
|
|
73
|
+
isLoading: isLoading,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
// Notify token listener, if any
|
|
77
|
+
var idToken = authClient.idToken, refreshToken = authClient.refreshToken, token = authClient.token;
|
|
78
|
+
onTokens &&
|
|
79
|
+
onTokens({
|
|
80
|
+
idToken: idToken,
|
|
81
|
+
refreshToken: refreshToken,
|
|
82
|
+
token: token,
|
|
83
|
+
});
|
|
84
|
+
}; };
|
|
85
|
+
_this.refreshToken = function (event) { return function () {
|
|
86
|
+
var _a = _this.props, autoRefreshToken = _a.autoRefreshToken, authClient = _a.authClient, onEvent = _a.onEvent;
|
|
87
|
+
// Notify Events listener
|
|
88
|
+
onEvent && onEvent(event);
|
|
89
|
+
if (autoRefreshToken !== false) {
|
|
90
|
+
// Refresh Keycloak token
|
|
91
|
+
authClient.updateToken(5);
|
|
92
|
+
}
|
|
93
|
+
}; };
|
|
94
|
+
return _this;
|
|
95
|
+
}
|
|
96
|
+
KeycloakProvider.prototype.componentDidMount = function () {
|
|
97
|
+
this.init();
|
|
98
|
+
};
|
|
99
|
+
KeycloakProvider.prototype.componentDidUpdate = function (_a) {
|
|
100
|
+
var prevAuthClient = _a.authClient, prevInitOptions = _a.initOptions;
|
|
101
|
+
var _b = this.props, initOptions = _b.initOptions, authClient = _b.authClient;
|
|
102
|
+
if (authClient !== prevAuthClient ||
|
|
103
|
+
!isEqual(initOptions, prevInitOptions)) {
|
|
104
|
+
// De-init previous AuthClient instance
|
|
105
|
+
prevAuthClient.onReady = undefined;
|
|
106
|
+
prevAuthClient.onAuthSuccess = undefined;
|
|
107
|
+
prevAuthClient.onAuthError = undefined;
|
|
108
|
+
prevAuthClient.onAuthRefreshSuccess = undefined;
|
|
109
|
+
prevAuthClient.onAuthRefreshError = undefined;
|
|
110
|
+
prevAuthClient.onAuthLogout = undefined;
|
|
111
|
+
prevAuthClient.onTokenExpired = undefined;
|
|
112
|
+
// Reset state
|
|
113
|
+
this.setState(__assign({}, initialState));
|
|
114
|
+
// Init new AuthClient instance
|
|
115
|
+
this.init();
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
KeycloakProvider.prototype.init = function () {
|
|
119
|
+
var _a = this.props, initOptions = _a.initOptions, authClient = _a.authClient;
|
|
120
|
+
// Attach Keycloak listeners
|
|
121
|
+
authClient.onReady = this.updateState('onReady');
|
|
122
|
+
authClient.onAuthSuccess = this.updateState('onAuthSuccess');
|
|
123
|
+
authClient.onAuthError = this.onError('onAuthError');
|
|
124
|
+
authClient.onAuthRefreshSuccess = this.updateState('onAuthRefreshSuccess');
|
|
125
|
+
authClient.onAuthRefreshError = this.onError('onAuthRefreshError');
|
|
126
|
+
authClient.onAuthLogout = this.updateState('onAuthLogout');
|
|
127
|
+
authClient.onTokenExpired = this.refreshToken('onTokenExpired');
|
|
128
|
+
authClient
|
|
129
|
+
.init(__assign(__assign({}, defaultInitOptions), initOptions))
|
|
130
|
+
.catch(this.onError('onInitError'));
|
|
131
|
+
};
|
|
132
|
+
KeycloakProvider.prototype.render = function () {
|
|
133
|
+
var _a = this.props, children = _a.children, authClient = _a.authClient, LoadingComponent = _a.LoadingComponent;
|
|
134
|
+
var _b = this.state, initialized = _b.initialized, isLoading = _b.isLoading;
|
|
135
|
+
if (!!LoadingComponent && (!initialized || isLoading)) {
|
|
136
|
+
return LoadingComponent;
|
|
137
|
+
}
|
|
138
|
+
return (_jsx(AuthContext.Provider, { value: { initialized: initialized, authClient: authClient }, children: children }));
|
|
139
|
+
};
|
|
140
|
+
return KeycloakProvider;
|
|
141
|
+
}(React.PureComponent));
|
|
142
|
+
}
|
|
143
|
+
function isUserAuthenticated(authClient) {
|
|
144
|
+
return !!authClient.idToken && !!authClient.token;
|
|
145
|
+
}
|
|
146
|
+
export default createAuthProvider;
|
|
146
147
|
//# sourceMappingURL=provider.js.map
|
package/lib/provider.js.map
CHANGED
|
@@ -6,5 +6,5 @@
|
|
|
6
6
|
"@react-keycloak-refork\\core\\provider.tsx"
|
|
7
7
|
],
|
|
8
8
|
"names": [],
|
|
9
|
-
"mappings": "
|
|
9
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAE9B,OAAO,OAAO,MAAM,oBAAoB,CAAA;AA8ExC;;;;;;;GAOG;AACH,MAAM,UAAU,kBAAkB,CAChC,WAAgD;IAEhD,IAAM,kBAAkB,GAA0B;QAChD,MAAM,EAAE,WAAW;KACpB,CAAA;IAED,IAAM,YAAY,GAAsB;QACtC,WAAW,EAAE,KAAK;QAClB,eAAe,EAAE,KAAK;QACtB,SAAS,EA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