diff --git a/server/accounts.js b/server/accounts.js index 9729a67..7f4934a 100644 --- a/server/accounts.js +++ b/server/accounts.js @@ -48,7 +48,7 @@ function signup(connection) { bcrypt.hash(fields.password, salt, (err, hash) => { if (err) throw err; - //generate a random number as a key + //generate a random number as a token let rand = Math.floor(Math.random() * 100000); //save the generated data to the signups table @@ -198,9 +198,61 @@ function logout(connection) { } } +function passwordChange(connection) { + return (req, res) => { + //formidable handles forms + let form = formidable.IncomingForm(); + + //parse form + form.parse(req, (err, fields) => { + if (err) throw err; + + //validate password, retype + if (!validateEmail(fields.email) || fields.password.length < 8 || fields.password !== fields.retype) { + res.write('
Invalid password change data
'); + res.end(); + return; + } + + //generate the new salt, hash + bcrypt.genSalt(11, (err, salt) => { + if (err) throw err; + bcrypt.hash(fields.password, salt, (err, hash) => { + if (err) throw err; + + let query = 'UPDATE accounts SET salt = ?, hash = ? WHERE email = ?;'; + connection.query(query, [salt, hash, fields.email], (err) => { + if (err) throw err; + + //clear all session data for this user (a 'feature') + let query = 'DELETE FROM sessions WHERE sessions.accountId IN (SELECT accounts.id FROM accounts WHERE email = ?);'; + connection.query(query, [fields.email], (err) => { + if (err) throw err; + + //create the new session + let rand = Math.floor(Math.random() * 100000); + + let query = 'INSERT INTO sessions (accountId, token) VALUES ((SELECT accounts.id FROM accounts WHERE email = ?), ?);'; + connection.query(query, [fields.email, rand], (err) => { + if (err) throw err; + + //send json containing the account info + res.status(200).json({ + token: rand + }); + }); + }); + }); + }); + }); + }); + } +} + module.exports = { signup: signup, verify: verify, login: login, - logout: logout + logout: logout, + passwordChange: passwordChange }; \ No newline at end of file diff --git a/server/index.js b/server/index.js index 92619a3..598bb22 100644 --- a/server/index.js +++ b/server/index.js @@ -20,6 +20,7 @@ app.post('/signup', accounts.signup(connection)); app.get('/verify', accounts.verify(connection)); app.post('/login', accounts.login(connection)); app.post('/logout', accounts.logout(connection)); +app.post('/passwordchange', accounts.passwordChange(connection)); //static directories app.use('/styles', express.static(path.resolve(__dirname + '/../public/styles')) ); diff --git a/src/actions/accounts.js b/src/actions/accounts.js index 6388305..1253bb3 100644 --- a/src/actions/accounts.js +++ b/src/actions/accounts.js @@ -1,5 +1,6 @@ export const LOGIN = 'LOGIN'; export const LOGOUT = 'LOGOUT'; +export const SESSIONCHANGE = 'SESSIONCHANGE'; export function login(id, email, username, token) { return { @@ -16,3 +17,10 @@ export function logout() { type: LOGOUT }; } + +export function sessionChange(token) { + return { + type: SESSIONCHANGE, + token: token + } +} \ No newline at end of file diff --git a/src/components/pages/home.jsx b/src/components/pages/home.jsx index d876fa6..0f767e1 100644 --- a/src/components/pages/home.jsx +++ b/src/components/pages/home.jsx @@ -7,28 +7,47 @@ import PropTypes from 'prop-types'; import Signup from '../panels/signup.jsx'; import Login from '../panels/login.jsx'; import Logout from '../panels/logout.jsx'; +import PasswordChange from '../panels/password_change.jsx'; class Home extends React.Component { constructor(props) { super(props); - this.state = {}; + this.state = { + changedPassword: false + }; } render() { - //well this is goofy + //DEBUGGING: well this is goofy let SidePanel; if (this.props.id) { SidePanel = () => { + let PasswordChangePanel; + + if (!this.state.changedPassword) { + PasswordChangePanel = () => { + return (Password changed!
); + } + } + return (You are logged in.
+{this.state.warning}
+