Skip to content

Commit

Permalink
login and register endpoints functional and tested
Browse files Browse the repository at this point in the history
  • Loading branch information
travisallen6 committed Oct 29, 2018
1 parent 4e62829 commit 72d0058
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 11 deletions.
8 changes: 6 additions & 2 deletions controllers/authController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const bcrypt = require('bcryptjs');
module.exports = {
login: async (req, res) => {
const { username, password } = req.body;
const [user] = await req.db.get_user([username]);
const [user] = await req.app.get('db').get_user([username]);
if (!user) {
return res.status(401).send('User not found');
}
Expand All @@ -19,9 +19,13 @@ module.exports = {

register: async (req, res) => {
const { username, password, name: fullName } = req.body;
const [existingUser] = await req.app.get('db').check_existing_user([username]);
if (existingUser) {
return res.status(409).send('Username taken');
}
const salt = bcrypt.genSaltSync(10);
const hash = bcrypt.hashSync(password, salt);
const [{ user_id: id, name }] = await req.db.register_user([username, hash, fullName]);
const [{ id, name }] = await req.app.get('db').register_user([fullName, username, hash]);
return res.status(201).send({ id, name });
},
};
2 changes: 2 additions & 0 deletions db/check_existing_user.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
select * from users
where username = $1;
2 changes: 1 addition & 1 deletion db/get_user.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
SELECT * FROM user
SELECT * FROM users
WHERE username = $1;
2 changes: 1 addition & 1 deletion db/queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ CREATE TABLE users
(
id SERIAL PRIMARY KEY,
name VARCHAR(50),
username VARCHAR(20),
username VARCHAR(120),
hash text
);

Expand Down
7 changes: 4 additions & 3 deletions db/register_user.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
INSERT INTO user
(name, username, password)
INSERT INTO users
(name, username, hash)
VALUES
($1, $2, $3);
($1, $2, $3)
returning *;
10 changes: 10 additions & 0 deletions nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"watch": [
"server/",
"db/"
],
"env": {
"NODE_ENV": "development"
},
"ext": "js json sql"
}
10 changes: 6 additions & 4 deletions index.js → server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ require('dotenv').config();
const express = require('express');
const session = require('express-session');
const massive = require('massive');
const ac = require('./controllers/authController');
const ac = require('../controllers/authController');

const app = express();
const { SESSION_SECRET, CONNECION_STRING } = process.env;
const { SESSION_SECRET, CONNECTION_STRING } = process.env;

app.use(
session({
Expand All @@ -15,11 +15,13 @@ app.use(
})
);

massive(CONNECION_STRING).then(db => {
massive(CONNECTION_STRING).then(db => {
app.set('db', db);
console.log('DB connected');
db.queries().then(() => console.log('DB connected'));
});

app.use(express.json());

app.post('/auth/register', ac.register);
app.post('/auth/login', ac.login);

Expand Down

0 comments on commit 72d0058

Please sign in to comment.