Skip to content

Commit

Permalink
final adding
Browse files Browse the repository at this point in the history
  • Loading branch information
AsalaKM committed Aug 15, 2018
2 parents b03631f + 54761de commit f919c97
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 43 deletions.
11 changes: 6 additions & 5 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ <h3 class="post-title">title</h3>
</div>
<div class="addSection">
<h3>Add New Post</h3>
<form action="POST" class="addpost">
<form action="/addpost" method="POST" class="addpost">
<label class="addlable" for="title">Title: </label>
<input class="addinput" placeholder="post's title" id="title" type="text" name="title" class="form-input">
<input class="addinput" placeholder="post's title" id="title" type="text" name="post_title" class="form-input">
<label class="addlable" for="body">Body: </label>
<textarea ass="addinput" placeholder="post's body" id="body" type="body" name="body" class="form-input"></textarea>
<textarea class="addinput" name="post_body" placeholder="post's body" id="body" type="body" name="body" class="form-input"></textarea>
<label>.</label>
<button id="add" class="addbtn form-input">add post</button>
</form>

</div>
</section>
</main>
Expand All @@ -63,7 +64,7 @@ <h3>Add New Post</h3>
copyright &copy; 2018 | Othman, Ali, Salwa & Asala
</address>
</footer>
<script src="/public/js/dom.js"></script>
<script src="/public/js/fetch.js"></script>
<script src="public/js/fetch.js"></script>
<script src="public/js/dom.js"></script>
</body>
</html>
63 changes: 40 additions & 23 deletions public/js/dom.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,69 @@
fetch(null, 'get', '/', view);

fetch(null,'GET','/checkauth',(err, obj) => {
if(err){
console.log(err);
}
console.log(typeof obj);
if(obj){
console.log(obj);
}else{
console.log(obj);
}
});

