Skip to content
This repository was archived by the owner on Apr 12, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/app/navbar/navbar.component.css
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
margin-right: 40px;
}


@media all and (max-width: 600px) {
* {
font-size: 80%;
Expand Down
1 change: 1 addition & 0 deletions src/app/navbar/navbar.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<ul class="navbar-nav ml-auto">
<li><a class="nav-link" href="#">Home</a></li>
<li><a class="nav-link" href="#">Sign Up</a></li>
<li><a *ngIf="isLoggedin()" class="nav-link" href="/" (click)="logout()">Logout</a></li>
<li><a class="nav-link" href="#"> <i class="fa fa-user"></i>&nbsp;&nbsp;<i class="fa fa-ellipsis-v"></i></a></li>
</ul>
</div>
Expand Down
24 changes: 23 additions & 1 deletion src/app/navbar/navbar.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';

@Component({
selector: 'app-navbar',
Expand All @@ -7,8 +8,29 @@ import { Component, OnInit } from '@angular/core';
})
export class NavbarComponent implements OnInit {

constructor() { }
constructor(private router: Router) { }

// Check if User is Logged In
isLoggedin(){

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! Thanks for going with the suggested approach, you can further simplify this in future like

isLoggedIn() {
  return localStorage.getItem('token') !== null
}

But this works totally fine.

const token_exists = localStorage.getItem('token');
if(token_exists){
return true;
}
else{
return false;
}
}

logout(){

// Remove Token and expiry date
localStorage.removeItem('token');
localStorage.removeItem('expires_at');

// Redirect to home/login page
this.router.navigateByUrl('/');

}
ngOnInit() {
}

Expand Down