forked from FAForever/website
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactoring auth with auto refresh token and api-client
- Loading branch information
Showing
13 changed files
with
284 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports.AuthFailed = class AuthFailed extends Error {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
const { Axios } = require('axios') | ||
const refresh = require('passport-oauth2-refresh') | ||
const { AuthFailed } = require('./ApiErrors') | ||
const appConfig = require('../config/app') | ||
|
||
const getRefreshToken = (user) => { | ||
return new Promise((resolve, reject) => { | ||
refresh.requestNewAccessToken(appConfig.oauth.strategy, user.refreshToken, function (err, accessToken, refreshToken) { | ||
if (err || !accessToken || !refreshToken) { | ||
return reject(new AuthFailed('Failed to refresh token')) | ||
} | ||
|
||
return resolve([accessToken, refreshToken]) | ||
}) | ||
}) | ||
} | ||
|
||
module.exports = (javaApiBaseURL, user) => { | ||
let tokenRefreshRunning = null | ||
const client = new Axios({ | ||
baseURL: javaApiBaseURL | ||
}) | ||
|
||
client.interceptors.request.use( | ||
async config => { | ||
config.headers = { | ||
Authorization: `Bearer ${user.token}` | ||
} | ||
|
||
return config | ||
}) | ||
|
||
client.interceptors.response.use(async (res) => { | ||
if (!res.config._refreshTokenRequest && res.config && res.status === 401) { | ||
res.config._refreshTokenRequest = true | ||
|
||
if (!tokenRefreshRunning) { | ||
tokenRefreshRunning = getRefreshToken(user) | ||
} | ||
|
||
const [token, refreshToken] = await tokenRefreshRunning | ||
|
||
user.token = token | ||
user.refreshToken = refreshToken | ||
|
||
return client.request(res.config) | ||
} | ||
|
||
if (res.status === 401) { | ||
throw new AuthFailed('Token no longer valid and refresh did not help') | ||
} | ||
|
||
return res | ||
}) | ||
|
||
return client | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.