@frontegg/react-hooks 7.4.0-alpha.0 → 7.4.0-alpha.2
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 +2 -1
- package/auth/hooks.js +27 -13
- package/index.js +1 -1
- package/node/auth/hooks.js +25 -11
- package/node/index.js +1 -1
- package/package.json +3 -3
package/auth/hooks.d.ts
CHANGED
|
@@ -77,7 +77,8 @@ export declare const useIsAuthenticated: () => boolean;
|
|
|
77
77
|
* }
|
|
78
78
|
* ```
|
|
79
79
|
*
|
|
80
|
-
* Use this `frontegg` hook function to obtain the authenticated user.
|
|
80
|
+
* Use this `frontegg` hook function to obtain the authenticated user.
|
|
81
|
+
* If the user is not authenticated, this method will immediately redirect the user to the login page.
|
|
81
82
|
* To prevent this immediate redirection behavior, use the `useAuthUserOrNull` method.
|
|
82
83
|
*/
|
|
83
84
|
export declare const useAuthUser: () => User;
|
package/auth/hooks.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { useContext } from 'react';
|
|
1
|
+
import { useContext, useEffect } from 'react';
|
|
2
2
|
import { ContextHolder } from '@frontegg/rest-api';
|
|
3
3
|
import { FronteggStoreContext, useStore } from '../FronteggStoreContext';
|
|
4
4
|
import { useSnapshot } from '../useSnapshot';
|
|
5
|
-
import {
|
|
5
|
+
import { useRootState } from '../common';
|
|
6
6
|
const defaultMapper = {
|
|
7
7
|
state: state => state,
|
|
8
8
|
actions: actions => actions
|
|
@@ -79,7 +79,7 @@ export const useOnRedirectTo = () => {
|
|
|
79
79
|
} = useAuth();
|
|
80
80
|
const {
|
|
81
81
|
appName
|
|
82
|
-
} =
|
|
82
|
+
} = useRootState();
|
|
83
83
|
return onRedirectTo || ContextHolder.for(appName).onRedirectTo;
|
|
84
84
|
};
|
|
85
85
|
export const useAuthRoutes = () => useAuthState().routes;
|
|
@@ -109,24 +109,38 @@ export const useIsAuthenticated = () => {
|
|
|
109
109
|
* }
|
|
110
110
|
* ```
|
|
111
111
|
*
|
|
112
|
-
* Use this `frontegg` hook function to obtain the authenticated user.
|
|
112
|
+
* Use this `frontegg` hook function to obtain the authenticated user.
|
|
113
|
+
* If the user is not authenticated, this method will immediately redirect the user to the login page.
|
|
113
114
|
* To prevent this immediate redirection behavior, use the `useAuthUserOrNull` method.
|
|
114
115
|
*/
|
|
115
116
|
export const useAuthUser = () => {
|
|
116
|
-
const
|
|
117
|
-
loginUrl,
|
|
118
|
-
customLoginUrl
|
|
119
|
-
} = useAuthRoutes();
|
|
117
|
+
const authRoutes = useAuthRoutes();
|
|
120
118
|
const onRedirectTo = useOnRedirectTo();
|
|
119
|
+
const loginWithRedirect = useLoginWithRedirectV2();
|
|
121
120
|
const {
|
|
122
|
-
user
|
|
121
|
+
user,
|
|
122
|
+
hostedLoginBox
|
|
123
123
|
} = useAuth();
|
|
124
|
-
const isSSR = typeof window === 'undefined';
|
|
125
124
|
const noUser = {};
|
|
125
|
+
const isSSR = typeof window === 'undefined';
|
|
126
|
+
useEffect(() => {
|
|
127
|
+
if (user == null && !isSSR) {
|
|
128
|
+
if (hostedLoginBox) {
|
|
129
|
+
loginWithRedirect({
|
|
130
|
+
shouldRedirectToLogin: true
|
|
131
|
+
});
|
|
132
|
+
} else {
|
|
133
|
+
const {
|
|
134
|
+
loginUrl,
|
|
135
|
+
customLoginUrl
|
|
136
|
+
} = authRoutes;
|
|
137
|
+
onRedirectTo(customLoginUrl != null ? customLoginUrl : loginUrl, {
|
|
138
|
+
refresh: true
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}, [hostedLoginBox, user, authRoutes, onRedirectTo, loginWithRedirect]);
|
|
126
143
|
if (user == null && !isSSR) {
|
|
127
|
-
onRedirectTo(customLoginUrl != null ? customLoginUrl : loginUrl, {
|
|
128
|
-
refresh: true
|
|
129
|
-
});
|
|
130
144
|
return noUser;
|
|
131
145
|
}
|
|
132
146
|
return user || noUser;
|
package/index.js
CHANGED
package/node/auth/hooks.js
CHANGED
|
@@ -91,7 +91,7 @@ const useOnRedirectTo = () => {
|
|
|
91
91
|
} = useAuth();
|
|
92
92
|
const {
|
|
93
93
|
appName
|
|
94
|
-
} = (0, _common.
|
|
94
|
+
} = (0, _common.useRootState)();
|
|
95
95
|
return onRedirectTo || _restApi.ContextHolder.for(appName).onRedirectTo;
|
|
96
96
|
};
|
|
97
97
|
exports.useOnRedirectTo = useOnRedirectTo;
|
|
@@ -123,25 +123,39 @@ const useIsAuthenticated = () => {
|
|
|
123
123
|
* }
|
|
124
124
|
* ```
|
|
125
125
|
*
|
|
126
|
-
* Use this `frontegg` hook function to obtain the authenticated user.
|
|
126
|
+
* Use this `frontegg` hook function to obtain the authenticated user.
|
|
127
|
+
* If the user is not authenticated, this method will immediately redirect the user to the login page.
|
|
127
128
|
* To prevent this immediate redirection behavior, use the `useAuthUserOrNull` method.
|
|
128
129
|
*/
|
|
129
130
|
exports.useIsAuthenticated = useIsAuthenticated;
|
|
130
131
|
const useAuthUser = () => {
|
|
131
|
-
const
|
|
132
|
-
loginUrl,
|
|
133
|
-
customLoginUrl
|
|
134
|
-
} = useAuthRoutes();
|
|
132
|
+
const authRoutes = useAuthRoutes();
|
|
135
133
|
const onRedirectTo = useOnRedirectTo();
|
|
134
|
+
const loginWithRedirect = useLoginWithRedirectV2();
|
|
136
135
|
const {
|
|
137
|
-
user
|
|
136
|
+
user,
|
|
137
|
+
hostedLoginBox
|
|
138
138
|
} = useAuth();
|
|
139
|
-
const isSSR = typeof window === 'undefined';
|
|
140
139
|
const noUser = {};
|
|
140
|
+
const isSSR = typeof window === 'undefined';
|
|
141
|
+
(0, _react.useEffect)(() => {
|
|
142
|
+
if (user == null && !isSSR) {
|
|
143
|
+
if (hostedLoginBox) {
|
|
144
|
+
loginWithRedirect({
|
|
145
|
+
shouldRedirectToLogin: true
|
|
146
|
+
});
|
|
147
|
+
} else {
|
|
148
|
+
const {
|
|
149
|
+
loginUrl,
|
|
150
|
+
customLoginUrl
|
|
151
|
+
} = authRoutes;
|
|
152
|
+
onRedirectTo(customLoginUrl != null ? customLoginUrl : loginUrl, {
|
|
153
|
+
refresh: true
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}, [hostedLoginBox, user, authRoutes, onRedirectTo, loginWithRedirect]);
|
|
141
158
|
if (user == null && !isSSR) {
|
|
142
|
-
onRedirectTo(customLoginUrl != null ? customLoginUrl : loginUrl, {
|
|
143
|
-
refresh: true
|
|
144
|
-
});
|
|
145
159
|
return noUser;
|
|
146
160
|
}
|
|
147
161
|
return user || noUser;
|
package/node/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@frontegg/react-hooks",
|
|
3
|
-
"version": "7.4.0-alpha.
|
|
3
|
+
"version": "7.4.0-alpha.2",
|
|
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": "7.4.0-alpha.
|
|
10
|
-
"@frontegg/types": "7.4.0-alpha.
|
|
9
|
+
"@frontegg/redux-store": "7.4.0-alpha.2",
|
|
10
|
+
"@frontegg/types": "7.4.0-alpha.2",
|
|
11
11
|
"@types/react": "*",
|
|
12
12
|
"@types/react-is": "^17.0.7",
|
|
13
13
|
"get-value": "^3.0.1",
|