Skip to content

Commit

Permalink
feat(authentication-oauth): Set oAuth redirect URL dynamically and pa…
Browse files Browse the repository at this point in the history
…ss query the service (#1737)
  • Loading branch information
Elvin Dzhavadov authored and daffl committed Jan 14, 2020
1 parent c602508 commit 0b05f0b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
10 changes: 6 additions & 4 deletions packages/authentication-oauth/src/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@ export default (options: OauthSetupSettings) => {
authApp.use(expressSession);

authApp.get('/:name', (req, res) => {
const { feathers_token, ...query } = req.query;
const { feathers_token, redirect, ...query } = req.query;
const { name } = req.params as any;

if (feathers_token) {
debug(`Got feathers_token query parameter to link accounts`, feathers_token);
req.session.accessToken = feathers_token;
req.session.query = query;
}
req.session.redirect = redirect;
req.session.query = query;

res.redirect(`${path}/connect/${name}?${qs.stringify(query)}`);
});
Expand All @@ -56,7 +57,7 @@ export default (options: OauthSetupSettings) => {

authApp.get('/:name/authenticate', async (req, res, next) => {
const { name } = req.params as any;
const { accessToken, grant, query = {} } = req.session;
const { accessToken, grant, query = {}, redirect } = req.session;
const service = app.defaultAuthentication(authService);
const [ strategy ] = service.getStrategies(name) as OAuthStrategy[];
const params = {
Expand All @@ -65,7 +66,8 @@ export default (options: OauthSetupSettings) => {
strategy: linkStrategy,
accessToken
} : null,
query
query,
redirect
};
const sendResponse = async (data: AuthenticationResult|Error) => {
try {
Expand Down
2 changes: 1 addition & 1 deletion packages/authentication-oauth/src/strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class OAuthStrategy extends AuthenticationBaseStrategy {
}

async getRedirect (data: AuthenticationResult|Error, params?: Params) {
const queryRedirect = (params && params.query && params.query.redirect) || '';
const queryRedirect = (params && params.redirect) || '';
const { redirect } = this.authentication.configuration.oauth;

if (!redirect) {
Expand Down
2 changes: 1 addition & 1 deletion packages/authentication-oauth/test/strategy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('@feathersjs/authentication-oauth/strategy', () => {
assert.equal(redirect, '/home#access_token=testing');

redirect = await strategy.getRedirect({ accessToken: 'testing' }, {
query: { redirect: '/hi-there' }
redirect: '/hi-there'
});
assert.strictEqual('/home/hi-there#access_token=testing', redirect);

Expand Down

0 comments on commit 0b05f0b

Please sign in to comment.