Skip to content
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

fix (serialization of state): Changing the state object to a new vari… #48

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/src/app/actions/authenticate.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export const login = credentials => dispatch => api
*/
export const logout = () => (dispatch) => {
localStorage.removeItem('token');
localStorage.removeItem('http://hellobooks');
localStorage.removeItem('http://hellobooks:state');
localStorage.clear();
setAuthorizationToken(false);
dispatch(userLoggedOut());
Expand Down
2 changes: 1 addition & 1 deletion client/src/app/actions/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const showErrorNotification = ({ message, error }) => (dispatch) => {
kind: 'info',
dismissAfter: 3500
}));
localStorage.removeItem('http://hellobooks');
localStorage.removeItem('http://hellobooks:state');
localStorage.removeItem('token');
setAuthorizationToken(false);
} else {
Expand Down
5 changes: 3 additions & 2 deletions client/src/app/components/container/authentication/Logout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class Logout extends Component {
*/
componentDidMount() {
this.props.logout();
/* eslint-disable */
Materialize.toast("User Logging Out", 2500);
this.props.history.push('/');
}
/**
Expand All @@ -31,10 +33,9 @@ class Logout extends Component {
* @return {JSX} JSX representation of DOM
*/
render() {
this.setState({ isAuthneticated: false });
return (
<Row className="center landing">
<h1 className="">
<h1>
Logging out...
</h1>
</Row>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,5 +196,11 @@ const mapStateToProps = state => ({

});

export default connect(mapStateToProps, {
deleteBookAction, fetchAllBooks, fetchSelectedBook })(Book);
export default connect(
mapStateToProps,
{
deleteBookAction,
fetchAllBooks,
fetchSelectedBook
}
)(Book);
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ EditBookModal.defaultProps = {


EditBookModal.propTypes = {
book: PropTypes.object,
book: PropTypes.array,
header: PropTypes.string,
updateBookDetails: PropTypes.func.isRequired
};
Expand Down
4 changes: 2 additions & 2 deletions client/src/app/utils/localSave.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
export const saveState = (state) => {
const serializedState = JSON.stringify(state);
localStorage.setItem('http://hellobooks', serializedState);
localStorage.setItem('http://hellobooks:state', serializedState);
};

const initializeState = {
Expand All @@ -22,7 +22,7 @@ const initializeState = {
*/
export const loadState = () => {
try {
const serializedState = localStorage.getItem('http://hellobooks');
const serializedState = localStorage.getItem('http://hellobooks:state');
if (serializedState === null) {
return initializeState;
}
Expand Down
41 changes: 28 additions & 13 deletions server/src/controllers/books.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,25 @@ export default {
categoryId: req.body.categoryId,
description: req.body.description,
bookImage: req.body.bookImage || process.env.DEFAULT_BOOK_COVER

})
.then((createdBook) => {
createdBook
.then((newBook) => {
newBook
.getCategory()
.then((category) => {
const newBook = {
title: createdBook.title,
category: category.categoryName
const createdBook = {
title: newBook.title,
category: category.categoryName,
author: newBook.author,
description: newBook.description,
bookImage: newBook.bookImage
};
res
.status(201)
.send({
message: `${newBook.title} ` +
message: `${createdBook.title} ` +
`has been added to the library,` +
`Category: ${newBook.category}`,
`Category: ${createdBook.category}`,
createdBook
});
});
Expand Down Expand Up @@ -137,18 +141,21 @@ export default {
.updateAttributes(req.body, {
fields: Object.keys(req.body)
})
.then((updatedBook) => {
updatedBook
.then((editedBook) => {
editedBook
.getCategory()
.then((category) => {
const newBook = {
title: updatedBook.title,
category: category.categoryName
const updatedBook = {
title: editedBook.title,
category: category.categoryName,
author: editedBook.author,
description: editedBook.description,
bookImage: editedBook.bookImage
};
res
.status(200)
.send({
message: `${newBook.title} has been updated`,
message: `${updatedBook.title} has been updated`,
updatedBook
});
});
Expand Down Expand Up @@ -178,6 +185,14 @@ export default {
.findAndCountAll({
limit,
offset,
attributes: [
'id',
'title',
'author',
'categoryId',
'description',
'bookImage'
],
order: [
['createdAt', 'DESC']
]
Expand Down
11 changes: 9 additions & 2 deletions server/src/controllers/category.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ export default {
Categories.create({
categoryName
})
.then((category) => {
.then((newCategory) => {
const category = {
categoryName: newCategory.categoryName,
id: newCategory.id
};
res.status(201)
.send({
message: `Category added!, ${category.categoryName}`,
Expand Down Expand Up @@ -93,7 +97,10 @@ export default {
*/
listCategories(req, res) {
return Categories
.all({ order: [['categoryName', 'ASC']] })
.all({
attributes: ['id', 'categoryName'],
order: [['categoryName', 'ASC']]
})
.then((categories) => {
if (Object.keys(categories).length < 1) {
return res.status(404)
Expand Down
15 changes: 8 additions & 7 deletions server/src/controllers/middleware/authenticate.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const authenticate = (req, res, next) => {
if (error) {
return res.status(401).json({
token: null,
state: {},
message: 'Unauthorised access'
});
}
Expand All @@ -41,7 +40,6 @@ const authenticate = (req, res, next) => {
} else {
res.status(401).send({
token: null,
state: {},
message: 'Unauthorised access'
});
}
Expand All @@ -50,13 +48,13 @@ const authenticate = (req, res, next) => {
/**
* @description Decode Token for the server side processes
*
* @param {object} req
* @param {object} req - HTTP request object
*
* @param {object} res
* @param {object} res - HTTP response object
*
* @param {object} next
* @param {undefined} next
*
* @returns {object} res
* @returns {object} res - decodedToken
*/
const decodeToken = (req, res, next) => {
const token = req.headers['x-access-token'] || req.headers.authorization;
Expand All @@ -65,15 +63,18 @@ const decodeToken = (req, res, next) => {
req.userId = decodedToken.id.id;
next();
} else {
res.status(401).send({ message: 'Unauthorised access' });
res.status(401).send({ token: null, message: 'Unauthorised access' });
}
};


/**
* @description Generates a json web token with the supplied parameters
*
* @param {number} id
*
* @param {boolean} isAdmin
*
* @return {promise} signed token
*/
const getJWT = (id, isAdmin) =>
Expand Down
9 changes: 4 additions & 5 deletions server/src/controllers/middleware/checkAdmin.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import jwtDecode from 'jwt-decode';

/**
* R
*
*
* @description Middleware that to check token for administrator parameters
*
Expand All @@ -17,10 +17,9 @@ export default (req, res, next) => {
const token = req.headers['x-access-token'] || req.headers.authorization;
const decodedToken = jwtDecode(token);
if (decodedToken.id.isAdmin) {
next(null, {
isAdmin: decodedToken.id.isAdmin
});
req.isAdmin = decodedToken.id.isAdmin;
next();
} else {
res.status(403).send({ message: 'You are forbidden, Sorry' });
res.status(403).send({ token: null, message: 'Unauthorised Access' });
}
};
2 changes: 1 addition & 1 deletion server/src/controllers/middleware/checkGoogleAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default (req, res, next) => {
familyName: lastname,
imageUrl: userImage
} = req.body;
const username = email.slice(0, email.indexOf('@')) + googleId.slice(5);
const username = email.slice(0, email.indexOf('@')) + googleId.slice(0, 3);
const password = googleId;
const passwordConfirmation = googleId;
req.body = {
Expand Down
8 changes: 6 additions & 2 deletions server/src/controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ const userController = {
googleId: req.body.googleId || null,
isAdmin: req.body.isAdmin
}).then((user) => {
if (user) {
if (user.googleId)
{
return userController.signIn(req, res);
}
else {
res.status(201).send({
message: `${user.username} has been added to the ` +
'library, Please Login, you will be only ' +
Expand Down Expand Up @@ -91,7 +95,7 @@ const userController = {
.then((user) => {
if (!user) {
if (googleId) {
return userController.create(req, res);
return userController.createUser(req, res);
}
return res.status(404).send({
success: false,
Expand Down
10 changes: 9 additions & 1 deletion server/src/controllers/userBooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ export default {
* @return {string} message - returns message
*
* @return {Object} books
*
*
* @return {object} pagination - returns pagination
*
*/
Expand All @@ -396,6 +396,14 @@ export default {
required: true
}
],
attributes: [
'bookId',
'userId',
'returnDate',
'userReturnDate',
'returnStatus',
'overdueAmount'
],
limit,
offset,
order: [
Expand Down
34 changes: 17 additions & 17 deletions server/src/test/authenticate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,22 @@ describe('Authentication', () => {
done();
});
});
it('should return a 401 status ' +
'code if token is not an admin token', (done) => {
chai
.request(app)
.post('/api/v1/admin/category')
.set('x-access-token', userToken)
.send({ categoryName: 'EDUCATIONAL' })
.end((err, res) => {
expect(res.status)
.to
.equal(403);
expect(res.body.token)
.to.equal(undefined);
done();
});
});
it(`should return a 403 status ' +
'code if token is not an admin token`, (done) => {
chai
.request(app)
.post('/api/v1/admin/category')
.set('x-access-token', userToken)
.send({ categoryName: 'EDUCATIONAL' })
.end((err, res) => {
expect(res.status)
.to
.equal(403);
expect(res.body.token)
.to.equal(null);
done();
});
});
it('should return 401 if there is an error with token', (done) => {
chai
.request(app)
Expand Down Expand Up @@ -172,7 +172,7 @@ describe('Authentication', () => {
.post('/api/v1/auth/users/signin')
.send(newUser)
.end((err, res) => {
expect(res.status).to.equal(500);
expect(res.status).to.equal(400);
done();
});
}
Expand Down