Ensure team authentication matches subdomain

This commit is contained in:
Tom Moor
2018-11-17 23:30:05 -08:00
parent 5d6dca0faa
commit 3c563e3001
4 changed files with 17 additions and 4 deletions

View File

@ -19,15 +19,28 @@ let authenticatedStores;
const Auth = observer(({ auth, children }: Props) => { const Auth = observer(({ auth, children }: Props) => {
if (auth.authenticated) { if (auth.authenticated) {
const { user, team } = auth; const { user, team } = auth;
const { hostname } = window.location;
if (!team || !user) { if (!team || !user) {
return <LoadingIndicator />; return <LoadingIndicator />;
} }
// If we're authenticated but viewing a subdomain that doesn't match the
// authenticated team then kick the user to the teams subdomain.
// www is a special case, as always
if (
process.env.SUBDOMAINS_ENABLED &&
team.subdomain &&
!hostname.startsWith(`${team.subdomain}.`) &&
!hostname.startsWith('www.')
) {
window.location.href = `${team.url}${window.location.pathname}`;
return <LoadingIndicator />;
}
// Only initialize stores once. Kept in global scope because otherwise they // Only initialize stores once. Kept in global scope because otherwise they
// will get overridden on route change // will get overridden on route change
if (!authenticatedStores) { if (!authenticatedStores) {
// Stores for authenticated user
authenticatedStores = { authenticatedStores = {
integrations: new IntegrationsStore({ integrations: new IntegrationsStore({
ui: stores.ui, ui: stores.ui,

View File

@ -86,7 +86,7 @@ router.get('/', async ctx => {
const team = await Team.find({ const team = await Team.find({
where: { subdomain }, where: { subdomain },
}); });
if (team && process.env.SUBDOMAINS_ENABLED) { if (team && process.env.SUBDOMAINS_ENABLED === 'true') {
return renderpage( return renderpage(
ctx, ctx,
<SubdomainSignin <SubdomainSignin

View File

@ -17,7 +17,7 @@ const definePlugin = new webpack.DefinePlugin({
'process.env': { 'process.env': {
URL: JSON.stringify(process.env.URL), URL: JSON.stringify(process.env.URL),
SLACK_KEY: JSON.stringify(process.env.SLACK_KEY), SLACK_KEY: JSON.stringify(process.env.SLACK_KEY),
SUBDOMAINS_ENABLED: JSON.stringify(process.env.SUBDOMAINS_ENABLED) SUBDOMAINS_ENABLED: JSON.stringify(process.env.SUBDOMAINS_ENABLED === 'true')
} }
}); });

View File

@ -36,7 +36,7 @@ productionWebpackConfig.plugins = [
'process.env.URL': JSON.stringify(process.env.URL), 'process.env.URL': JSON.stringify(process.env.URL),
'process.env.NODE_ENV': JSON.stringify('production'), 'process.env.NODE_ENV': JSON.stringify('production'),
'process.env.GOOGLE_ANALYTICS_ID': JSON.stringify(process.env.GOOGLE_ANALYTICS_ID), 'process.env.GOOGLE_ANALYTICS_ID': JSON.stringify(process.env.GOOGLE_ANALYTICS_ID),
'process.env.SUBDOMAINS_ENABLED': JSON.stringify(process.env.SUBDOMAINS_ENABLED), 'process.env.SUBDOMAINS_ENABLED': JSON.stringify(process.env.SUBDOMAINS_ENABLED === 'true'),
}), }),
]; ];