Prettied, Linted and updated angular.json according to the latest schematic of Angular CLI.
Now all that is needed is to make it ready for strict compiling. Removed eslint-plugin-prettier as it is not recommended and causes errors for both eslint and prettier Bumped to v8.0.0
This commit is contained in:
@ -1,9 +1,10 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
|
||||
|
||||
import { AuthService } from './auth.service';
|
||||
import { ToasterService } from '../core/toaster.service';
|
||||
|
||||
import { AuthService } from './auth.service';
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class AuthGuard implements CanActivate {
|
||||
constructor(
|
||||
@ -13,11 +14,11 @@ export class AuthGuard implements CanActivate {
|
||||
) {}
|
||||
|
||||
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
|
||||
const user = this.authService.user;
|
||||
const { user } = this.authService;
|
||||
const permission =
|
||||
route.data['permission'] === undefined
|
||||
? route.data['permission']
|
||||
: route.data['permission'].replace(/ /g, '-').toLowerCase();
|
||||
route.data.permission === undefined
|
||||
? route.data.permission
|
||||
: route.data.permission.replace(/ /g, '-').toLowerCase();
|
||||
if (!user) {
|
||||
// not logged in so redirect to login page with the return url
|
||||
this.router.navigate(['/login'], { queryParams: { returnUrl: state.url } });
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { Injectable } from '@angular/core';
|
||||
import { BehaviorSubject, Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
|
||||
import { User } from '../core/user';
|
||||
import { environment } from '../../environments/environment';
|
||||
import { User } from '../core/user';
|
||||
|
||||
const loginUrl = '/token';
|
||||
const refreshUrl = '/refresh';
|
||||
@ -20,16 +20,37 @@ export class AuthService {
|
||||
this.currentUser = this.currentUserSubject.asObservable();
|
||||
}
|
||||
|
||||
static parseJwt(token): User {
|
||||
const base64Url = token.split('.')[1];
|
||||
const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
|
||||
const jsonPayload = decodeURIComponent(
|
||||
atob(base64)
|
||||
.split('')
|
||||
.map((c) => `%${`00${c.charCodeAt(0).toString(16)}`.slice(-2)}`)
|
||||
.join(''),
|
||||
);
|
||||
|
||||
const decoded = JSON.parse(jsonPayload);
|
||||
return new User({
|
||||
id: decoded.userId,
|
||||
name: decoded.sub,
|
||||
lockedOut: decoded.lockedOut,
|
||||
perms: decoded.scopes,
|
||||
access_token: token,
|
||||
exp: decoded.exp,
|
||||
ver: decoded.ver,
|
||||
});
|
||||
}
|
||||
|
||||
checkStorage(): User {
|
||||
const existingToken: User = JSON.parse(localStorage.getItem(JWT_USER));
|
||||
if (existingToken === null || Date.now() > existingToken.exp * 1000) {
|
||||
localStorage.removeItem(JWT_USER);
|
||||
this.currentUserSubject = new BehaviorSubject<User>(null);
|
||||
return null;
|
||||
} else {
|
||||
this.currentUserSubject = new BehaviorSubject<User>(existingToken);
|
||||
return existingToken;
|
||||
}
|
||||
this.currentUserSubject = new BehaviorSubject<User>(existingToken);
|
||||
return existingToken;
|
||||
}
|
||||
|
||||
public get user(): User {
|
||||
@ -60,10 +81,11 @@ export class AuthService {
|
||||
return this.http
|
||||
.post<any>(loginUrl, formData)
|
||||
.pipe(map((u) => u.access_token))
|
||||
.pipe(map((u) => this.parseJwt(u)))
|
||||
.pipe(map((u) => AuthService.parseJwt(u)))
|
||||
.pipe(
|
||||
map((user) => {
|
||||
// store user details and jwt token in local storage to keep user logged in between page refreshes
|
||||
// Store user details and jwt token in local storage
|
||||
// to keep user logged in between page refreshes
|
||||
localStorage.setItem(JWT_USER, JSON.stringify(user));
|
||||
this.currentUserSubject.next(user);
|
||||
return user;
|
||||
@ -71,30 +93,6 @@ export class AuthService {
|
||||
);
|
||||
}
|
||||
|
||||
parseJwt(token): User {
|
||||
const base64Url = token.split('.')[1];
|
||||
const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
|
||||
const jsonPayload = decodeURIComponent(
|
||||
atob(base64)
|
||||
.split('')
|
||||
.map(function (c) {
|
||||
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
|
||||
})
|
||||
.join(''),
|
||||
);
|
||||
|
||||
const decoded = JSON.parse(jsonPayload);
|
||||
return new User({
|
||||
id: decoded.userId,
|
||||
name: decoded.sub,
|
||||
lockedOut: decoded.lockedOut,
|
||||
perms: decoded.scopes,
|
||||
access_token: token,
|
||||
exp: decoded.exp,
|
||||
ver: decoded.ver
|
||||
});
|
||||
}
|
||||
|
||||
needsRefreshing(): boolean {
|
||||
return Date.now() > (this.user.exp - environment.ACCESS_TOKEN_REFRESH_MINUTES * 60) * 1000;
|
||||
}
|
||||
@ -109,10 +107,11 @@ export class AuthService {
|
||||
return this.http
|
||||
.post<any>(refreshUrl, {})
|
||||
.pipe(map((u) => u.access_token))
|
||||
.pipe(map((u) => this.parseJwt(u)))
|
||||
.pipe(map((u) => AuthService.parseJwt(u)))
|
||||
.pipe(
|
||||
map((user) => {
|
||||
// store user details and jwt token in local storage to keep user logged in between page refreshes
|
||||
// Store user details and jwt token in local storage
|
||||
// to keep user logged in between page refreshes
|
||||
localStorage.setItem(JWT_USER, JSON.stringify(user));
|
||||
this.currentUserSubject.next(user);
|
||||
return user;
|
||||
|
||||
@ -1,9 +1,10 @@
|
||||
import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core';
|
||||
import { AuthService } from '../auth.service';
|
||||
import { ActivatedRoute, Router } from '@angular/router';
|
||||
import { ToasterService } from '../../core/toaster.service';
|
||||
import { FormBuilder, FormGroup } from '@angular/forms';
|
||||
import { ActivatedRoute, Router } from '@angular/router';
|
||||
|
||||
import { ToasterService } from '../../core/toaster.service';
|
||||
import { CookieService } from '../../shared/cookie.service';
|
||||
import { AuthService } from '../auth.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-login',
|
||||
@ -40,7 +41,7 @@ export class LoginComponent implements OnInit, AfterViewInit {
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/';
|
||||
this.returnUrl = this.route.snapshot.queryParams.returnUrl || '/';
|
||||
}
|
||||
|
||||
ngAfterViewInit() {
|
||||
@ -51,18 +52,18 @@ export class LoginComponent implements OnInit, AfterViewInit {
|
||||
|
||||
login(): void {
|
||||
const formModel = this.form.value;
|
||||
const username = formModel.username;
|
||||
const password = formModel.password;
|
||||
const otp = formModel.otp;
|
||||
const { username } = formModel;
|
||||
const { password } = formModel;
|
||||
const { otp } = formModel;
|
||||
this.auth
|
||||
.login(username, password, otp)
|
||||
// .pipe(first())
|
||||
.subscribe(
|
||||
(data) => {
|
||||
() => {
|
||||
this.router.navigate([this.returnUrl]);
|
||||
},
|
||||
(error) => {
|
||||
if (error.status === 401 && 'Client is not registered' === error.error.detail) {
|
||||
if (error.status === 401 && error.error.detail === 'Client is not registered') {
|
||||
this.showOtp = true;
|
||||
this.clientId = this.cs.getCookie('client_id');
|
||||
}
|
||||
|
||||
@ -1,7 +1,8 @@
|
||||
import { Component, OnInit } from '@angular/core';
|
||||
import { Router } from '@angular/router';
|
||||
import { AuthService } from '../auth.service';
|
||||
|
||||
import { ToasterService } from '../../core/toaster.service';
|
||||
import { AuthService } from '../auth.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-logout',
|
||||
|
||||
Reference in New Issue
Block a user