Add support for SUBDOMAINS_ENABLED=false

This commit is contained in:
Tom Moor
2018-11-11 22:06:50 -08:00
parent 61138ff4fa
commit b3a8d34af3
6 changed files with 46 additions and 22 deletions

View File

@ -19,7 +19,7 @@ router.post('team.update', auth(), async ctx => {
const team = await Team.findById(user.teamId); const team = await Team.findById(user.teamId);
authorize(user, 'update', team); authorize(user, 'update', team);
if (process.env.SUBDOMAINS_ENABLED) { if (process.env.SUBDOMAINS_ENABLED === 'true') {
team.subdomain = subdomain === '' ? null : subdomain; team.subdomain = subdomain === '' ? null : subdomain;
} }

View File

@ -110,7 +110,7 @@ export default function auth(options?: { required?: boolean } = {}) {
// set a transfer cookie for the access token itself and redirect // set a transfer cookie for the access token itself and redirect
// to the teams subdomain if subdomains are enabled // to the teams subdomain if subdomains are enabled
if (process.env.SUBDOMAINS_ENABLED && team.subdomain) { if (process.env.SUBDOMAINS_ENABLED === 'true' && team.subdomain) {
// get any existing sessions (teams signed in) and add this team // get any existing sessions (teams signed in) and add this team
const existing = JSON.parse(ctx.cookies.get('sessions') || '{}'); const existing = JSON.parse(ctx.cookies.get('sessions') || '{}');
const sessions = JSON.stringify({ const sessions = JSON.stringify({

View File

@ -45,7 +45,7 @@ const Team = sequelize.define(
{ {
getterMethods: { getterMethods: {
url() { url() {
if (!this.subdomain || !process.env.SUBDOMAINS_ENABLED) { if (!this.subdomain || process.env.SUBDOMAINS_ENABLED !== 'true') {
return process.env.URL; return process.env.URL;
} }

View File

@ -15,10 +15,11 @@ export const screenshotUrl = `${process.env.URL}/screenshot.png`;
type Props = { type Props = {
children?: React.Node, children?: React.Node,
sessions?: Object, sessions: Object,
loggedIn: boolean,
}; };
function Layout({ children, sessions }: Props) { function Layout({ children, loggedIn, sessions }: Props) {
return ( return (
<html lang="en"> <html lang="en">
<head> <head>
@ -66,7 +67,7 @@ function Layout({ children, sessions }: Props) {
{'{{CSS}}'} {'{{CSS}}'}
</head> </head>
<Body> <Body>
<TopNavigation sessions={sessions} /> <TopNavigation sessions={sessions} loggedIn={loggedIn} />
{children} {children}
<BottomNavigation /> <BottomNavigation />
</Body> </Body>

View File

@ -25,7 +25,12 @@ type Sessions = {
}, },
}; };
function TopNavigation({ sessions }: { sessions: ?Sessions }) { type Props = {
sessions: ?Sessions,
loggedIn: boolean,
};
function TopNavigation({ sessions, loggedIn }: Props) {
const orderedSessions = sortBy(sessions, 'name'); const orderedSessions = sortBy(sessions, 'name');
return ( return (
@ -47,20 +52,32 @@ function TopNavigation({ sessions }: { sessions: ?Sessions }) {
<MenuItem> <MenuItem>
<a href={developers()}>API</a> <a href={developers()}>API</a>
</MenuItem> </MenuItem>
{orderedSessions.length ? ( {loggedIn ? (
<React.Fragment>
{process.env.SUBDOMAINS_ENABLED === 'true' ? (
<MenuItem highlighted> <MenuItem highlighted>
<a href={developers()}>Your Teams</a> <a>Your Teams</a>
<ol> <ol>
{orderedSessions.map(session => ( {orderedSessions.map(session => (
<MenuItem key={session.url}> <MenuItem key={session.url}>
<a href={`${session.url}/dashboard`}> <a href={`${session.url}/dashboard`}>
<TeamLogo src={session.logoUrl} width={20} height={20} /> <TeamLogo
src={session.logoUrl}
width={20}
height={20}
/>
{session.name} {session.name}
</a> </a>
</MenuItem> </MenuItem>
))} ))}
</ol> </ol>
</MenuItem> </MenuItem>
) : (
<MenuItem highlighted>
<a href="/dashboard">Dashboard</a>
</MenuItem>
)}
</React.Fragment>
) : ( ) : (
<MenuItem> <MenuItem>
<a href="/#signin">Sign In</a> <a href="/#signin">Sign In</a>

View File

@ -14,10 +14,16 @@ const sheet = new ServerStyleSheet();
export default function renderpage(ctx: Object, children: React.Node) { export default function renderpage(ctx: Object, children: React.Node) {
const sessions = JSON.parse(ctx.cookies.get('sessions') || '{}'); const sessions = JSON.parse(ctx.cookies.get('sessions') || '{}');
const loggedIn = !!(
ctx.cookies.get('accessToken') || Object.keys(sessions).length
);
const html = ReactDOMServer.renderToString( const html = ReactDOMServer.renderToString(
<StyleSheetManager sheet={sheet.instance}> <StyleSheetManager sheet={sheet.instance}>
<ThemeProvider theme={theme}> <ThemeProvider theme={theme}>
<Layout sessions={sessions}>{children}</Layout> <Layout sessions={sessions} loggedIn={loggedIn}>
{children}
</Layout>
</ThemeProvider> </ThemeProvider>
</StyleSheetManager> </StyleSheetManager>
); );