Skip to content

Commit 485b4a7

Browse files
authored
Merge pull request #21 from FACG2/beta3
Beta3
2 parents e88e7f3 + e844a30 commit 485b4a7

5 files changed

Lines changed: 104 additions & 57 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Make an APP for add blog post
4848

4949

5050
## Team :sunglasses: :
51-
* [Aia](http://github.com/)
52-
* [Salwa](http://github.com/)
53-
* [Samir](http://github.com/)
51+
* [Aia](http://github.com/EngAyoosh)
52+
* [Salwa](http://github.com/SalwaAlnazly)
53+
* [Samer](http://github.com/SamerElaila)
5454
* [Mahmoud](http://github.com/mrm7moud)

public/js/sign_up.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// var name = document.querySelector('.username');
2+
var email = document.querySelector('.email');
3+
var password = document.querySelector('.password');
4+
var confirmPassword = document.querySelector('.confirm-password');
5+
// var form = document.querySelector('.form');
6+
var message = document.querySelector('.message');
7+
8+
var obj = {
9+
name: true,
10+
email: false,
11+
password: true,
12+
confirmPassword: false
13+
};
14+
15+
email.addEventListener('focusout', function () {
16+
if (email.value.match(/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/)) {//eslint-disable-line
17+
obj.email = true;
18+
email.style.border = '1px solid #0F0';
19+
message.innerHTML = '';
20+
} else {
21+
message.innerHTML = "<p style='color:red'>Please Enter a valid email address</p>";
22+
email.style.border = '1px solid #F00';
23+
obj.email = false;
24+
}
25+
if (obj.emai && obj.confirmPassword) message.innerHTML = '';
26+
});
27+
28+
confirmPassword.addEventListener('focusout', function () {
29+
if (password.value === confirmPassword.value) {
30+
obj.confirmPassword = true;
31+
password.style.border = '1px solid #0F0';
32+
confirmPassword.style.border = '1px solid #0F0';
33+
// message.innerHTML = '';
34+
} else {
35+
message.innerHTML = "<p style='color:red'>Password Doesn't match</p>";
36+
password.style.border = '1px solid #F00';
37+
confirmPassword.style.border = '1px solid #F00';
38+
obj.confirmPassword = false;
39+
}
40+
if (obj.name && obj.confirmPassword) message.innerHTML = '';
41+
});
42+
43+
// form.addEventListener('submit', function (event) {
44+
// event.preventDefault();
45+
// request('/sign-up', 'POST' , data , function (err, res) {//eslint-disable-line
46+
// if (err) {
47+
// console.log(err);
48+
// } else {
49+
// console.log(res);
50+
// }
51+
// });
52+
// });

public/sign_up.html

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@ <h1>SIGNUP</h1>
2020

2121
<section>
2222
<form class="form" action="/sign-up" method="POST">
23-
<input type="text" name="name" value="" placeholder="name ...." required>
24-
<input type="text" name="email" value="" placeholder="email ...." required>
25-
<input type="password" name="password" value="" placeholder="Password ...." required>
26-
<input type="password" name="confirm_password" value="" placeholder="Renter Password ...." required>
23+
<input type="text" class='username' name="name" value="" placeholder="name ...." required>
24+
<input type="text" class='email' name="email" value="" placeholder="email ...." required>
25+
<input type="password" class=password name="password" value="" placeholder="Password ...." required>
26+
<input type="password" class='confirm-password' name="confirm_password" value="" placeholder="Renter Password ...." required>
27+
<div class='message'></div>
2728
<button type="submit" name="button">signup</button>
2829
</form>
2930
</section>
30-
31+
<script src='./js/request.js'></script>
3132
<script type="text/javascript" src="./js/sign_up.js"></script>
3233
</body>
3334
</html>

src/handlers/get.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ function handleHome (req, res) {
3232
function readHome (req, res) {
3333
fs.readFile(path.join(__dirname, '/../../public/index.html'), (err, data) => {
3434
if (err) {
35-
res.writeHead(500, {'Content-Type': 'text/html'});
36-
res.end('Internal Server Error');
35+
res.writeHead(302, {'Location': '/404'});
36+
res.end();
3737
} else {
3838
res.writeHead(200, {'Content-Type': 'text/html'});
3939
res.end(data);
@@ -42,10 +42,6 @@ function readHome (req, res) {
4242
}
4343

4444
function handleLogin (req, res) {
45-
// const data = {
46-
// name: 'test',
47-
// password: '123'
48-
// };
4945
let content = '';
5046
req.on('data', (chunk) => {
5147
content += chunk;
@@ -116,8 +112,8 @@ function handleBlogs (req, res) {
116112
function readBlogs (req, res) {
117113
fs.readFile(path.join(__dirname, '/../../public/home.html'), (err, data) => {
118114
if (err) {
119-
res.writeHead(500, {'Content-Type': 'text/html'});
120-
res.end('Server Error');
115+
res.writeHead(302, {'Location': '/404'});
116+
res.end();
121117
} else {
122118
res.writeHead(200, {'Content-Type': 'text/html'});
123119
res.end(data);
@@ -126,7 +122,7 @@ function readBlogs (req, res) {
126122
}
127123

128124
function handleUserBlog (req, res) {
129-
125+
// Not Implemented Yet
130126
}
131127

132128
function handleLogout (req, res) {
@@ -151,7 +147,7 @@ function handleGeneric (req, res) {
151147
function handleSignup (req, res) {
152148
fs.readFile(path.join(__dirname, '/../../public/sign_up.html'), (err, data) => {
153149
if (err) {
154-
res.writeHead(302, {'Location': '/'});
150+
res.writeHead(302, {'Location': '/404'});
155151
res.end();
156152
} else {
157153
res.writeHead(200, {'Content-Type': 'text/html'});
@@ -180,7 +176,7 @@ function handleNewBlog (req, res) {
180176
function readAddBlog (req, res) {
181177
fs.readFile(path.join(__dirname, '/../../public/add_blog.html'), (err, data) => {
182178
if (err) {
183-
res.writeHead(302, {'Location': '/'});
179+
res.writeHead(302, {'Location': '/404'});
184180
res.end();
185181
} else {
186182
res.writeHead(200, {'Content-Type': 'text/html'});

src/handlers/post.js

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,61 +3,59 @@ const queryString = require('querystring');
33
const jwt = require('jsonwebtoken');
44
const SECRET = 'afdhasjkhdfsadjfhskdjhf';
55
const bcrypt = require('bcryptjs');
6-
// const path = require('path');
7-
// const fs = require('fs');
86
const cookie = require('cookie');
97

108
function handleSignup (req, res) {
11-
/*
12-
insert data to DB
13-
create a new cookie
14-
redirect to home.html
15-
input: username , password , email
16-
*/
179
let content = '';
1810
req.on('data', (chunk) => {
1911
content += chunk;
2012
});
2113
req.on('end', () => {
2214
const data = queryString.parse(content);
23-
// const data = {
24-
// name: 'test',
25-
// password: '123',
26-
// email: 'a@a.a'
27-
// };
2815
if (req.headers.cookie) {
2916
res.writeHead(302, {'location': '/'});
3017
res.end();
3118
}
3219
bcrypt.hash(data.password, 10, (err, hashedPassword) => {
3320
if (err) {
34-
res.end(err);
21+
console.log(err);
22+
res.end();
3523
} else {
36-
// console.log(hashedPassword, 'hashedPassword');
37-
data.password = hashedPassword;
38-
query(`INSERT INTO users(name , email , password) VALUES($1,$2,$3) RETURNING *`, [data.name, data.email, data.password], (err1, record) => {
39-
if (err1) {
40-
res.writeHead(302, {'Location': '/signup'});
41-
res.end();
42-
} else {
43-
// console.log(record);
44-
jwt.sign({name: record[0].name, id: record[0].id}, SECRET, (err2, token) => {
45-
if (err2) {
46-
// console.log(err2);
47-
res.writeHead(302, {'Location': '/'});
48-
res.end();
49-
} else {
50-
res.writeHead(302, {'Set-Cookie': `token=${token}; Max-Age=99999`, 'Location': '/blogs'});
51-
res.end();
52-
}
53-
});
54-
}
55-
});
24+
console.log(isValid(data));
25+
if (isValid(data)) {
26+
data.password = hashedPassword;
27+
query(`INSERT INTO users(name , email , password) VALUES($1,$2,$3) RETURNING *`, [data.name, data.email, data.password], (err1, record) => {
28+
if (err1) {
29+
res.writeHead(302, {'Location': '/signup'});
30+
res.end();
31+
} else {
32+
jwt.sign({name: record[0].name, id: record[0].id}, SECRET, (err2, token) => {
33+
if (err2) {
34+
res.writeHead(302, {'Location': '/'});
35+
res.end();
36+
} else {
37+
res.writeHead(302, {'Set-Cookie': `token=${token}; Max-Age=99999`, 'Location': '/blogs'});
38+
res.end();
39+
}
40+
});
41+
}
42+
});
43+
} else {
44+
// if data is not valid
45+
res.writeHead(302, {'Location': '/signup'});
46+
res.end();
47+
}
5648
}
5749
});
5850
});
5951
}
6052

53+
function isValid (data) {
54+
return (typeof data.name !== 'string' || data.name !== '') &&
55+
(typeof data.email !== 'string' || !data.email.match(/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/)) && //eslint-disable-line
56+
(typeof data.password !== 'string') && (data.confirmPassword !== data.password);
57+
}
58+
6159
function handleAddBlog (req, res) {
6260
let content = '';
6361
req.on('data', (chunk) => {
@@ -74,8 +72,8 @@ function handleAddBlog (req, res) {
7472
} else {
7573
query('INSERT INTO posts(title,contents,post_date,user_id) VALUES ($1,$2,$3,$4) RETURNING * ', [data.title, data.contents, data.post_date, result.id], (error, res1) => {
7674
if (error) {
77-
console.log(error);
78-
res.end('There is error');
75+
res.writeHead(302, {'Location': '/404'});
76+
res.end();
7977
} else {
8078
res.writeHead(302, {'Location': '/blogs'});
8179
res.end();
@@ -91,11 +89,11 @@ function handleAddBlog (req, res) {
9189
}
9290

9391
function handleEditBlog (req, res) {
94-
92+
// Not Implemented Yet
9593
}
9694

9795
function handleDeleteBlog (req, res) {
98-
96+
// Not Implemented Yet
9997
}
10098

10199
module.exports = {

0 commit comments

Comments
 (0)