Skip to content

Commit 1d90d5c

Browse files
committed
finished api example
1 parent f933781 commit 1d90d5c

7 files changed

Lines changed: 799 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
.ENV

controller/auth_controller.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const bcrypt = require('bcryptjs');
2+
3+
const register = async (req, res) => {
4+
// get db instance
5+
const db = req.app.get('db');
6+
// get email and password from body
7+
const {email, password} = req.body;
8+
// find an existing email
9+
const foundUser = await db.get_user([email]);
10+
// check to see if user is found
11+
if(foundUser[0]) return res.status(409).send('Sorry, email already exists.');
12+
// If user is not found, create a new hash and salt
13+
const salt = bcrypt.genSaltSync(15);
14+
const hash = bcrypt.hashSync(password, salt);
15+
// Add user to database
16+
const newUser = await db.register_user([email, hash]);
17+
// Add user to the session
18+
req.session.user = newUser[0];
19+
// Send user back
20+
res.status(200).send(req.session.user);
21+
};
22+
23+
const login = async (req, res) => {
24+
// get db instance
25+
const db = req.app.get('db');
26+
// get email and password from body
27+
const {email, password} = req.body;
28+
// find an existing email
29+
const foundUser = await db.get_user([email]);
30+
// check to see if user is found
31+
if(!foundUser[0]) return res.status(409).send('Sorry, email already exists.');
32+
// else use will be found so compare password to the hashed password stored in db
33+
const authenticated = bcrypt.compareSync(password, foundUser[0].password);
34+
// check to see if authenticated is true or false
35+
if(authenticated){
36+
// remove user password
37+
delete foundUser[0].password;
38+
// if authed set user to session and make a response
39+
req.session.user = foundUser[0];
40+
// send response
41+
res.status(200).send(req.session.user);
42+
} else {
43+
// if failure send error message
44+
return res.status(401).send('Inccorect username or password');
45+
};
46+
};
47+
48+
module.exports = {
49+
register,
50+
login
51+
};

db/find_user.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SELECT *
2+
FROM users
3+
WHERE email = $1

db/register_user.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
INSERT INTO users
2+
(email, password)
3+
VALUES
4+
($1, $2)
5+
RETURNING id, email;

db/seeds/users.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE users (
2+
id SERIAL PRIMARY KEY,
3+
email TEXT,
4+
password TEXT
5+
);

index.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
require('dotenv').config();
2+
const express = require('express');
3+
const cors = require('cors');
4+
const massive = require('massive');
5+
const session = require('express-session');
6+
7+
// Controllers
8+
const authCtrl = require('./controller/auth_controller');
9+
10+
// ENV Variable
11+
const {
12+
SERVER_PORT,
13+
CONNECTION_STRING,
14+
SESSION_SECRET
15+
} = process.env;
16+
17+
// App Instance
18+
const app = express();
19+
20+
// Database Connection
21+
massive(CONNECTION_STRING)
22+
.then(dbInstance => {
23+
app.set('db', dbInstance);
24+
console.log('Database is running!')
25+
})
26+
.catch(error => {
27+
console.log('Databse connection failed!')
28+
});
29+
30+
// TLM
31+
app.use(express.json());
32+
app.use(cors());
33+
app.use(session({
34+
resave: false,
35+
saveUninitialized: true,
36+
secret: SESSION_SECRET,
37+
cookie: {
38+
maxAge: 60000
39+
}
40+
}));
41+
42+
// End Points
43+
app.post('/auth/register', authCtrl.register);
44+
app.post('/auth/login', authCtrl.login);
45+
46+
47+
// App Listening
48+
app.listen(SERVER_PORT, () => console.log('Server running!'))

0 commit comments

Comments
 (0)