Skip to content

Commit

Permalink
feat(auth): add guards
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarbeau committed Aug 30, 2018
1 parent 2fb09b1 commit 49c6bc9
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 18 deletions.
37 changes: 37 additions & 0 deletions projects/auth/src/lib/shared/admin.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Injectable } from '@angular/core';
import {
CanActivate,
Router,
ActivatedRouteSnapshot,
RouterStateSnapshot
} from '@angular/router';

import { ConfigService } from '@igo2/core';
import { AuthService } from './auth.service';

@Injectable({
providedIn: 'root'
})
export class AdminGuard implements CanActivate {
constructor(
private authService: AuthService,
private config: ConfigService,
private router: Router
) {}

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
const token = this.authService.decodeToken();
if (token && token.user && token.user.admin) {
return true;
}

this.authService.redirectUrl = state.url;

const authConfig = this.config.getConfig('auth');
if (authConfig && authConfig.loginRoute) {
this.router.navigateByUrl(authConfig.loginRoute);
}

return false;
}
}
2 changes: 1 addition & 1 deletion projects/auth/src/lib/shared/auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class AuthGuard implements CanActivate {
) {}

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
if (this.authService.logged) {
if (this.authService.authenticated) {
return true;
}

Expand Down
5 changes: 5 additions & 0 deletions projects/auth/src/lib/shared/auth.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@ export interface AuthOptions {
facebook?: AuthFacebookOptions;
google?: AuthGoogleOptions;
trustHosts?: string[];
profilsGuard?: string[];
}

export interface User {
source?: string;
sourceId?: string;
email?: string;
firstName?: string;
lastName?: string;
locale?: string;
admin?: boolean;
defaultContextId?: string;
}
41 changes: 24 additions & 17 deletions projects/auth/src/lib/shared/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Router } from '@angular/router';
import { Observable, BehaviorSubject, of } from 'rxjs';
import { tap } from 'rxjs/operators';

import { ConfigService } from '@igo2/core';
import { ConfigService, LanguageService } from '@igo2/core';
import { Base64 } from '@igo2/utils';

import { AuthOptions, User } from './auth.interface';
Expand All @@ -24,6 +24,7 @@ export class AuthService {
private http: HttpClient,
private tokenService: TokenService,
private config: ConfigService,
private languageService: LanguageService,
@Optional() private router: Router
) {
this.options = this.config.getConfig('auth') || {};
Expand All @@ -39,14 +40,7 @@ export class AuthService {
password: this.encodePassword(password)
});

return this.http
.post(`${this.options.url}/login`, body, { headers: myHeader })
.pipe(
tap((data: any) => {
this.tokenService.set(data.token);
this.authenticate$.next(true);
})
);
return this.loginCall(body, myHeader);
}

loginWithToken(token: string, type: string): any {
Expand All @@ -58,14 +52,7 @@ export class AuthService {
typeConnection: type
});

return this.http
.post(`${this.options.url}/login`, body, { headers: myHeader })
.pipe(
tap((data: any) => {
this.tokenService.set(data.token);
this.authenticate$.next(true);
})
);
return this.loginCall(body, myHeader);
}

loginAnonymous() {
Expand Down Expand Up @@ -113,6 +100,10 @@ export class AuthService {
return this.http.get<User>(url);
}

getProfils() {
return this.http.get(`${this.options.url}/profils`);
}

updateUser(user: User): Observable<User> {
const url = this.options.url;
return this.http.patch<User>(url, JSON.stringify(user));
Expand All @@ -122,6 +113,7 @@ export class AuthService {
return Base64.encode(password);
}

// authenticated or anonymous
get logged(): boolean {
return this.authenticated || this.isAnonymous;
}
Expand All @@ -133,4 +125,19 @@ export class AuthService {
get authenticated(): boolean {
return this.isAuthenticated();
}

private loginCall(body, headers) {
return this.http
.post(`${this.options.url}/login`, body, { headers: headers })
.pipe(
tap((data: any) => {
this.tokenService.set(data.token);
const tokenDecoded = this.decodeToken();
if (tokenDecoded && tokenDecoded.user && tokenDecoded.user.locale) {
this.languageService.setLanguage(tokenDecoded.user.locale);
}
this.authenticate$.next(true);
})
);
}
}
3 changes: 3 additions & 0 deletions projects/auth/src/lib/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@ export * from './token.service';
export * from './auth.service';
export * from './auth.interface';
export * from './auth.interceptor';
export * from './logged.guard';
export * from './auth.guard';
export * from './admin.guard';
export * from './profils.guard';
export * from './protected.directive';
36 changes: 36 additions & 0 deletions projects/auth/src/lib/shared/logged.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Injectable } from '@angular/core';
import {
CanActivate,
Router,
ActivatedRouteSnapshot,
RouterStateSnapshot
} from '@angular/router';

import { ConfigService } from '@igo2/core';
import { AuthService } from './auth.service';

@Injectable({
providedIn: 'root'
})
export class LoggedGuard implements CanActivate {
constructor(
private authService: AuthService,
private config: ConfigService,
private router: Router
) {}

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
if (this.authService.logged) {
return true;
}

this.authService.redirectUrl = state.url;

const authConfig = this.config.getConfig('auth');
if (authConfig && authConfig.loginRoute) {
this.router.navigateByUrl(authConfig.loginRoute);
}

return false;
}
}
45 changes: 45 additions & 0 deletions projects/auth/src/lib/shared/profils.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Injectable } from '@angular/core';
import {
CanActivate,
Router,
ActivatedRouteSnapshot,
RouterStateSnapshot
} from '@angular/router';
import { map } from 'rxjs/operators';

import { ConfigService } from '@igo2/core';
import { AuthService } from './auth.service';

@Injectable({
providedIn: 'root'
})
export class ProfilsGuard implements CanActivate {
constructor(
private authService: AuthService,
private config: ConfigService,
private router: Router
) {}

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
return this.authService.getProfils().pipe(
map((profils: string[]) => {
const authConfig = this.config.getConfig('auth');

if (
profils &&
profils.some(v => authConfig.profilsGuard.indexOf(v) !== -1)
) {
return true;
}

this.authService.redirectUrl = state.url;

if (authConfig && authConfig.loginRoute) {
this.router.navigateByUrl(authConfig.loginRoute);
}

return false;
})
);
}
}

0 comments on commit 49c6bc9

Please sign in to comment.