Skip to content

Commit

Permalink
Add view and route to link social account to main account on signup.
Browse files Browse the repository at this point in the history
  • Loading branch information
jneto committed May 7, 2015
1 parent f5991c2 commit 2105a48
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
35 changes: 33 additions & 2 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,12 @@ module.exports = function(passport) {
user.socialOauthIds[req.body.socialNetwork] = req.body.socialLoginOauthId;
User.register(user, req.body.password, function(err, user) {
if (err) {
return res.render("signup", {info: "Sorry. That username already exists. Try again."});
return res.render('signup', {
message: 'Sorry. That username already exists. Try again.',
socialNetwork: req.body.socialNetwork,
socialLoginOauthId: req.body.socialLoginOauthId,
csrfToken: req.csrfToken()
});
}

//send email verification
Expand All @@ -124,6 +129,32 @@ module.exports = function(passport) {
});
});

router.post('/connect-account', function(req, res, next) {
passport.authenticate('local', function(err, user, info) {
if (err) {
return next(err);
}
if (!user) {
res.render('signup', {
message: 'invalid email or password',
socialNetwork: req.body.socialNetwork,
socialLoginOauthId: req.body.socialLoginOauthId,
csrfToken: req.csrfToken()

});
} else {
user.socialOauthIds[req.body.socialNetwork] = req.body.socialLoginOauthId;
user.save(function(err) {
if (err) {
next(err);
} else {
res.redirect('/');
}
});
}
})(req, res, next);
});

router.get('/email-verification', function(req, res) {
res.render('email-verification', {
title: 'Email verification sent!',
Expand Down Expand Up @@ -180,7 +211,7 @@ module.exports = function(passport) {
if (!user) {
res.render('signup', {
socialNetwork: socialNetwork,
profile: profile,
socialLoginOauthId: profile.id,
csrfToken: req.csrfToken()
});
} else {
Expand Down
16 changes: 15 additions & 1 deletion views/signup.jade
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,24 @@ block content
div.container-fluid
if socialNetwork
h1 connect your #{socialNetwork} account to your speakerthumbs account
form(class='form-horizontal', role='form', action='/connect-account', method='POST')
input(type="hidden", name="_csrf", value="#{csrfToken}")
input(type="hidden", name="socialLoginOauthId", value="#{socialLoginOauthId}")
input(type="hidden", name="socialNetwork", value="#{socialNetwork}")
div.form-group
label(for='user-email', class='control-label col-sm-2') Email
div.col-sm-10
input(type='text', class='form-control', id='user-email', name='email', placeholder='Email')
div.form-group
label(for='user-password', class='control-label col-sm-2') Password
div.col-sm-10
input(type='password', class='form-control', id='user-password', name='password', placeholder='Password')
button(type='submit', class='btn btn-primary pull-right') Connect my account!
h1 if you don't have an account, create one
form(class='form-horizontal', role='form', action='/signup', method='POST')
input(type="hidden", name="_csrf", value="#{csrfToken}")
if socialNetwork
input(type="hidden", name="socialLoginOauthId", value="#{profile.id}")
input(type="hidden", name="socialLoginOauthId", value="#{socialLoginOauthId}")
input(type="hidden", name="socialNetwork", value="#{socialNetwork}")
div.form-group
label(for='user-name', class='control-label col-sm-2') Name
Expand Down

0 comments on commit 2105a48

Please sign in to comment.