Add guard

This commit is contained in:
Mathieu Sanchez 2019-05-21 11:52:12 +09:00
parent cbcfff0c58
commit 589333e565
6 changed files with 42 additions and 14 deletions

View File

@ -16,5 +16,6 @@
<li>
<h2><a target="_blank" rel="noopener" href="https://blog.angular.io/">Angular blog</a></h2>
</li>
<app-test></app-test>
</ul>

View File

@ -12,7 +12,7 @@ import { LogService } from '@app/core/services';
@NgModule({
declarations: [
AppComponent
AppComponent,
],
imports: [
BrowserModule,
@ -24,7 +24,7 @@ import { LogService } from '@app/core/services';
LogService,
{
provide: ErrorHandler,
useClass: GlobalErrorHandler
useClass: GlobalErrorHandler,
},
],
bootstrap: [ AppComponent ]

View File

@ -0,0 +1,26 @@
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router';
import { AuthService } from '@app/core/services';
@Injectable( { providedIn: 'root' } )
export class AuthGuard implements CanActivate {
constructor( private router: Router, private authenticationService: AuthService ) {
}
canActivate( route: ActivatedRouteSnapshot, state: RouterStateSnapshot ) {
const currentUser = this.authenticationService.currentUserValue;
if ( currentUser ) {
// logged in so return true
return true;
}
// not logged in so redirect to login page with the return url
this.router.navigate( [ '/login' ], { queryParams: { returnUrl: state.url } } );
return false;
}
}

View File

@ -0,0 +1 @@
export * from './auth.guard';

View File

@ -23,10 +23,10 @@ export class AuthService {
}
public get currentUserValue(): User {
if ( this.currentUserSubject.value && this.currentUserSubject.value.timeGetToken + 60 * 60 * 1000 < new Date().getTime() ) {
const data = new FormData();
// if ( this.currentUserSubject.value && this.currentUserSubject.value.timeGetToken + 60 * 60 * 1000 < new Date().getTime() ) {
// const data = new FormData();
data.append( 'auth_token', this.currentUserSubject.value.token );
// data.append( 'auth_token', this.currentUserSubject.value.token );
// this.http.post<any>( environment.baseUrl + config.url.upauth, data ).subscribe( ( res ) => {
// if ( res.statusCode !== 200 ) {
// this.logout();
@ -35,15 +35,15 @@ export class AuthService {
// this.stor.setLocal( 'user', this.currentUserSubject.value );
// }
// } );
}
// }
return this.currentUserSubject.value;
}
public login( username: string, password: string ): any {
const data: FormData = new FormData();
// const data: FormData = new FormData();
data.append( 'email', username );
data.append( 'password', sha512.sha512( password ) );
// data.append( 'email', username );
// data.append( 'password', sha512.sha512( password ) );
// return this.http.post<any>( environment.baseUrl + config.url.auth, data )
// .pipe( map( result => {
@ -69,9 +69,9 @@ export class AuthService {
public logout(): void {
// remove user from local storage to log user out
this.stor.setLocal( 'user' );
this.currentUserSubject.next( null );
this.router.navigate( [ '/login' ] );
// this.stor.setLocal( 'user' );
// this.currentUserSubject.next( null );
// this.router.navigate( [ '/login' ] );
}
public createAccount( email: string, password: string, repeat: string ): any {

View File

@ -60,7 +60,7 @@ export class StorageService {
} else {
this.data.localData[ key ] = val;
}
localStorage.setItem( 'c2a', JSON.stringify( this.data.localData ) );
localStorage.setItem( 'discoTrip', JSON.stringify( this.data.localData ) );
}
public setSession( key: string, value?: any ): void {
@ -79,7 +79,7 @@ export class StorageService {
} else {
this.data.sessionData[ key ] = val;
}
sessionStorage.setItem( 'c2a', JSON.stringify( this.data.localData ) );
sessionStorage.setItem( 'discoTrip', JSON.stringify( this.data.localData ) );
}
public setPage( key: string, value?: any ): void {