From d0fd8ad27fa3aaa84f8465e4cd9140ee80b501de Mon Sep 17 00:00:00 2001 From: Mike Rode Date: Thu, 5 Sep 2019 00:39:05 -0500 Subject: [PATCH] Passport local auth and front end for registration, functioning but needs work --- client/src/components/App.js | 2 + client/src/components/Hero.js | 8 + client/src/components/auth/Login.js | 118 +++++++++++++++ client/src/components/auth/SignUp.js | 17 +-- server/controllers/auth.controller.js | 39 ++--- ...190905024038-add_password_to_user_model.js | 11 ++ server/db/models/user.js | 1 + server/index.js | 6 +- server/services/auth/passport.js | 143 ++++++++++++------ 9 files changed, 267 insertions(+), 78 deletions(-) create mode 100644 server/db/migrations/20190905024038-add_password_to_user_model.js diff --git a/client/src/components/App.js b/client/src/components/App.js index abdb517..180f655 100644 --- a/client/src/components/App.js +++ b/client/src/components/App.js @@ -13,6 +13,7 @@ import PopularList from './PopularList'; import PlexTokenForm from './plex/PlexTokenForm'; import TopRatedList from './TopRatedList'; import SignUp from './auth/SignUp'; +import Login from './auth/Login'; class App extends Component { componentDidMount() { @@ -29,6 +30,7 @@ class App extends Component { + diff --git a/client/src/components/Hero.js b/client/src/components/Hero.js index c7428a2..2fd991d 100644 --- a/client/src/components/Hero.js +++ b/client/src/components/Hero.js @@ -52,6 +52,14 @@ class Hero extends Component { group Login with Google +
+ + show_chartLogin + +
{ + event.preventDefault(); + console.log('loginuserstate', this.state); + this.loginUser(this.state); + }; + + loginUser = async params => { + console.log('params', params); + const res = await axios({ + method: 'post', + url: '/api/auth/login', + data: params, + }); + console.log('login response', res.data); + window.location.reload(); + }; + + render() { + const {classes} = this.props; + if (this.props.auth) { + return ; + } else if (!this.props.auth) { + return ( + + +
+
+
+
+ +
+
+
+
{this.state.errorMessage}
+
+
+
+
+
+
+
+

Email

+ this.setState({email: e.target.value})} + /> +
+
+ +
+
+

Password

+ + this.setState({password: e.target.value}) + } + /> +
+
+ +
+
+
+ +
+
+
+
+
+
+
+
+
+ ); + } + } +} + +Login.propTypes = { + classes: PropTypes.object.isRequired, +}; + +function mapStateToProps({auth}) { + return {auth}; +} + +export default connect(mapStateToProps)(withStyles(styles)(Login)); diff --git a/client/src/components/auth/SignUp.js b/client/src/components/auth/SignUp.js index 6279280..4a6150b 100644 --- a/client/src/components/auth/SignUp.js +++ b/client/src/components/auth/SignUp.js @@ -13,8 +13,6 @@ class SignUp extends React.Component { state = { email: '', password: '', - errorMessage: '', - redirect: false, }; onFormSubmit = event => { @@ -25,14 +23,13 @@ class SignUp extends React.Component { signUpUser = async params => { console.log('params', params); - const res = await axios.post('/api/auth/login', {params}); - if (res.data.includes('Invalid')) { - console.log('signup response', res); - this.setState({errorMessage: res.data}); - } else { - console.log('signup response', res); - window.location.reload(); - } + const res = await axios({ + method: 'post', + url: '/api/auth/sign-up', + data: params, + }); + console.log('signup response', res); + window.location.reload(); }; render() { diff --git a/server/controllers/auth.controller.js b/server/controllers/auth.controller.js index 286e561..0dd3434 100644 --- a/server/controllers/auth.controller.js +++ b/server/controllers/auth.controller.js @@ -13,29 +13,34 @@ router.get( }, ); -router.post( - '/login', - function(req, res, next) { - console.log('Request params', req.params); - console.log('Request body', req.body); - console.log('Request query', req.query); - next(); - }, - passport.authenticate('local', { - failureRedirect: '/sign-up', - failureFlash: true, - }), - function(req, res) { - console.log('res', res.body); - res.redirect('/'); - }, -); +router.post('/sign-up', function(req, res, next) { + passport.authenticate('local-signup', function(err, user, info) { + if (err) { + console.log('sign up error', err); + return next(err); + } + if (!user) { + console.log('no user returned', info); + return res.json({message: info.message}); + } + console.log('user found', user); + res.json(user); + })(req, res, next); +}); + +router.post('/login', passport.authenticate('local-login'), function(req, res) { + // If this function gets called, authentication was successful. + // `req.user` contains the authenticated user. + console.log('User in session', req.user); + res.redirect('/'); +}); router.get('/google/callback', passport.authenticate('google'), (req, res) => { res.redirect('/plex-pin'); }); router.get('/current_user', (req, res) => { + console.log('current user', req.user); res.send(req.user); }); diff --git a/server/db/migrations/20190905024038-add_password_to_user_model.js b/server/db/migrations/20190905024038-add_password_to_user_model.js new file mode 100644 index 0000000..bdd43ca --- /dev/null +++ b/server/db/migrations/20190905024038-add_password_to_user_model.js @@ -0,0 +1,11 @@ +module.exports = { + up: function(queryInterface, Sequelize) { + // logic for transforming into the new state + return queryInterface.addColumn('Users', 'password', Sequelize.STRING); + }, + + down: function(queryInterface, Sequelize) { + // logic for reverting the changes + return queryInterface.removeColumn('Users', 'password'); + }, +}; diff --git a/server/db/models/user.js b/server/db/models/user.js index 9c366fa..722e7e7 100644 --- a/server/db/models/user.js +++ b/server/db/models/user.js @@ -14,6 +14,7 @@ module.exports = (sequelize, DataTypes) => { sonarrUrl: DataTypes.STRING, sonarrApiKey: DataTypes.STRING, admin: DataTypes.BOOLEAN, + password: DataTypes.STRING, }, {}, ); diff --git a/server/index.js b/server/index.js index dd3181d..9db7cf6 100644 --- a/server/index.js +++ b/server/index.js @@ -85,9 +85,9 @@ export default () => { }); }; - process.on('uncaughtException', error => { - console.log('Unhandled Exception due to:', error); - process.exit(1); + process.on('SIGINT', function() { + console.log('SIGINT'); + process.exit(); }); process.on('unhandledRejection', (reason, p) => { diff --git a/server/services/auth/passport.js b/server/services/auth/passport.js index cb46029..cfdc5db 100644 --- a/server/services/auth/passport.js +++ b/server/services/auth/passport.js @@ -7,62 +7,114 @@ import keys from '../../../config'; import models from '../../db/models'; passport.serializeUser((user, done) => { + console.log('serial', user); done(null, user.id); }); passport.deserializeUser((id, done) => { + console.log('deserial', id); models.User.findByPk(id).then(user => { done(null, user); }); }); -// passport.use( -// new LocalStrategy( -// {usernameField: 'email', passwordField: 'password'}, -// async (email, password, done) => { -// try { -// console.log('Made it to passport', email); -// const existingUser = await User.findOne({email: email}); -// if (existingUser) { -// done(null, existingUser); -// } -// if (!user) { -// done(null, false, {message: 'Incorrect username.'}); -// } -// if (!user.validPassword(password)) { -// done(null, false, {message: 'Incorrect password.'}); -// } -// const hashedPassword = generateHash(password); -// const user = await models.User.create({ -// email: email, -// password: hashedPassword, -// }); - -// done(null, user); -// } catch (error) { -// console.log('passport error', error); -// done(error); -// } -// }, -// ), -// ); +const generateHash = password => { + return bCrypt.hashSync(password, bCrypt.genSaltSync(8), null); +}; passport.use( + 'local-signup', new LocalStrategy( - {usernameField: 'email', passwordField: 'password'}, - function(username, password, done) { - User.findOne({email: email}, function(err, user) { - if (err) { - return done(err); - } - if (!user) { - return done(null, false, {message: 'Incorrect email.'}); - } - if (!user.validPassword(password)) { - return done(null, false, {message: 'Incorrect password.'}); - } - return done(null, user); + { + usernameField: 'email', + passwordField: 'password', + }, + + async function(email, password, done) { + console.log('passport - signup', email); + const exisitingUser = await models.User.findOne({ + where: {email: email}, + returning: true, + plain: true, + raw: true, }); + if (exisitingUser) { + return done(null, false, { + message: 'That email is already taken', + }); + } + const userPassword = generateHash(password); + + const data = { + email: email, + password: userPassword, + }; + + console.log('user data before save', data); + const newUser = models.User.create(data, { + returning: true, + plain: true, + raw: true, + }).then(function(newUser, created) { + if (!newUser) { + return done(null, false); + } + + if (newUser) { + console.log('new user created', newUser); + return done(null, newUser); + } + }); + }, + ), +); + +//LOCAL SIGNIN +passport.use( + 'local-login', + new LocalStrategy( + { + usernameField: 'email', + passwordField: 'password', + }, + + function(email, password, done) { + var isValidPassword = function(userpass, password) { + return bCrypt.compareSync(password, userpass); + }; + + models.User.findOne({ + where: { + email: email, + }, + returning: true, + plain: true, + raw: true, + }) + .then(function(user) { + if (!user) { + return done(null, false, { + message: 'Email does not exist', + }); + } + + if (!isValidPassword(user.password, password)) { + return done(null, false, { + message: 'Incorrect password.', + }); + } + + console.log('user sent to serialize', user.id); + + return done(null, user); + }) + .catch(function(err) { + console.log('Error:', err); + + return done(null, false, { + message: 'Something went wrong with your Signin', + }); + }); }, ), ); @@ -96,8 +148,3 @@ passport.use( }, ), ); - -// should export to commonUtils file -const generateHash = string => { - return bCrypt.hashSync(string, bCrypt.genSaltSync(8), null); -};