Skip to content

Commit 7655554

Browse files
committed
change pg-promise queries
1 parent 0bf47d6 commit 7655554

10 files changed

Lines changed: 548 additions & 49 deletions

File tree

api/user-api.js

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1+
const { as } = require("pg-promise");
2+
13
module.exports = function (userService) {
24

3-
async function user(req, res) {
4-
try {
5+
async function user(req, res) {
6+
try {
57
let id = req.body.id;
6-
let users = await userService.getUserName();
7-
res.json({
8-
status: 'success',
9-
data: users
10-
});
11-
}
12-
catch (err) {
13-
res.json({
14-
status: "error",
15-
error: err.stack
16-
});
17-
}
18-
};
8+
let users = await userService.getUserName();
9+
res.json({
10+
status: 'success',
11+
data: users
12+
});
13+
}
14+
catch (err) {
15+
res.json({
16+
status: "error",
17+
error: err.stack
18+
});
19+
}
20+
};
1921

2022
const validatePassword = async (req, res) => {
2123
try {
@@ -38,13 +40,23 @@ module.exports = function (userService) {
3840
error: err.stack
3941
})
4042
}
41-
}
43+
}
4244

4345
const signUp = async (req, res) => {
4446
try {
4547
let username = req.body.username;
4648
let password = req.body.password;
47-
49+
let storeData = await userService.signUp(username, password);
50+
if (storeData == true) {
51+
res.json({
52+
status: 'success',
53+
data: "user sign-up," + username
54+
});
55+
}
56+
res.json({
57+
status: "error",
58+
data: "invalid username or password fomat"
59+
})
4860
/*
4961
- set-up salt
5062
- hash the password
@@ -64,10 +76,28 @@ module.exports = function (userService) {
6476
}
6577
}
6678

79+
const login = async (req, res) => {
80+
try {
81+
let username = req.body.username;
82+
let password = req.body.password;
83+
let user = await userService.login(username, password);
84+
res.json({
85+
status: "sucess",
86+
data: user
87+
})
88+
} catch (error) {
89+
res.json({
90+
status: "error",
91+
error: error.stack
92+
})
93+
}
94+
}
95+
6796
return {
6897
user,
6998
validatePassword,
70-
signUp
99+
signUp,
100+
login
71101
}
72-
102+
73103
}

index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const Products = require('./routes/products');
99
const ProductsAPI = require('./api/products-api');
1010
const UserAPI = require('./api/user-api');
1111

12-
1312
const app = express();
1413
const session = require('express-session');
1514
const flash = require('express-flash');
@@ -26,7 +25,7 @@ if (process.env.DATABASE_URL && !local){
2625
}
2726
const connectionString = process.env.DATABASE_URL || 'postgresql://localhost:5432/my_products_list';
2827

29-
const db = pgp(connectionString);
28+
const db = pgp(connectionString);
3029

3130
const categoryService = CategoryService(db);
3231
const productService = ProductService(db);
@@ -89,11 +88,12 @@ app.post('/api/products', productsAPI.add);
8988
app.get('/api/categories', categoryAPI.all);
9089

9190
app.get('/api/users', userAPI.user);
92-
// add user/sign-up end-point
93-
91+
app.post('/api/signUp', userAPI.signUp);
92+
app.post('/api/login', userAPI.login);
9493

9594
app.use(errorHandler);
9695

96+
9797
//configure the port number using and environment number
9898
var portNumber = process.env.PORT || 3000;
9999

model/user.model.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const bcrypt = require('bcrypt');
2+
3+
module.exports = function userModel() {
4+
5+
const usernameIsValid = username => {
6+
let valid = /^[0-9a-zA-Z_.-]+$/.test(username)
7+
return valid ? username : "invalid format"
8+
};
9+
10+
const cryptPassword = async password => {
11+
const salt = await bcrypt.genSalt(10);
12+
const hashPassword = await bcrypt.hashSync(password, salt);
13+
return hashPassword;
14+
}
15+
16+
const comparePassword = async (password, passwordHash) => {
17+
// console.log(passwordHash + " " + " hash");
18+
const match = await bcrypt.compare(password, passwordHash);
19+
return match ? true : false;
20+
}
21+
22+
return {
23+
usernameIsValid,
24+
cryptPassword,
25+
comparePassword
26+
}
27+
28+
}
29+
30+
// export default [usernameIsValid, cryptPassword]
31+
32+

0 commit comments

Comments
 (0)