Skip to content

Commit 7c08f68

Browse files
committed
Make render optional
1 parent 855be25 commit 7c08f68

File tree

1 file changed

+59
-24
lines changed

1 file changed

+59
-24
lines changed

src/components/Authorize/index.jsx

Lines changed: 59 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export default class Authorize extends Component {
2424
* and `idToken`, depending on the login method used.
2525
* https://auth0.com/docs/libraries/auth0js/v9#extract-the-authresult-and-get-user-info
2626
*/
27-
render: func.isRequired,
27+
render: func,
2828
/**
2929
* Your Auth0 account domain (ex. myaccount.auth0.com).
3030
*/
@@ -50,6 +50,11 @@ export default class Authorize extends Component {
5050
* https://auth0.com/docs/libraries/auth0js/v9#extract-the-authresult-and-get-user-info
5151
*/
5252
onAuthorize: func,
53+
/**
54+
* Execute a function if authorization fails. Receives a single argument
55+
* consisting of the error object.
56+
*/
57+
onError: func,
5358
/**
5459
* Open the authorization flow in a popup; useful for single-page apps or
5560
* flows where you do not wish to interrupt the current page state.
@@ -135,7 +140,8 @@ export default class Authorize extends Component {
135140

136141
componentWillReceiveProps(nextProps) {
137142
if (
138-
'authorize' in nextProps ||
143+
('authorize' in nextProps &&
144+
nextProps.authorize !== this.props.authorize) ||
139145
nextProps.domain !== this.props.domain ||
140146
nextProps.clientID !== this.props.clientID ||
141147
nextProps.audience !== this.props.audience ||
@@ -163,6 +169,7 @@ export default class Authorize extends Component {
163169
this.authorize(props);
164170
} else {
165171
localStorage.removeItem(CHANNEL);
172+
localStorage.removeItem(SESSION);
166173

167174
if (props.authorize !== this.props.authorize) {
168175
this.setState({ error: null, authResult: null, userInfo: null });
@@ -261,15 +268,29 @@ export default class Authorize extends Component {
261268

262269
this.renewalTimer = setTimeout(async () => {
263270
try {
264-
this.setState({
265-
authResult: await this.renew(),
266-
});
271+
this.setState(
272+
{
273+
authResult: await this.renew(),
274+
},
275+
() => {
276+
this.props.onAuthorize &&
277+
this.props.onAuthorize({
278+
authResult: this.state.authResult,
279+
userInfo: this.state.userInfo,
280+
});
281+
}
282+
);
267283
} catch (error) {
268-
this.setState({
269-
error,
270-
authResult: null,
271-
userInfo: null,
272-
});
284+
this.setState(
285+
{
286+
error,
287+
authResult: null,
288+
userInfo: null,
289+
},
290+
() => {
291+
this.props.onError && this.props.onError(error);
292+
}
293+
);
273294
}
274295
}, delay);
275296
}
@@ -294,21 +315,31 @@ export default class Authorize extends Component {
294315
this.scheduleRenewal(authResult.expiresIn);
295316
}
296317

297-
if (this.props.onAuthorize) {
298-
this.props.onAuthorize({ authResult, userInfo });
299-
}
300-
301-
this.setState({
302-
error: null,
303-
authResult,
304-
userInfo,
305-
});
318+
this.setState(
319+
{
320+
error: null,
321+
authResult,
322+
userInfo,
323+
},
324+
() => {
325+
this.props.onAuthorize &&
326+
this.props.onAuthorize({
327+
authResult,
328+
userInfo,
329+
});
330+
}
331+
);
306332
} catch (error) {
307-
this.setState({
308-
error,
309-
authResult: null,
310-
userInfo: null,
311-
});
333+
this.setState(
334+
{
335+
error,
336+
authResult: null,
337+
userInfo: null,
338+
},
339+
() => {
340+
this.props.onError && this.props.onError(error);
341+
}
342+
);
312343
}
313344
}
314345

@@ -350,6 +381,10 @@ export default class Authorize extends Component {
350381
render() {
351382
const { render } = this.props;
352383

384+
if (!render) {
385+
return null;
386+
}
387+
353388
return render({ ...this.state });
354389
}
355390
}

0 commit comments

Comments
 (0)