Login and logout menu display madolities
This commit is contained in:
@@ -4,8 +4,6 @@
|
|||||||
<div class="container-fluid">
|
<div class="container-fluid">
|
||||||
<div class="row flex-nowrap">
|
<div class="row flex-nowrap">
|
||||||
<div class="col-auto col-md-3 col-xl-2 px-sm-2 px-0 bg-dark" style="max-width: 200px;">
|
<div class="col-auto col-md-3 col-xl-2 px-sm-2 px-0 bg-dark" style="max-width: 200px;">
|
||||||
<login></login>
|
|
||||||
<logout></logout>
|
|
||||||
<sidenav class="sticky-top"></sidenav>
|
<sidenav class="sticky-top"></sidenav>
|
||||||
</div>
|
</div>
|
||||||
<div class="col py-3">
|
<div class="col py-3">
|
||||||
|
|||||||
@@ -7,7 +7,10 @@ import {NgbModal} from "@ng-bootstrap/ng-bootstrap";
|
|||||||
@Component({
|
@Component({
|
||||||
selector: 'login',
|
selector: 'login',
|
||||||
template: `
|
template: `
|
||||||
<button class="btn btn-lg btn-outline-primary" (click)="openLoginModal()">Login</button>
|
<button *ngIf="!isAuthenticated"
|
||||||
|
class="nav-link px-3 w-100"
|
||||||
|
(click)="openLoginModal()"
|
||||||
|
><i-bs name="key-fill"/><span class="ms-1 d-none d-sm-inline">Login</span></button>
|
||||||
<ng-template #loginModal let-modal>
|
<ng-template #loginModal let-modal>
|
||||||
<div class="modal-header">
|
<div class="modal-header">
|
||||||
<h4 class="modal-title" id="modal-basic-title">Login</h4>
|
<h4 class="modal-title" id="modal-basic-title">Login</h4>
|
||||||
@@ -20,19 +23,25 @@ import {NgbModal} from "@ng-bootstrap/ng-bootstrap";
|
|||||||
<legend>Login</legend>
|
<legend>Login</legend>
|
||||||
<div class="form-field">
|
<div class="form-field">
|
||||||
<label>Username:</label>
|
<label>Username:</label>
|
||||||
<input name="username" formControlName="username">
|
<input class="form-control" name="username" formControlName="username">
|
||||||
</div>
|
</div>
|
||||||
<div class="form-field">
|
<div class="form-field">
|
||||||
<label>Password:</label>
|
<label>Password:</label>
|
||||||
<input name="password" formControlName="password"
|
<input class="form-control" name="password" formControlName="password"
|
||||||
type="password">
|
type="password">
|
||||||
</div>
|
</div>
|
||||||
</fieldset>
|
</fieldset>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
<div class="modal-footer">
|
||||||
<div class="form-buttons">
|
<div class="form-buttons">
|
||||||
<button class="button button-primary"
|
<button class="btn btn-primary"
|
||||||
(click)="this.login()">Login</button>
|
(click)="this.login()">Login</button>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
<div class="form-buttons">
|
||||||
|
<button class="btn btn-danger"
|
||||||
|
(click)="modal.dismiss('Cancel click')">Cancel</button>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</ng-template>
|
</ng-template>
|
||||||
`
|
`
|
||||||
@@ -45,6 +54,8 @@ export class LoginComponent {
|
|||||||
|
|
||||||
form: FormGroup;
|
form: FormGroup;
|
||||||
|
|
||||||
|
isAuthenticated: boolean = false
|
||||||
|
|
||||||
constructor(private fb:FormBuilder,
|
constructor(private fb:FormBuilder,
|
||||||
private authService: AuthService,
|
private authService: AuthService,
|
||||||
private modalService: NgbModal
|
private modalService: NgbModal
|
||||||
@@ -57,9 +68,13 @@ export class LoginComponent {
|
|||||||
|
|
||||||
this.authService.onAuthenticationRequired$.subscribe((required: boolean) => {
|
this.authService.onAuthenticationRequired$.subscribe((required: boolean) => {
|
||||||
if (required) {
|
if (required) {
|
||||||
this.openLoginModal()
|
this.openLoginModal();
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
this.isAuthenticated = this.authService.is_authenticated;
|
||||||
|
this.authService.onIsAuthenticated$.subscribe((isAuthenticated: boolean) => {
|
||||||
|
this.isAuthenticated = isAuthenticated;
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
openLoginModal() {
|
openLoginModal() {
|
||||||
@@ -84,13 +99,21 @@ export class LoginComponent {
|
|||||||
@Component({
|
@Component({
|
||||||
selector: 'logout',
|
selector: 'logout',
|
||||||
template: `
|
template: `
|
||||||
<button class="btn btn-lg btn-outline-primary" (click)="logout()">Logout</button>
|
<button *ngIf="isAuthenticated"
|
||||||
|
class="nav-link px-3 w-100"
|
||||||
|
(click)="logout()"
|
||||||
|
><i-bs name="door-open-fill"/><span class="ms-1 d-none d-sm-inline">Logout</span></button>
|
||||||
`
|
`
|
||||||
})
|
})
|
||||||
export class LogoutComponent {
|
export class LogoutComponent {
|
||||||
@Output() logoutSuccess = new EventEmitter()
|
@Output() logoutSuccess = new EventEmitter()
|
||||||
|
isAuthenticated: boolean = false
|
||||||
|
|
||||||
constructor(private authService: AuthService) {
|
constructor(private authService: AuthService) {
|
||||||
|
this.isAuthenticated = this.authService.is_authenticated;
|
||||||
|
this.authService.onIsAuthenticated$.subscribe((isAuthenticated: boolean) => {
|
||||||
|
this.isAuthenticated = isAuthenticated
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
logout() {
|
logout() {
|
||||||
|
|||||||
@@ -18,9 +18,15 @@ export class AuthService {
|
|||||||
private authenticationRequired = new Subject<boolean>();
|
private authenticationRequired = new Subject<boolean>();
|
||||||
onAuthenticationRequired$ = this.authenticationRequired.asObservable();
|
onAuthenticationRequired$ = this.authenticationRequired.asObservable();
|
||||||
|
|
||||||
|
is_authenticated = false;
|
||||||
|
private isAuthenticated = new Subject<boolean>();
|
||||||
|
onIsAuthenticated$ = this.isAuthenticated.asObservable();
|
||||||
|
|
||||||
constructor(private http: HttpClient,
|
constructor(private http: HttpClient,
|
||||||
private flashMessage: FlashmessagesService) {
|
private flashMessage: FlashmessagesService) {
|
||||||
this.access_token = localStorage.getItem('authtoken')
|
this.access_token = localStorage.getItem('authtoken')
|
||||||
|
this.is_authenticated = Boolean(this.access_token)
|
||||||
|
this.isAuthenticated.next(this.is_authenticated)
|
||||||
}
|
}
|
||||||
|
|
||||||
login(username:string, password:string ) {
|
login(username:string, password:string ) {
|
||||||
@@ -36,6 +42,8 @@ export class AuthService {
|
|||||||
localStorage.setItem('authtoken', v.access_token);
|
localStorage.setItem('authtoken', v.access_token);
|
||||||
this.access_token = v.access_token
|
this.access_token = v.access_token
|
||||||
this.flashMessage.success('Login successful. Welcome ' + username);
|
this.flashMessage.success('Login successful. Welcome ' + username);
|
||||||
|
this.is_authenticated = true;
|
||||||
|
this.isAuthenticated.next(this.is_authenticated)
|
||||||
return username
|
return username
|
||||||
} ));
|
} ));
|
||||||
}
|
}
|
||||||
@@ -48,6 +56,8 @@ export class AuthService {
|
|||||||
localStorage.removeItem('authtoken');
|
localStorage.removeItem('authtoken');
|
||||||
this.access_token = null;
|
this.access_token = null;
|
||||||
this.flashMessage.success('Logout successful. Goodbye');
|
this.flashMessage.success('Logout successful. Goodbye');
|
||||||
|
this.is_authenticated = false;
|
||||||
|
this.isAuthenticated.next(this.is_authenticated)
|
||||||
} ));
|
} ));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,5 +69,7 @@ export class AuthService {
|
|||||||
localStorage.removeItem('authtoken');
|
localStorage.removeItem('authtoken');
|
||||||
this.access_token = null;
|
this.access_token = null;
|
||||||
this.authenticationRequired.next(true);
|
this.authenticationRequired.next(true);
|
||||||
|
this.is_authenticated = false;
|
||||||
|
this.isAuthenticated.next(this.is_authenticated)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,10 +3,14 @@
|
|||||||
<img class="fs-5 d-none d-sm-inline w-100" src="/assets/logo.png" alt="Cooper, Hillman & Toshi LLP">
|
<img class="fs-5 d-none d-sm-inline w-100" src="/assets/logo.png" alt="Cooper, Hillman & Toshi LLP">
|
||||||
</a>
|
</a>
|
||||||
<ul class="nav nav-pills flex-column align-items-sm-start mb-sm-auto mb-0 w-100" id="menu">
|
<ul class="nav nav-pills flex-column align-items-sm-start mb-sm-auto mb-0 w-100" id="menu">
|
||||||
<li *ngFor="let item of Menu" class="nav-item w-100">
|
<li><login></login></li>
|
||||||
<a class="nav-link px-3 w-100" routerLink="{{item.link}}" [class.active]="is_current_page(item)">
|
<ng-container *ngIf="isAuthenticated">
|
||||||
<i-bs [name]="item.icon"></i-bs><span class="ms-1 d-none d-sm-inline" [innerHTML]="item.title"></span>
|
<li *ngFor="let item of Menu" class="nav-item w-100">
|
||||||
</a>
|
<a class="nav-link px-3 w-100" routerLink="{{item.link}}" [class.active]="is_current_page(item)">
|
||||||
</li>
|
<i-bs [name]="item.icon"></i-bs><span class="ms-1 d-none d-sm-inline" [innerHTML]="item.title"></span>
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
</ng-container>
|
||||||
|
<li><logout></logout></li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { Component } from "@angular/core";
|
import { Component } from "@angular/core";
|
||||||
import { Router } from '@angular/router';
|
import { Router } from '@angular/router';
|
||||||
import { IconNamesEnum } from "ngx-bootstrap-icons";
|
import { IconNamesEnum } from "ngx-bootstrap-icons";
|
||||||
|
import { AuthService } from "../auth/auth.service";
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: "sidenav",
|
selector: "sidenav",
|
||||||
@@ -41,7 +42,17 @@ export class SidenavComponent {
|
|||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
constructor(private router: Router) {}
|
isAuthenticated: boolean = false
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
private router: Router,
|
||||||
|
private authService: AuthService,) {
|
||||||
|
|
||||||
|
this.isAuthenticated = this.authService.is_authenticated;
|
||||||
|
this.authService.onIsAuthenticated$.subscribe((isAuthenticated: boolean) => {
|
||||||
|
this.isAuthenticated = isAuthenticated;
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
is_current_page(menu_item: any) {
|
is_current_page(menu_item: any) {
|
||||||
return this.router.url.indexOf(menu_item.link) > -1;
|
return this.router.url.indexOf(menu_item.link) > -1;
|
||||||
|
|||||||
Reference in New Issue
Block a user