diff --git a/packages/authentication-oauth/src/express.ts b/packages/authentication-oauth/src/express.ts index 2ef198ee4e..c15b16063b 100644 --- a/packages/authentication-oauth/src/express.ts +++ b/packages/authentication-oauth/src/express.ts @@ -44,6 +44,7 @@ export default (options: OauthSetupSettings) => { if (feathers_token) { debug(`Got feathers_token query parameter to link accounts`, feathers_token); req.session.accessToken = feathers_token; + req.session.query = query; } res.redirect(`${path}/connect/${name}?${qs.stringify(query)}`); @@ -55,12 +56,20 @@ export default (options: OauthSetupSettings) => { authApp.get('/:name/authenticate', async (req, res, next) => { const { name } = req.params as any; - const { accessToken, grant } = req.session; + const { accessToken, grant, query = {} } = req.session; const service = app.defaultAuthentication(authService); const [ strategy ] = service.getStrategies(name) as OAuthStrategy[]; + const params = { + authStrategies: [ name ], + authentication: accessToken ? { + strategy: linkStrategy, + accessToken + } : null, + query + }; const sendResponse = async (data: AuthenticationResult|Error) => { try { - const redirect = await strategy.getRedirect(data); + const redirect = await strategy.getRedirect(data, params); if (redirect !== null) { res.redirect(redirect); @@ -78,15 +87,6 @@ export default (options: OauthSetupSettings) => { try { const payload = config.defaults.transport === 'session' ? grant.response : req.query; - - const params = { - authStrategies: [ name ], - authentication: accessToken ? { - strategy: linkStrategy, - accessToken - } : null - }; - const authentication = { strategy: name, ...payload diff --git a/packages/authentication-oauth/src/strategy.ts b/packages/authentication-oauth/src/strategy.ts index e5ca187db9..d86ec9967a 100644 --- a/packages/authentication-oauth/src/strategy.ts +++ b/packages/authentication-oauth/src/strategy.ts @@ -72,13 +72,15 @@ export class OAuthStrategy extends AuthenticationBaseStrategy { return null; } - async getRedirect (data: AuthenticationResult|Error) { + async getRedirect (data: AuthenticationResult|Error, params?: Params) { + const queryRedirect = (params && params.query && params.query.redirect) || ''; const { redirect } = this.authentication.configuration.oauth; if (!redirect) { return null; } + const redirectUrl = redirect + queryRedirect; const separator = redirect.endsWith('?') ? '' : (redirect.indexOf('#') !== -1 ? '?' : '#'); const authResult: AuthenticationResult = data; @@ -88,7 +90,7 @@ export class OAuthStrategy extends AuthenticationBaseStrategy { error: data.message || 'OAuth Authentication not successful' }; - return redirect + separator + querystring.stringify(query); + return redirectUrl + separator + querystring.stringify(query); } async findEntity (profile: OAuthProfile, params: Params) { diff --git a/packages/authentication-oauth/test/strategy.test.ts b/packages/authentication-oauth/test/strategy.test.ts index 8a52190cf5..a0967719b6 100644 --- a/packages/authentication-oauth/test/strategy.test.ts +++ b/packages/authentication-oauth/test/strategy.test.ts @@ -24,6 +24,11 @@ describe('@feathersjs/authentication-oauth/strategy', () => { let redirect = await strategy.getRedirect({ accessToken: 'testing' }); assert.equal(redirect, '/home#access_token=testing'); + redirect = await strategy.getRedirect({ accessToken: 'testing' }, { + query: { redirect: '/hi-there' } + }); + assert.strictEqual('/home/hi-there#access_token=testing', redirect); + redirect = await strategy.getRedirect(new Error('something went wrong')); assert.equal(redirect, '/home#error=something%20went%20wrong');