Tax is added directly to product for sale

Auth guard and auth service simplified and fixed so that user is updated upon login
Home component changed to use square buttons
Fixed showing the totals in the bill

ng linted the project
This commit is contained in:
Amritanshu
2019-08-11 01:37:14 +05:30
parent d7fdf751b9
commit dcaf23b390
23 changed files with 334 additions and 266 deletions

View File

@ -20,7 +20,8 @@ export class AuthGuard implements CanActivate {
this.toaster.show('Danger', 'User is not authenticated');
return false;
}
const hasPermission = permission === undefined || user.perms.indexOf(permission) !== -1;
const hasPermission = this.auth.hasPermission(permission);
if (!hasPermission) {
this.toaster.show('Danger', 'You do not have the permission to access this area.');
}
@ -28,14 +29,11 @@ export class AuthGuard implements CanActivate {
}
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
if (this.auth.user === undefined) {
return this.auth.userObservable
.pipe(
map<User, boolean>((value: User) => this.checkUser(
next.data['permission'], value, state
))
);
}
return this.checkUser(next.data['permission'], this.auth.user, state);
return this.auth.userObservable
.pipe(
map<User, boolean>((value: User) => this.checkUser(
next.data['permission'], value, state
))
);
}
}

View File

@ -1,10 +1,10 @@
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs/internal/Observable';
import { catchError, tap } from 'rxjs/operators';
import { Subject } from 'rxjs/internal/Subject';
import { catchError, map, tap } from 'rxjs/operators';
import { ErrorLoggerService } from '../core/error-logger.service';
import { User } from '../core/user';
import { merge, Subject } from 'rxjs';
const httpOptions = {
@ -19,22 +19,18 @@ const checkUrl = '/v1/auth';
providedIn: 'root'
})
export class AuthService {
public userObservable = new Subject<User>();
private readonly userSubject: Subject<User>;
private user: User;
public userObservable: Observable<User>;
constructor(private http: HttpClient, private log: ErrorLoggerService) {
this.check().subscribe();
this.userSubject = new Subject<User>();
this.userObservable = merge(this.checkObserver(), this.userSubject).pipe(
tap(x => this.user = x)
);
}
private _user: User;
get user() {
return this._user;
}
set user(user: User) {
this._user = user;
this.log.handleError('AuthService', 'Set User', user);
this.userObservable.next(user);
checkObserver(): Observable<User> {
return <Observable<User>>this.http.get<User>(checkUrl, httpOptions);
}
login(name: string, password: string, otp?: string, clientName?: string): Observable<any> {
@ -49,29 +45,20 @@ export class AuthService {
return this.http.post(loginUrl, data, httpOptions)
.pipe(
tap((user: User) => this.user = user),
tap((user: User) => this.userSubject.next(user)),
catchError(this.log.handleError('AuthService', 'login'))
);
}
logout(): Observable<any> {
return this.http.post(logoutUrl, {}, httpOptions)
logout(): Observable<boolean> {
return <Observable<boolean>>this.http.post<User>(logoutUrl, {}, httpOptions)
.pipe(
tap((user: User) => this.user = user),
catchError(this.log.handleError('AuthService', 'logout'))
tap((user: User) => this.userSubject.next(user)),
map(() => true)
);
}
check(): Observable<any> {
return this.http.get(checkUrl, httpOptions)
.pipe(
tap((user: User) => this.user = user),
catchError(this.log.handleError('AuthService', 'check'))
);
}
hasPermission(permission: string) : boolean {
hasPermission(permission: string): boolean {
return this.user !== undefined && this.user.isAuthenticated && this.user.perms.indexOf(permission) !== -1;
}
}