Skip to content

Commit

Permalink
Route to controlled next path after social login
Browse files Browse the repository at this point in the history
  • Loading branch information
gocreating committed Oct 24, 2016
1 parent 1a9d027 commit 8dcb7d6
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 38 deletions.
67 changes: 36 additions & 31 deletions src/common/components/pages/user/LoginPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,41 @@ import PageLayout from '../../layouts/PageLayout';
import Head from '../../widgets/Head';
import LoginForm from '../../forms/LoginForm';

const LoginPage = (props) => (
<PageLayout>
<Head
links={[
'https://cdnjs.cloudflare.com/ajax/libs/bootstrap-social/5.0.0/bootstrap-social.min.css',
]}
/>
<PageHeader>Login</PageHeader>
<Grid>
<Row>
<Col md={9}>
<LoginForm location={props.location} />
</Col>
<Col md={3}>
<a
href="/auth/facebook"
className="btn btn-block btn-social btn-facebook"
>
<span className="fa fa-facebook"></span>Login with Facebook
</a>
<a
href="/auth/linkedin"
className="btn btn-block btn-social btn-linkedin"
>
<span className="fa fa-linkedin"></span>Login with LinkedIn
</a>
</Col>
</Row>
</Grid>
</PageLayout>
);
let LoginPage = ({ location }) => {
let { next } = location.query;
let search = next ? '?next=' + next : '';

return (
<PageLayout>
<Head
links={[
'https://cdnjs.cloudflare.com/ajax/libs/bootstrap-social/5.0.0/bootstrap-social.min.css',
]}
/>
<Grid>
<PageHeader>Login</PageHeader>
<Row>
<Col md={9}>
<LoginForm />
</Col>
<Col md={3}>
<a
href={`/auth/facebook${search}`}
className="btn btn-block btn-social btn-facebook"
>
<span className="fa fa-facebook"></span>Login with Facebook
</a>
<a
href={`/auth/linkedin${search}`}
className="btn btn-block btn-social btn-linkedin"
>
<span className="fa fa-linkedin"></span>Login with LinkedIn
</a>
</Col>
</Row>
</Grid>
</PageLayout>
);
};

export default LoginPage;
20 changes: 14 additions & 6 deletions src/server/controllers/socialAuth.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import passport from 'passport';

export default {
initFacebook: passport.authenticate('facebook', {
scope: ['public_profile', 'email'],
}),
initLinkedin: passport.authenticate('linkedin', {
state: Math.random(),
}),
initFacebook: (req, res, next) => (
passport.authenticate('facebook', {
scope: ['public_profile', 'email'],
state: JSON.stringify({ next: req.query.next }),
})(req, res, next)
),
initLinkedin: (req, res, next) => (
passport.authenticate('linkedin', {
state: JSON.stringify({
next: req.query.next,
random: Math.random(),
}),
})(req, res, next)
),
};
5 changes: 4 additions & 1 deletion src/server/controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { handleDbError } from '../decorators/handleError';
import User from '../models/User';
import filterAttribute from '../utils/filterAttribute';
import { loginUser } from '../../common/actions/userActions';
import { redirect } from '../../common/actions/routeActions';

export default {
list(req, res) {
Expand Down Expand Up @@ -87,10 +88,12 @@ export default {
}))
.then(() => {
let { token, user } = req.store.getState().cookies;
let state = JSON.parse(req.query.state);

res.cookie('token', token);
res.cookie('user', user);
res.redirect('/');
req.store.dispatch(redirect(state.next || '/'));
return next();
});
}));
},
Expand Down

0 comments on commit 8dcb7d6

Please sign in to comment.