@frontegg/react-hooks 6.147.0-alpha.2 → 6.147.0-alpha.4
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/auth/hooks.d.ts +35 -3
- package/auth/hooks.js +38 -3
- package/index.js +1 -1
- package/node/auth/hooks.js +38 -3
- package/node/index.js +1 -1
- package/package.json +3 -3
package/auth/hooks.d.ts
CHANGED
|
@@ -7,6 +7,19 @@ export declare type AuthMapper = {
|
|
|
7
7
|
};
|
|
8
8
|
export declare type StateHookFunction<T> = (() => T) & (<S extends {}>(mapper: (state: T) => S) => S);
|
|
9
9
|
export declare type AuthStateMapper<S> = (state: AuthState) => S;
|
|
10
|
+
/**
|
|
11
|
+
* Use this `frontegg` hook function to obtain the complete authentication state, if it exists.
|
|
12
|
+
*
|
|
13
|
+
* ```jsx
|
|
14
|
+
* export const MyFunctionComponent = () => {
|
|
15
|
+
* const { isAuthenticated, user } = useAuth();
|
|
16
|
+
*
|
|
17
|
+
* return isAuthenticated ? <div>Hello, User {user.name}</div> : null;
|
|
18
|
+
* }
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* You can also utilize other `frontegg` hooks like `useAuthUser` to specifically retrieve the user and redirect to the login page if necessary, `useAuthUserOrNull` to get the user if available, and `useIsAuthenticated` for checking authentication status.
|
|
22
|
+
*/
|
|
10
23
|
export declare function useAuth(): AuthState;
|
|
11
24
|
export declare function useAuth<S>(stateMapper: AuthStateMapper<S>): S;
|
|
12
25
|
/**
|
|
@@ -24,7 +37,16 @@ export declare function useAuth<S>(stateMapper: AuthStateMapper<S>): S;
|
|
|
24
37
|
* }
|
|
25
38
|
* ```
|
|
26
39
|
*
|
|
27
|
-
|
|
40
|
+
/**
|
|
41
|
+
* Use this frontegg hook function to redirect the user to the login page when in hosted login mode.
|
|
42
|
+
* If the user is already authenticated, this method will direct the user to the store, and you can retrieve user information using the `useAuthUserOrNull` method.
|
|
43
|
+
*
|
|
44
|
+
* To ensure the user is available on the first page load when authenticated, configure this option in your `FronteggProvider`:
|
|
45
|
+
* `authOptions`:
|
|
46
|
+
* `hostedLoginOptions`:
|
|
47
|
+
* `loadUserOnFirstLoad: true`
|
|
48
|
+
*
|
|
49
|
+
* When using this option, you can have the user on the first load, and you can control when the user is redirected to the login page by using `loginWithRedirect`.
|
|
28
50
|
*/
|
|
29
51
|
export declare const useLoginWithRedirect: () => AuthActions['requestHostedLoginAuthorize'];
|
|
30
52
|
export declare const useAuthActions: () => AuthActions;
|
|
@@ -49,10 +71,20 @@ export declare const useIsAuthenticated: () => boolean;
|
|
|
49
71
|
* }
|
|
50
72
|
* ```
|
|
51
73
|
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
74
|
+
* Use this `frontegg` hook function to obtain the authenticated user. If the user is not authenticated, this method will immediately redirect the user to the login page. (Primarily intended for embedded mode usage)
|
|
75
|
+
* To prevent this immediate redirection behavior, use the `useAuthUserOrNull` method.
|
|
54
76
|
*/
|
|
55
77
|
export declare const useAuthUser: () => User;
|
|
78
|
+
/**
|
|
79
|
+
* ```jsx
|
|
80
|
+
* export const MyFunctionComponent = () => {
|
|
81
|
+
* const user = useAuthUserOrNull();
|
|
82
|
+
* return user ? <div>Hello {user.name}!</div> : <div>Hello Guest!</div>
|
|
83
|
+
* }
|
|
84
|
+
* ```
|
|
85
|
+
*
|
|
86
|
+
* Use this `frontegg` hook function to retrieve the authenticated user. If the user is not authenticated, this hook will return null. To redirect the user to the login page in case they are not authenticated, use the `useAuthUser` method.
|
|
87
|
+
*/
|
|
56
88
|
export declare const useAuthUserOrNull: () => User | null;
|
|
57
89
|
/**
|
|
58
90
|
* hooks helpers
|
package/auth/hooks.js
CHANGED
|
@@ -10,6 +10,21 @@ const defaultMapper = {
|
|
|
10
10
|
state: state => state,
|
|
11
11
|
actions: actions => actions
|
|
12
12
|
};
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Use this `frontegg` hook function to obtain the complete authentication state, if it exists.
|
|
16
|
+
*
|
|
17
|
+
* ```jsx
|
|
18
|
+
* export const MyFunctionComponent = () => {
|
|
19
|
+
* const { isAuthenticated, user } = useAuth();
|
|
20
|
+
*
|
|
21
|
+
* return isAuthenticated ? <div>Hello, User {user.name}</div> : null;
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* You can also utilize other `frontegg` hooks like `useAuthUser` to specifically retrieve the user and redirect to the login page if necessary, `useAuthUserOrNull` to get the user if available, and `useIsAuthenticated` for checking authentication status.
|
|
26
|
+
*/
|
|
27
|
+
|
|
13
28
|
export function useAuth(stateMapper = defaultMapper.state) {
|
|
14
29
|
return useSelector(state => stateMapper(state[authStoreName]), shallowEqual);
|
|
15
30
|
}
|
|
@@ -29,7 +44,16 @@ export function useAuth(stateMapper = defaultMapper.state) {
|
|
|
29
44
|
* }
|
|
30
45
|
* ```
|
|
31
46
|
*
|
|
32
|
-
|
|
47
|
+
/**
|
|
48
|
+
* Use this frontegg hook function to redirect the user to the login page when in hosted login mode.
|
|
49
|
+
* If the user is already authenticated, this method will direct the user to the store, and you can retrieve user information using the `useAuthUserOrNull` method.
|
|
50
|
+
*
|
|
51
|
+
* To ensure the user is available on the first page load when authenticated, configure this option in your `FronteggProvider`:
|
|
52
|
+
* `authOptions`:
|
|
53
|
+
* `hostedLoginOptions`:
|
|
54
|
+
* `loadUserOnFirstLoad: true`
|
|
55
|
+
*
|
|
56
|
+
* When using this option, you can have the user on the first load, and you can control when the user is redirected to the login page by using `loginWithRedirect`.
|
|
33
57
|
*/
|
|
34
58
|
export const useLoginWithRedirect = () => {
|
|
35
59
|
const dispatch = useDispatch();
|
|
@@ -70,8 +94,8 @@ export const useIsAuthenticated = () => useSelector(({
|
|
|
70
94
|
* }
|
|
71
95
|
* ```
|
|
72
96
|
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
97
|
+
* Use this `frontegg` hook function to obtain the authenticated user. If the user is not authenticated, this method will immediately redirect the user to the login page. (Primarily intended for embedded mode usage)
|
|
98
|
+
* To prevent this immediate redirection behavior, use the `useAuthUserOrNull` method.
|
|
75
99
|
*/
|
|
76
100
|
export const useAuthUser = () => {
|
|
77
101
|
const {
|
|
@@ -90,6 +114,17 @@ export const useAuthUser = () => {
|
|
|
90
114
|
}
|
|
91
115
|
return user || noUser;
|
|
92
116
|
};
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* ```jsx
|
|
120
|
+
* export const MyFunctionComponent = () => {
|
|
121
|
+
* const user = useAuthUserOrNull();
|
|
122
|
+
* return user ? <div>Hello {user.name}!</div> : <div>Hello Guest!</div>
|
|
123
|
+
* }
|
|
124
|
+
* ```
|
|
125
|
+
*
|
|
126
|
+
* Use this `frontegg` hook function to retrieve the authenticated user. If the user is not authenticated, this hook will return null. To redirect the user to the login page in case they are not authenticated, use the `useAuthUser` method.
|
|
127
|
+
*/
|
|
93
128
|
export const useAuthUserOrNull = () => {
|
|
94
129
|
const {
|
|
95
130
|
user
|
package/index.js
CHANGED
package/node/auth/hooks.js
CHANGED
|
@@ -18,6 +18,21 @@ const defaultMapper = {
|
|
|
18
18
|
state: state => state,
|
|
19
19
|
actions: actions => actions
|
|
20
20
|
};
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Use this `frontegg` hook function to obtain the complete authentication state, if it exists.
|
|
24
|
+
*
|
|
25
|
+
* ```jsx
|
|
26
|
+
* export const MyFunctionComponent = () => {
|
|
27
|
+
* const { isAuthenticated, user } = useAuth();
|
|
28
|
+
*
|
|
29
|
+
* return isAuthenticated ? <div>Hello, User {user.name}</div> : null;
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* You can also utilize other `frontegg` hooks like `useAuthUser` to specifically retrieve the user and redirect to the login page if necessary, `useAuthUserOrNull` to get the user if available, and `useIsAuthenticated` for checking authentication status.
|
|
34
|
+
*/
|
|
35
|
+
|
|
21
36
|
function useAuth(stateMapper = defaultMapper.state) {
|
|
22
37
|
return (0, _FronteggStoreContext.useSelector)(state => stateMapper(state[_reduxStore.authStoreName]), _reactRedux.shallowEqual);
|
|
23
38
|
}
|
|
@@ -37,7 +52,16 @@ function useAuth(stateMapper = defaultMapper.state) {
|
|
|
37
52
|
* }
|
|
38
53
|
* ```
|
|
39
54
|
*
|
|
40
|
-
|
|
55
|
+
/**
|
|
56
|
+
* Use this frontegg hook function to redirect the user to the login page when in hosted login mode.
|
|
57
|
+
* If the user is already authenticated, this method will direct the user to the store, and you can retrieve user information using the `useAuthUserOrNull` method.
|
|
58
|
+
*
|
|
59
|
+
* To ensure the user is available on the first page load when authenticated, configure this option in your `FronteggProvider`:
|
|
60
|
+
* `authOptions`:
|
|
61
|
+
* `hostedLoginOptions`:
|
|
62
|
+
* `loadUserOnFirstLoad: true`
|
|
63
|
+
*
|
|
64
|
+
* When using this option, you can have the user on the first load, and you can control when the user is redirected to the login page by using `loginWithRedirect`.
|
|
41
65
|
*/
|
|
42
66
|
const useLoginWithRedirect = () => {
|
|
43
67
|
const dispatch = (0, _FronteggStoreContext.useDispatch)();
|
|
@@ -82,8 +106,8 @@ const useIsAuthenticated = () => (0, _FronteggStoreContext.useSelector)(({
|
|
|
82
106
|
* }
|
|
83
107
|
* ```
|
|
84
108
|
*
|
|
85
|
-
*
|
|
86
|
-
*
|
|
109
|
+
* Use this `frontegg` hook function to obtain the authenticated user. If the user is not authenticated, this method will immediately redirect the user to the login page. (Primarily intended for embedded mode usage)
|
|
110
|
+
* To prevent this immediate redirection behavior, use the `useAuthUserOrNull` method.
|
|
87
111
|
*/
|
|
88
112
|
exports.useIsAuthenticated = useIsAuthenticated;
|
|
89
113
|
const useAuthUser = () => {
|
|
@@ -103,6 +127,17 @@ const useAuthUser = () => {
|
|
|
103
127
|
}
|
|
104
128
|
return user || noUser;
|
|
105
129
|
};
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* ```jsx
|
|
133
|
+
* export const MyFunctionComponent = () => {
|
|
134
|
+
* const user = useAuthUserOrNull();
|
|
135
|
+
* return user ? <div>Hello {user.name}!</div> : <div>Hello Guest!</div>
|
|
136
|
+
* }
|
|
137
|
+
* ```
|
|
138
|
+
*
|
|
139
|
+
* Use this `frontegg` hook function to retrieve the authenticated user. If the user is not authenticated, this hook will return null. To redirect the user to the login page in case they are not authenticated, use the `useAuthUser` method.
|
|
140
|
+
*/
|
|
106
141
|
exports.useAuthUser = useAuthUser;
|
|
107
142
|
const useAuthUserOrNull = () => {
|
|
108
143
|
const {
|
package/node/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@frontegg/react-hooks",
|
|
3
|
-
"version": "6.147.0-alpha.
|
|
3
|
+
"version": "6.147.0-alpha.4",
|
|
4
4
|
"main": "./node/index.js",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Frontegg LTD",
|
|
7
7
|
"dependencies": {
|
|
8
8
|
"@babel/runtime": "^7.18.6",
|
|
9
|
-
"@frontegg/redux-store": "6.147.0-alpha.
|
|
10
|
-
"@frontegg/types": "6.147.0-alpha.
|
|
9
|
+
"@frontegg/redux-store": "6.147.0-alpha.4",
|
|
10
|
+
"@frontegg/types": "6.147.0-alpha.4",
|
|
11
11
|
"@types/react": "*",
|
|
12
12
|
"get-value": "^3.0.1",
|
|
13
13
|
"react-redux": "^7.x"
|