Skip to content
Open
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: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"@testing-library/react": "^9.5.0",
"@testing-library/user-event": "^7.2.1",
"auth0-js": "^9.13.4",
"bcrypt": "^5.0.0",
"bcryptjs": "^2.4.3",
"bootstrap": "^4.5.0",
"eslint": "^6.6.0",
"fomantic-ui-css": "^2.8.6",
Expand Down
1 change: 1 addition & 0 deletions src/reducers/actions/user.actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ function register(user) {
dispatch(alertActions.success('Registration successful'));
},
error => {
// console.log(error)
dispatch(failure(error.toString()));
dispatch(alertActions.error(error.toString()));
}
Expand Down
18 changes: 15 additions & 3 deletions src/services/user.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { authHeader } from '../helpers';
const config = {
apiUrl: 'http://localhost:4000'
}

export const userService = {
login,
logout,
Expand All @@ -13,6 +12,9 @@ export const userService = {
delete: _delete
};

const bcrypt = require('bcryptjs');
const saltRounds = 10;

function login(username, password) {
const requestOptions = {
method: 'POST',
Expand Down Expand Up @@ -53,7 +55,15 @@ function getById(id) {
return fetch(`${config.apiUrl}/users/${id}`, requestOptions).then(handleResponse);
}

function register(user) {

async function register(user) {
const hashedPassword = await new Promise((resolve, reject) => {
bcrypt.hash(user.password, saltRounds, function(err, hash) {
if (err) reject(err)
resolve(hash)
});
})
user.password = hashedPassword;
const requestOptions = {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
Expand Down Expand Up @@ -84,15 +94,17 @@ function _delete(id) {
}

function handleResponse(response) {

return response.text().then(text => {
const data = text && JSON.parse(text);

if (!response.ok) {
if (response.status === 401) {
// auto logout if 401 response returned from api
logout();
window.location.reload(true);
}

const error = (data && data.message) || response.statusText;
return Promise.reject(error);
}
Expand Down