-
Notifications
You must be signed in to change notification settings - Fork 13
Refresh token #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
17984cc
3136570
d476618
925bc29
1d232db
245531f
9c6c871
9142b40
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| export const exchangeRefreshForAccessToken = ({ clientId, clientSecret, tokenEndpoint, fetch = window.fetch, token }) => { | ||
| const payload = { | ||
| client_secret: clientSecret, | ||
| client_id: clientId, | ||
| grant_type: "refresh_token", | ||
| scope: "openid, profile", | ||
| refresh_token: token.refresh_token | ||
| }; | ||
| return fetch(tokenEndpoint, { | ||
| headers: { | ||
| 'Content-Type': 'application/x-www-form-urlencoded' | ||
| }, | ||
| method: 'POST', | ||
| body: new window.URLSearchParams(payload) | ||
| }) | ||
| .then(r => { | ||
| if (!r.ok) { | ||
| throw new Error(`Token response not ok, status is ${r.status}, check the react-u5auth configuration (wrong provider or token endpoint?)`); | ||
| } | ||
| return r.json(); | ||
| }) | ||
| .then(token => { | ||
| const { expires_in } = token; | ||
| if (expires_in && Number.isFinite(expires_in)) { | ||
| const slackSeconds = 10; | ||
| // add 'expires_at', with the given slack | ||
| token.expires_at = new Date(new Date().getTime() + expires_in * 1000 - (slackSeconds * 1000)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I see, you have slack here, too. I wouldn't change the value of
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure what you mean here, I was basically following how you implemented the expired_at in the |
||
| } | ||
| return token; | ||
| }) | ||
| .catch(err => { | ||
| console.error('ERR (fetch)', err); | ||
| throw err; | ||
| }); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some auth providers don't provide refresh tokens. Before setting a timer to use the refresh token, we must check if there actually is a refresh token.