Skip to content

Commit

Permalink
Merge pull request #251 from cofacts/redirect
Browse files Browse the repository at this point in the history
Redirect user to domains in RUMORS_SITE_REDIRECT_ORIGIN to fix login issue
  • Loading branch information
MrOrz authored Mar 24, 2021
2 parents b887a4f + 67bf7ed commit 0c42992
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
15 changes: 13 additions & 2 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,19 @@ ROLLBAR_ENV=localhost
HTTP_HEADER_APP_ID=x-app-id
HTTP_HEADER_APP_SECRET=x-app-secret

# official web clients
# Official web clients. Use comma to separate all site origins.
RUMORS_SITE_CORS_ORIGIN=http://localhost:3000

# Websites to redirect back to. Use comma to separate all site origins.
# After logging-in, it will always redirect user to domains specified here.
# If the request is coming from other domains, it will be redirected to the first origin here.
#
# Please make sure the domain is the "same site" as PUBLIC_API_URL in rumors-site
# so that login cookies can be picked up when rumors-site make requests to this API server.
#
RUMORS_SITE_REDIRECT_ORIGIN=http://localhost:3000

# Official LINE clients
RUMORS_LINE_BOT_CORS_ORIGIN=http://localhost:5001

# official line bot client
Expand Down Expand Up @@ -56,7 +67,7 @@ GA_WEB_VIEW_ID=GA_WEB_VIEW_ID
GA_LINE_VIEW_ID=GA_LINE_VIEW_ID

# URL to URL resolver microservice (http://github.com/cofacts/url-resolver)
URL_RESOLVER_URL=http://localhost:4000
URL_RESOLVER_URL=localhost:4000

# Apollo engine. When not given, disables Apollo Engine introspection
ENGINE_API_KEY=
Expand Down
16 changes: 13 additions & 3 deletions src/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,23 @@ export const authRouter = Router()
ctx.session.appId === 'RUMORS_SITE' ||
ctx.session.appId === 'DEVELOPMENT_FRONTEND'
) {
const allowedOrigins = process.env.RUMORS_SITE_CORS_ORIGIN.split(',');
basePath = allowedOrigins.find(o => o === ctx.session.origin);
const validOrigins = (
process.env.RUMORS_SITE_REDIRECT_ORIGIN || ''
).split(',');

basePath =
validOrigins.find(o => o === ctx.session.origin) || validOrigins[0];
}

// TODO: Get basePath from DB for other client apps
try {
ctx.redirect(new URL(ctx.session.redirect, basePath).href);
} catch (err) {
err.status = 400;
err.expose = true;
throw err;
}

ctx.redirect(new URL(ctx.session.redirect, basePath).href);
// eslint-disable-next-line require-atomic-updates
ctx.session.appId = undefined;
// eslint-disable-next-line require-atomic-updates
Expand Down

0 comments on commit 0c42992

Please sign in to comment.