Skip to content

Commit

Permalink
write code to display login username instead in sign in and sign out
Browse files Browse the repository at this point in the history
Relates #36
  • Loading branch information
asem1789 committed Aug 28, 2019
1 parent a36046a commit ad035af
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 7 deletions.
9 changes: 8 additions & 1 deletion src/controllers/home.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
const jwt = require('jsonwebtoken');
require('env2')('config.env');

exports.home = (req, res, next) => {
res.render('home');
jwt.verify(req.cookies.login, process.env.SECRET, (err, rest) => {
if (err) {
res.render('home', { cook: false });
} else { res.render('home', { cook: rest.username }); }
});
};
10 changes: 9 additions & 1 deletion src/controllers/profile.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
const jwt = require('jsonwebtoken');
require('env2')('config.env');


exports.profile = (req, res, next) => {
res.render('profile');
jwt.verify(req.cookies.login, process.env.SECRET, (err, rest) => {
if (err) {
res.redirect('/login');
} else { res.render('profile', { cook: rest.username }); }
});
};
2 changes: 1 addition & 1 deletion src/controllers/signout.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
exports.signout = (req, res, next) => {
res.clearCookie('login');
res.redirect('/login');
res.redirect('/');
};
10 changes: 9 additions & 1 deletion src/views/partials/header.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@
<ul>
<li><a href="/" target="blank">HOME</a></li>
<li><a href="/profile" target="blank">PROFILE</a></li>
{{#if cook}}
<li><a href="/profile" target="blank">wellcome to <span>{{cook}}</span></a></li>
<li><a href="/signout" target="blank">SING OUT</a></li>

{{else}}
<li><a href="/login" target="blank">LOGIN</a></li>
<li><a href="/signup" target="blank">SIGN UP</a></li>
</ul>
{{/if}}


</ul>
</nav>
</header>
6 changes: 3 additions & 3 deletions src/views/profile.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
<main>
<section class="form-section">
<form action="/addPost" method="post">
<textarea name="post-text" id="text" class="post-text" cols="30" rows="10" placeholder="Write your post here"></textarea>
<textarea name="post-text" id="text" class="post-text" cols="30" rows="10"
placeholder="Write your post here"></textarea>
<label for="post-category">Category</label>
<input name="post-category" type="text">
<input type="text" name="post-img" id="post-image" placeholder="Add image URL here">
<button class="add-post-btn" type="submit">Add Post</button>
</form>
</section>

<section class="post-section">
{{#each postsArray}}
<div class="post-content">
Expand All @@ -22,4 +22,4 @@
</div>
{{/each}}
</section>
</main>
</main>

0 comments on commit ad035af

Please sign in to comment.