-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/google signin #50
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Initialize Firebase // firebase.initializeApp(config); | ||
// TODO: MOVE THESE TO ENVIRONMENT VARIABLES | ||
export default { | ||
apiKey: 'AIzaSyCUQ8B5UajnVa04E6hFUETBzKWD_4XlnVw', | ||
authDomain: 'hellobooks-180211.firebaseapp.com', | ||
databaseURL: 'https://hellobooks-180211.firebaseio.com', | ||
projectId: 'hellobooks-180211', | ||
storageBucket: 'hellobooks-180211.appspot.com', | ||
messagingSenderId: '701806023399' | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,249 @@ | ||
import bcrypt from 'bcrypt'; | ||
import dotenv from 'dotenv'; | ||
|
||
import { User, Book } from '../models'; | ||
import { getJWT } from '../helpers/helpers'; | ||
import { transporter, mailOptions } from '../config/mail'; | ||
|
||
dotenv.config(); | ||
|
||
|
||
const userController = { | ||
/** | ||
* Create new user account. | ||
* It sends a an object containing a success boolean | ||
* and a json web token or error | ||
* @public | ||
* @method | ||
* @param {object} req - express http request object | ||
* @param {object} res - express http response object | ||
* @return {Object} - returns an http response object | ||
*/ | ||
|
||
createUser(req, res) { | ||
const username = req.body.username; | ||
const email = req.body.email; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use object destructuring prefer-destructuring |
||
return User.find({ | ||
where: { $or: [{ username }, { email }] } | ||
}).then((existingUser) => { | ||
if (existingUser && existingUser.username === username) { | ||
return res.status(409).json({ | ||
message: 'username is taken', | ||
}); | ||
} | ||
if (existingUser && existingUser.email === email) { | ||
return res.status(409).json({ | ||
message: 'email is associated with an account', | ||
}); | ||
} | ||
User.create(req.body) | ||
.then((user) => { | ||
const { | ||
id, | ||
isAdmin, | ||
membershipType, | ||
} = user; | ||
const jwtOptions = { id, email, username, isAdmin, membershipType }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Expected a line break after this opening brace object-curly-newline |
||
const token = getJWT(jwtOptions); | ||
const { firstName, lastName } = user; | ||
return res.status(201).json({ | ||
token, | ||
id, | ||
firstName, | ||
lastName, | ||
isAdmin, | ||
message: `Welcome ${firstName}. This is your dashboard`, | ||
}); | ||
}) | ||
.catch(error => res.status(400).send({ | ||
error | ||
})); | ||
}) | ||
.catch(error => res.status(500).send({ | ||
error | ||
})); | ||
}, | ||
|
||
/** | ||
* Edit user Information | ||
* @public | ||
* @method | ||
* @param {object} req - express http request object | ||
* @param {object} res - express http response object | ||
* @return {Object} - returns an http response object | ||
*/ | ||
updateUserInfo(req, res) { | ||
const updateData = req.body; | ||
updateData.passwordResetToken = null; | ||
return User.findById(req.user.id) | ||
.then((user) => { | ||
user.update(updateData, { returning: true, plain: true }) | ||
.then(() => { | ||
const { | ||
id, | ||
email, | ||
username, | ||
isAdmin, | ||
membershipType, | ||
} = user; | ||
const jwtOptions = { id, email, username, isAdmin, membershipType }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Expected a line break after this opening brace object-curly-newline |
||
const token = getJWT(jwtOptions); | ||
const { firstName, lastName } = user; | ||
return res.status(200).json({ | ||
token, | ||
id, | ||
firstName, | ||
lastName, | ||
isAdmin, | ||
message: 'Your information was successfully updated', | ||
}); | ||
}, (error) => { | ||
res.status(500).send({ | ||
error, | ||
}); | ||
}); | ||
}) | ||
.catch(error => res.status(500).send({ | ||
error, | ||
})); | ||
}, | ||
|
||
/** | ||
* Get user data on sign in. | ||
* It sends a an object containing a success boolean | ||
* and a json web token or error | ||
* @public | ||
* @method | ||
* @param {object} req - express http request object | ||
* @param {object} res - express http response object | ||
* @return {Object} - returns an http response object | ||
*/ | ||
|
||
getUser(req, res) { | ||
const username = req.body.username; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use object destructuring prefer-destructuring |
||
const password = req.body.password; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use object destructuring prefer-destructuring |
||
return User.findOne({ where: { username } }).then((user) => { | ||
if (!user) { | ||
if (req.body.authId) { | ||
return userController.createUser(req, res); | ||
} | ||
return res.status(403).send({ | ||
message: 'user does not exist', | ||
}); | ||
} | ||
bcrypt.compare(password, user.password).then((result) => { | ||
if (!result) { | ||
return res.status(403).send({ | ||
message: 'wrong username and password combination', | ||
}); | ||
} | ||
const { | ||
id, | ||
email, | ||
isAdmin, | ||
membershipType, | ||
} = user; | ||
const jwtOptions = { id, email, username, isAdmin, membershipType }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Expected a line break after this opening brace object-curly-newline |
||
const token = getJWT(jwtOptions); | ||
const { firstName, lastName } = user; | ||
return res.status(200).json({ | ||
token, | ||
id, | ||
firstName, | ||
lastName, | ||
isAdmin, | ||
message: `Welcome back ${firstName}`, | ||
}); | ||
}).catch(error => res.status(500).send({ | ||
error, | ||
})); | ||
}).catch(error => res.status(400).send({ | ||
error | ||
})); | ||
}, | ||
|
||
/** | ||
* Get list of books borrowed by specific user | ||
* It sends a an object containing a success boolean | ||
* and a data key, an array of borrowed books or an error | ||
* Response can be filtered by returned status | ||
* @public | ||
* @method | ||
* @param {object} req - express http request object | ||
* @param {object} res - express http response object | ||
* @return {Object} - returns an http rresponse object | ||
*/ | ||
getBorrowedBooks(req, res) { | ||
const id = req.params.id; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use object destructuring prefer-destructuring |
||
User.findOne({ | ||
where: { id }, | ||
include: [{ model: Book }] | ||
}).then((user) => { | ||
let books; | ||
if (req.query && req.query.returned === 'false') { | ||
books = user.Books.filter( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unexpected newline after '(' function-paren-newline |
||
book => book.BorrowedBook.returned === false | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unexpected newline before ')' function-paren-newline |
||
} else if (req.query && req.query.returned === 'true') { | ||
books = user.Books.filter( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unexpected newline after '(' function-paren-newline |
||
book => book.BorrowedBook.returned === true | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unexpected newline before ')' function-paren-newline |
||
} else { | ||
books = user.Books; | ||
} | ||
return res.status(200).send({ | ||
books | ||
}); | ||
}) | ||
.catch(error => res.status(500).send({ | ||
message: 'An error occured while fetching borrowing history', | ||
error, | ||
})); | ||
}, | ||
|
||
passwordResetMail(req, res) { | ||
return User.findOne({ | ||
where: { email: req.body.email }, | ||
attributes: ['id', 'email'], | ||
plain: true, | ||
}) | ||
.then((user) => { | ||
if (!user) { | ||
return res.status(404).send({ | ||
message: 'Email does not match any account in our records', | ||
}); | ||
} | ||
const BASE_URL = process.env.NODE_ENV === 'development' ? | ||
'http://localhost:8080' : | ||
'https://segunolalive-hellobooks.com'; | ||
const token = getJWT({ id: user.id }, '1h'); | ||
user.passwordResetToken = token; | ||
user.save(); | ||
const to = user.email; | ||
const bcc = null; | ||
const subject = 'no-reply: Password reset link'; | ||
const html = `<h3>Use this link to reset your password.</h3> | ||
${BASE_URL}/reset-password?token=${token}} | ||
<p>This link is valid only for an hour</p>`; | ||
transporter.sendMail(mailOptions(to, bcc, subject, html), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Expected a newline after '(' function-paren-newline |
||
(err) => { | ||
if (err) { | ||
return res.status(500).send({ | ||
message: 'An error occured while sending you a link. Try again', | ||
}); | ||
} | ||
return res.status(200).send({ | ||
message: 'An password reset link has been sent to your email', | ||
}); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Expected a newline before ')' function-paren-newline |
||
}) | ||
.catch(() => ( | ||
res.status(500).send({ | ||
message: 'An error occured while sending you a link. Try again', | ||
}) | ||
)); | ||
} | ||
}; | ||
|
||
|
||
export default userController; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use object destructuring prefer-destructuring