diff --git a/src/app/app.component.html b/src/app/app.component.html index 5226d57..a3005ba 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -16,5 +16,6 @@
  • Angular blog

  • + diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 01b99b0..208d5d8 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -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 ] diff --git a/src/app/core/guard/auth.guard.ts b/src/app/core/guard/auth.guard.ts new file mode 100644 index 0000000..8f34b5e --- /dev/null +++ b/src/app/core/guard/auth.guard.ts @@ -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; + } + +} diff --git a/src/app/core/guard/index.ts b/src/app/core/guard/index.ts new file mode 100644 index 0000000..b41e34a --- /dev/null +++ b/src/app/core/guard/index.ts @@ -0,0 +1 @@ +export * from './auth.guard'; diff --git a/src/app/core/services/auth/auth.service.ts b/src/app/core/services/auth/auth.service.ts index 8988993..8af36c3 100644 --- a/src/app/core/services/auth/auth.service.ts +++ b/src/app/core/services/auth/auth.service.ts @@ -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( 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( 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 { diff --git a/src/app/core/services/storage/storage.service.ts b/src/app/core/services/storage/storage.service.ts index 69ad5db..5828ba3 100644 --- a/src/app/core/services/storage/storage.service.ts +++ b/src/app/core/services/storage/storage.service.ts @@ -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 {