'React, Heroku, unable to register new user
I migrated server and PostgresQL database to Heroku. However, when I test my new user registration form on Github, nothing happens. I checked the network response in the console, and get 400 Bad request, and "Unable to register" comment, which I set in handleRegister.
So it seems that it is communicating with Heroku, but not properly registering. It worked before when everything was stored locally. What am I missing?
Relevant segment of Server API:
const express = require('express');
const bodyParser = require('body-parser');
const bcrypt = require('bcrypt');
const app = express();
const cors = require('cors');
const knex = require('knex')({
client: 'pg',
connection: {
connectionString: process.env.DATABASE_URL,
ssl: true
}
});
const register = require('./controllers/register');
app.use(bodyParser.json());
app.use(cors());
app.get('/', (req, res) => {res.send("It's working!")})
app.post('/register', (req, res) => {register.handleRegister(req, res, knex, bcrypt)})
const PORT = process.env.PORT || 3000;
app.listen(PORT, err => {
if(err) throw err;
console.log("Server running");
});
Server register controller:
const handleRegister = (req, res, knex, bcrypt) => {
const { email, name, password} = req.body;
if (!email || !name || !password) {
return res.status(400).json('invalid form registration');
}
const salt = bcrypt.genSaltSync(saltRounds);
const hash = bcrypt.hashSync(password, salt);
knex.transaction(trx => {
trx.insert({
hash: hash,
email: email
})
.into('login')
.returning('email')
.then(loginEmail => {
return trx('users')
.returning('*')
.insert({
email: email,
name: name,
joined: new Date()
})
.then(user => {
res.json(user[0]);
})
})
.then(trx.commit)
.catch(trx.rollback)
})
.catch(err => res.status(400).json('Unable to register'));
}
Thanks in advance!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
