Skip to content

Commit

Permalink
login redirects for invited, existing, new user.
Browse files Browse the repository at this point in the history
  • Loading branch information
tima101 committed Jun 20, 2018
1 parent c524462 commit 3f247b6
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 30 deletions.
12 changes: 10 additions & 2 deletions api/server/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ export default function auth({ ROOT_URL, server }) {
prompt: 'select_account',
};

if (req.query && req.query.next && req.query.next.startsWith('/')) {
req.session.next_url = req.query.next;
} else {
req.session.next_url = null;
}

if (req.query && req.query.invitationToken) {
req.session.invitationToken = req.query.invitationToken;
} else {
Expand All @@ -93,15 +99,17 @@ export default function auth({ ROOT_URL, server }) {

if (req.user && req.user.isAdmin) {
res.redirect(`${URL_APP}/admin`);
} else if (req.session.next_url) {
let redirectUrlAfterLogin;
redirectUrlAfterLogin = req.session.next_url;
res.redirect(`${URL_APP}${redirectUrlAfterLogin}`);
} else {
let redirectUrlAfterLogin;

if (!req.user.defaultTeamSlug) {
redirectUrlAfterLogin = 'settings/create-team';
} else {
redirectUrlAfterLogin = `team/${req.user.defaultTeamSlug}/t/projects`;
}

res.redirect(`${URL_APP}/${redirectUrlAfterLogin}`);
}
},
Expand Down
6 changes: 3 additions & 3 deletions api/server/models/EmailTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ async function insertTemplates() {
If you're interested in hiring our team to build custom SaaS features, please fill out our <a href="https://goo.gl/forms/fnt6CkOOsaSUilIC3" target="blank">form</a>
</p>
<p>
<a href="https://youtu.be/QiLXx-0W8Q4?t=1m7s" target="blank">We hope you like pain</a>.
<a href="https://youtu.be/QiLXx-0W8Q4?t=1m7s" target="blank">We hope you don't like pain</a>.
</p>
Kelly & Timur, Team Async
`,
},
{
name: 'invitation',
subject: 'Async: Team Invitation',
message: `You've been invited to join {{teamName}}.
subject: 'You are invited to join a team at async-await.com',
message: `You've been invited to join <b>{{teamName}}</b>.
<br/>Click here to accept the invitation: {{invitationURL}}
`,
},
Expand Down
8 changes: 4 additions & 4 deletions app/components/common/LoginButton.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import React from 'react';

import Button from '@material-ui/core/Button';
import env from '../../lib/env';

import { styleLoginButton } from '../../lib/sharedStyles';

// TS errors: https://github.com/mui-org/material-ui/issues/8198

const dev = process.env.NODE_ENV !== 'production';
const LOGIN_URL = dev ? 'http://localhost:8000' : 'https://saas-api.async-await.com';
const { PRODUCTION_URL_API } = env;
const LOGIN_URL = dev ? 'http://localhost:8000' : PRODUCTION_URL_API;

class LoginButton extends React.PureComponent<{ next?: string; invitationToken?: string }> {
render() {
Expand All @@ -16,9 +18,7 @@ class LoginButton extends React.PureComponent<{ next?: string; invitationToken?:
let url = `${LOGIN_URL}/auth/google`;

if (next && invitationToken) {
url += `?next=${url}&invitationToken=${invitationToken}`;
} else if (next) {
url += `?next=${url}`;
url += `?next=${next}&invitationToken=${invitationToken}`;
}

return (
Expand Down
2 changes: 1 addition & 1 deletion app/components/teams/InviteMember.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class InviteMember extends React.Component<Props, State> {
await store.currentTeam.inviteMember(email);

this.setState({ email: '' });
notify('Invited');
notify('You successfully sent invitation.');
this.props.onClose();
NProgress.done();
} catch (error) {
Expand Down
44 changes: 24 additions & 20 deletions app/pages/invitation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Error from 'next/error';
import Router from 'next/router';

import LoginButton from '../components/common/LoginButton';
import { getInvitedTeamByToken } from '../lib/api/public';
import { getInvitedTeamByToken, removeInvitationIfMemberAdded } from '../lib/api/public';
import { Store, Team } from '../lib/store';
import withAuth from '../lib/withAuth';
import withLayout from '../lib/withLayout';
Expand All @@ -30,13 +30,17 @@ class Invitation extends React.Component<{ store: Store; team: Team; token: stri
}

componentDidMount() {
const { store, team } = this.props;
const { store, team, token } = this.props;

const user = store.currentUser;

if (user && team) {
if (team.memberIds.includes(user._id)) {
Router.push(`/projects?teamSlug=${team.slug}`, `/team/${team.slug}/projects`);
removeInvitationIfMemberAdded(token);
Router.push(
`/topics/detail?teamSlug=${team.slug}&topicSlug=projects`,
`/team/${team.slug}/t/projects`,
);
} else {
Router.push(`/`);
}
Expand All @@ -62,23 +66,23 @@ class Invitation extends React.Component<{ store: Store; team: Team; token: stri
<title>Team Invitation: {team.name}</title>
<meta name="description" content={`Invitation to join ${team.name}`} />
</Head>
<h2>
<Avatar
src={`${team.avatarUrl ||
'https://storage.googleapis.com/async-await/async-logo-40.svg'}`}
alt="Team logo"
style={{
margin: '10px auto 20px 0px',
cursor: 'pointer',
display: 'inline-flex',
}}
/>{' '}
{team.name}
</h2>

<p>Join {team.name} logging in with your Google account.</p>

<LoginButton next={`/team/${team.slug}/projects`} invitationToken={token} />
<br />
<br />
<Avatar
src={`${team.avatarUrl ||
'https://storage.googleapis.com/async-await/async-logo-40.svg'}`}
alt="Team logo"
style={{
verticalAlign: 'middle',
display: 'inline-flex',
}}
/>{' '}
<h2>{team.name}</h2>
<p>
Join <b>{team.name}</b> by logging in with your Google account.
</p>
<br />
<LoginButton next={`/team/${team.slug}/t/projects`} invitationToken={token} />
</div>
);
}
Expand Down

0 comments on commit 3f247b6

Please sign in to comment.