fix passport strategy config, remove github button from signup form

This commit is contained in:
catarak 2016-08-31 15:30:37 -04:00
parent 7a8e77282d
commit a741e4aaa8
2 changed files with 23 additions and 39 deletions

View file

@ -3,15 +3,12 @@ import { bindActionCreators } from 'redux';
import * as UserActions from '../actions'; import * as UserActions from '../actions';
import { reduxForm } from 'redux-form'; import { reduxForm } from 'redux-form';
import SignupForm from '../components/SignupForm'; import SignupForm from '../components/SignupForm';
import GithubButton from '../components/GithubButton';
function SignupView(props) { function SignupView(props) {
return ( return (
<div className="signup"> <div className="signup">
<h1>Sign Up</h1> <h1>Sign Up</h1>
<SignupForm {...props} /> <SignupForm {...props} />
<h2 className="signup__divider">Or</h2>
<GithubButton buttonText="Sign Up with Github" />
</div> </div>
); );
} }

View file

@ -40,32 +40,20 @@ passport.use(new GitHubStrategy({
callbackURL: '/auth/github/callback', callbackURL: '/auth/github/callback',
passReqToCallback: true passReqToCallback: true
}, (req, accessToken, refreshToken, profile, done) => { }, (req, accessToken, refreshToken, profile, done) => {
if (req.user) {
// the user should actually never get here.
User.findOne({ github: profile.id }, (err, existingUser) => {
if (existingUser) {
return res.json({'errors', { msg: 'There is already a GitHub account that belongs to you. Sign in with that account or delete it, then link it with your current account.' }});
} else {
User.findById(req.user.id, (err, user) => {
user.email = user.email || profile._json.email;
user.github = profile.id;
user.username = user.username || profile.username;
user.tokens.push({ kind: 'github', accessToken });
user.name = user.name || profile.displayName;
user.save((err) => {
return res.json({'info', { msg: 'GitHub account has been linked.' }});
});
});
}
});
} else {
User.findOne({ github: profile.id }, (err, existingUser) => { User.findOne({ github: profile.id }, (err, existingUser) => {
if (existingUser) { if (existingUser) {
return done(null, existingUser); return done(null, existingUser);
} }
User.findOne({ email: profile._json.email }, (err, existingEmailUser) => { User.findOne({ email: profile._json.email }, (err, existingEmailUser) => {
if (existingEmailUser) { if (existingEmailUser) {
return res.json('errors', { msg: 'There is already an account using this email address. Sign in to that account and link it with GitHub manually from Account Settings.' }); existingEmailUser.email = existingEmailUser.email || profile._json.email;
existingEmailUser.github = profile.id;
existingEmailUser.username = existingEmailUser.username || profile.username;
existingEmailUser.tokens.push({ kind: 'github', accessToken });
existingEmailUser.name = existingEmailUser.name || profile.displayName;
existingEmailUser.save((err) => {
return done(null, existingEmailUser);
});
} else { } else {
const user = new User(); const user = new User();
user.email = profile._json.email; user.email = profile._json.email;
@ -74,10 +62,9 @@ passport.use(new GitHubStrategy({
user.tokens.push({ kind: 'github', accessToken }); user.tokens.push({ kind: 'github', accessToken });
user.name = profile.displayName; user.name = profile.displayName;
user.save((err) => { user.save((err) => {
return res.json({'info', { msg: 'Account has been created with GitHub credentials.' }}); return done(null, user);
}); });
} }
}); });
}); });
}
})); }));