fetch(null, 'get', '/getposts', view);
function view(err, obj) {
console.log(obj);
const object = JSON.parse(obj);
console.log(object);
console.log('gdfg')
if (object.err) {
console.log('error');
}

const arr = object.data;
console.log('arr.length', arr);
const content = document.getElementsByClassName('posts')[0];
// CREATE ELEMENTS
arr.forEach((element) => {
const post = document.createElement('div');
object.forEach((element) => {
let post = document.createElement('div');
post.classList = 'post';

const postContent = document.createElement('div');
let postContent = document.createElement('div');
console.log(postContent)
postContent.classList = 'post-content';

const postTitle = document.createElement('h3');
let postTitle = document.createElement('h3');
postTitle.classList = 'post-title';
postTitle.textContent = element.post_title;

const postBody = document.createElement('p');
let postBody = document.createElement('p');
postBody.classList = 'post-body';
postBody.textContent = element.post_body;

const postDetails = document.createElement('div');
let postDetails = document.createElement('div');
postDetails.classList = 'post-details';

const owner = document.createElement('span');
let owner = document.createElement('span');
owner.classList = 'owner';
owner.textContent = element.posted_by;

const date = document.createElement('span');
let date = document.createElement('span');
date.classList = 'date';
date.textContent = element.date;

const time = document.createElement('span');
let time = document.createElement('span');
time.classList = 'time';
time.textContent = element.time;
let line = document.createElement('hr');

postContent.appendChild(postTitle);
postContent.appendChild(postBody);
postDetails.appendChild(date);
postDetails.appendChild(time);
// postDetails.appendChild(remove);
post.appendChild(postContent);
post.appendChild(postDetails);
content.appendChild(post);
content.appendChild(line);


// const remove = document.createElement('span');
// remove.classList = 'fas fa-trash';
Expand All @@ -48,15 +73,7 @@ function view(err, obj) {
// window.location = '/';
// });
});
const line = document.createElement('hr');

// APPEND CHILDS
postContent.appendChild(postTitle);
postContent.appendChild(postBody);
postDetails.appendChild(date);
postDetails.appendChild(time);
postDetails.appendChild(remove);
post.appendChild(postContent);
post.appendChild(postDetails);
content.appendChild(post);
content.appendChild(line);

}
10 changes: 10 additions & 0 deletions src/handlers/checkAuth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { isAuth, error401 } = require('./isAuth.js');

function checkAuth(req, res){
isAuth(req, res,(result)=>{
res.writeHead(200);

res.end(JSON.stringify({result}));
});
}
module.exports = checkAuth;
36 changes: 26 additions & 10 deletions src/handlers/handleAddPost.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ const querystring = require('querystring');
const { addPost } = require('./../queries/addData.js');
const { handlePageNotFound } = require('./homePage.js');

console.log(addPost);
const { parse } = require('cookie');
const { verify } = require('jsonwebtoken');

console.log(addPost);
const SECRET = process.env.SECRET;

function handleAddPost(req, res) {
let data = '';
Expand All @@ -13,16 +16,29 @@ function handleAddPost(req, res) {
});
req.on('end', () => {
const user = querystring.parse(data);
console.log(user);
addPost(user, (err, result) => {
res.writeHead(302, {
location: '/',
const { jwt } = parse(req.headers.cookie);
console.log(jwt);
verify(jwt, SECRET, (error, obj) => {
if (error) {
console.log(error);
}
console.log(obj);
user['user_id'] = obj['user_id'];
console.log(user);
addPost(user, (err, result) => {
if (err) {
console.log(err);
}
console.log(result);
res.writeHead(302, {
location: '/',
});
const obj = {
err,
result,
};
res.end(JSON.stringify(obj));
});
const obj = {
err,
result,
};
res.end(JSON.stringify(obj));
});
});
}
Expand Down
2 changes: 2 additions & 0 deletions src/handlers/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ const logout = require('./logout.js');
const handleQuery = require('./handleQuery.js');
const { isAuth, error401 } = require('./isAuth.js');
const { login } = require('./login.js');
const checkAuth = require('./checkAuth.js')
const signup = require('./signup.js');

module.exports = {
checkAuth,
handleQuery,
handleDeletePost,
handleHomePage,
Expand Down
9 changes: 5 additions & 4 deletions src/handlers/isAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const { parse } = require('cookie');
const { verify } = require('jsonwebtoken');

const SECRET = 'poiugyfguhijokpkoihugyfyguhijo';
const SECRET = process.env.SECRET;

const error401 = (req, res) => {
const message = '<h1>UNAUTHORIZED ERROR</h1>';
Expand All @@ -11,10 +11,11 @@ const error401 = (req, res) => {
};

const isAuth = (req, res, cb) => {
if (!req.header.cookie) return error401(req, res);
if (!req.headers) return cb(false);
if (!req.headers.cookie) return cb(false);
const { jwt } = parse(req.headers.cookie);
if (!jwt) return error401(req, res);

if (!jwt) return cb(false);
// so this is not ok return error401(req, res);
verify(jwt, SECRET, (err, jwt) => {
if (err) cb(false);
else {
Expand Down
3 changes: 3 additions & 0 deletions src/handlers/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ const login = (req, res) => {
if (err) {
console.log(err, 'user not exist or there is an error');
} else if (isAuh) {
console.log('is auth',isAuh);
const user = {
email: enteredData.email,
rule: enteredData.rule,
user_id: result[0].user_id,
};
console.log('user', user);
// Create Token
const loginJWT = sign(user, process.env.SECRET);

res.setHeader('Set-Cookie', `jwt=${loginJWT};`);
res.writeHead(302, {
Location: '/',
Expand Down
2 changes: 1 addition & 1 deletion src/router.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const {
handleDeletePost, handleStaticFiles, handleQuery, handleAddPost, handleHomePage,
handleDeletePost, handleStaticFiles, handleQuery, handleAddPost, handleHomePage,isAuth,
handlePageNotFound, signup, login, signupPage, loginPage, logout, checkAuth,
} = require('./handlers/handler.js');

Expand Down

0 comments on commit f919c97

Please sign in to comment.