Add guard
This commit is contained in:
parent
cbcfff0c58
commit
589333e565
@ -16,5 +16,6 @@
|
|||||||
<li>
|
<li>
|
||||||
<h2><a target="_blank" rel="noopener" href="https://blog.angular.io/">Angular blog</a></h2>
|
<h2><a target="_blank" rel="noopener" href="https://blog.angular.io/">Angular blog</a></h2>
|
||||||
</li>
|
</li>
|
||||||
|
<app-test></app-test>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ import { LogService } from '@app/core/services';
|
|||||||
|
|
||||||
@NgModule({
|
@NgModule({
|
||||||
declarations: [
|
declarations: [
|
||||||
AppComponent
|
AppComponent,
|
||||||
],
|
],
|
||||||
imports: [
|
imports: [
|
||||||
BrowserModule,
|
BrowserModule,
|
||||||
@ -24,7 +24,7 @@ import { LogService } from '@app/core/services';
|
|||||||
LogService,
|
LogService,
|
||||||
{
|
{
|
||||||
provide: ErrorHandler,
|
provide: ErrorHandler,
|
||||||
useClass: GlobalErrorHandler
|
useClass: GlobalErrorHandler,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
bootstrap: [ AppComponent ]
|
bootstrap: [ AppComponent ]
|
||||||
|
26
src/app/core/guard/auth.guard.ts
Normal file
26
src/app/core/guard/auth.guard.ts
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
1
src/app/core/guard/index.ts
Normal file
1
src/app/core/guard/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from './auth.guard';
|
@ -23,10 +23,10 @@ export class AuthService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public get currentUserValue(): User {
|
public get currentUserValue(): User {
|
||||||
if ( this.currentUserSubject.value && this.currentUserSubject.value.timeGetToken + 60 * 60 * 1000 < new Date().getTime() ) {
|
// if ( this.currentUserSubject.value && this.currentUserSubject.value.timeGetToken + 60 * 60 * 1000 < new Date().getTime() ) {
|
||||||
const data = new FormData();
|
// 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 ) => {
|
// this.http.post<any>( environment.baseUrl + config.url.upauth, data ).subscribe( ( res ) => {
|
||||||
// if ( res.statusCode !== 200 ) {
|
// if ( res.statusCode !== 200 ) {
|
||||||
// this.logout();
|
// this.logout();
|
||||||
@ -35,15 +35,15 @@ export class AuthService {
|
|||||||
// this.stor.setLocal( 'user', this.currentUserSubject.value );
|
// this.stor.setLocal( 'user', this.currentUserSubject.value );
|
||||||
// }
|
// }
|
||||||
// } );
|
// } );
|
||||||
}
|
// }
|
||||||
return this.currentUserSubject.value;
|
return this.currentUserSubject.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public login( username: string, password: string ): any {
|
public login( username: string, password: string ): any {
|
||||||
const data: FormData = new FormData();
|
// const data: FormData = new FormData();
|
||||||
|
|
||||||
data.append( 'email', username );
|
// data.append( 'email', username );
|
||||||
data.append( 'password', sha512.sha512( password ) );
|
// data.append( 'password', sha512.sha512( password ) );
|
||||||
|
|
||||||
// return this.http.post<any>( environment.baseUrl + config.url.auth, data )
|
// return this.http.post<any>( environment.baseUrl + config.url.auth, data )
|
||||||
// .pipe( map( result => {
|
// .pipe( map( result => {
|
||||||
@ -69,9 +69,9 @@ export class AuthService {
|
|||||||
|
|
||||||
public logout(): void {
|
public logout(): void {
|
||||||
// remove user from local storage to log user out
|
// remove user from local storage to log user out
|
||||||
this.stor.setLocal( 'user' );
|
// this.stor.setLocal( 'user' );
|
||||||
this.currentUserSubject.next( null );
|
// this.currentUserSubject.next( null );
|
||||||
this.router.navigate( [ '/login' ] );
|
// this.router.navigate( [ '/login' ] );
|
||||||
}
|
}
|
||||||
|
|
||||||
public createAccount( email: string, password: string, repeat: string ): any {
|
public createAccount( email: string, password: string, repeat: string ): any {
|
||||||
|
@ -60,7 +60,7 @@ export class StorageService {
|
|||||||
} else {
|
} else {
|
||||||
this.data.localData[ key ] = val;
|
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 {
|
public setSession( key: string, value?: any ): void {
|
||||||
@ -79,7 +79,7 @@ export class StorageService {
|
|||||||
} else {
|
} else {
|
||||||
this.data.sessionData[ key ] = val;
|
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 {
|
public setPage( key: string, value?: any ): void {
|
||||||
|
Loading…
Reference in New Issue
Block a user