diff --git a/ng6-seed/src/app/AwsService/aws.service.ts b/ng6-seed/src/app/AwsService/aws.service.ts index 2c4e60386..745763590 100755 --- a/ng6-seed/src/app/AwsService/aws.service.ts +++ b/ng6-seed/src/app/AwsService/aws.service.ts @@ -167,9 +167,12 @@ export class AwsService { * Logout function */ logout(){ + return Observable.create(observe=>{ this.shared.getCurrentUser().signOut(); + observe.next(); + observe.complete(); }); } diff --git a/ng6-seed/src/app/AwsService/cognito.service.ts b/ng6-seed/src/app/AwsService/cognito.service.ts index 4f5e240a3..33bcc3b64 100644 --- a/ng6-seed/src/app/AwsService/cognito.service.ts +++ b/ng6-seed/src/app/AwsService/cognito.service.ts @@ -11,6 +11,7 @@ import { CognitoUserSession } from "amazon-cognito-identity-js"; import { environment } from '../../environments/environment'; +import { BehaviorSubject } from 'rxjs'; const sineedge_poolData = { UserPoolId: "ap-south-1_y7dt3iEaX", //ap-south-1_qQ85yQZJO UserName :SPARQ_SINEEDGE_DEV ClientId: "291qt4smn5pql6o8cc2hi201lj" // ClientName: SINEEDGE_DEV_WEB @@ -49,6 +50,7 @@ export interface Callback { providedIn: 'root' }) export class CognitoService implements CanActivateChild { + public loggedIn :boolean = false; // public static _REGION = environment.region; // public static _IDENTITY_POOL_ID = environment.identityPoolId; @@ -59,6 +61,14 @@ export class CognitoService implements CanActivateChild { // UserPoolId: CognitoService._USER_POOL_ID, // ClientId: CognitoService._CLIENT_ID // }; + get isLoggedIn() { + if(sineedgedevUserpool.getCurrentUser() !=null){ + this.loggedIn = true; + }else{ + this.loggedIn = false; + } + return this.loggedIn; // {2} + } canActivateChild(){ if(this.getToken()){ //alert('session valid') @@ -82,6 +92,7 @@ isloggin:boolean = false; // } getCurrentUser() { + return sineedgedevUserpool.getCurrentUser(); } constructor(public route:Router) { } diff --git a/ng6-seed/src/app/_guard/auth.guard.ts b/ng6-seed/src/app/_guard/auth.guard.ts index 72d01d860..8d8db8cac 100644 --- a/ng6-seed/src/app/_guard/auth.guard.ts +++ b/ng6-seed/src/app/_guard/auth.guard.ts @@ -1,54 +1,46 @@ -// import { Injectable } from '@angular/core'; -// import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, CanActivateChild } from '@angular/router'; -// import { Observable , of } from 'rxjs'; +import { Injectable } from '@angular/core'; +import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, CanActivateChild } from '@angular/router'; +import { Observable , of } from 'rxjs'; +import { CognitoService } from '../AwsService/cognito.service'; +@Injectable() +export class AuthGuard implements CanActivate, CanActivateChild { -// @Injectable() -// export class AuthGuard implements CanActivate, CanActivateChild { + constructor(private router: Router,public shared:CognitoService) { } -// constructor(private router: Router) { } + canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { + if (this.shared.getToken()) { + alert("if"); + // this.router.navigate(['']); + return true + } else { + alert("else"); + this.router.navigate(['/login']); + return false; + } + } -// canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { -// if (localStorage.getItem('token') != null) { -// // this.router.navigate(['']); -// return true -// } else { -// this.router.navigate(['/login']); -// return false; -// } -// } + canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { + + if (this.shared.getToken()) { + // alert("if"); + + return true; + } else { + // alert("else"); + this.router.navigate(['/login']); + return false; + + } + } -// canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { -// if (this.loginService.loggedIn()) { -// let roles = route.data["role"] as Array; -// console.log("dataRole: " +roles); -// if(roles.length > -1){ -// var match = this.loginService.roleMatch(roles); -// if(match){ -// // this.router.navigate(['./dashboard']); -// return this.loginService.loggedIn() -// } -// else { -// this.router.navigate(['./404']); -// return this.loginService.loggedIn(); -// } -// } else { -// this.router.navigate(['/login']); -// return this.loginService.loggedIn(); -// } -// } else { -// this.router.navigate(['/login']); -// return this.loginService.loggedIn(); -// } -// } + // public getToken(): string { + // return localStorage.getItem('token'); + // } -// public getToken(): string { -// return localStorage.getItem('token'); -// } - -// public logOut() { -// localStorage.clear(); -// if(localStorage.getItem('token') === null){ -// this.router.navigate(['/login']); -// } -// } -// } \ No newline at end of file + // public logOut() { + // localStorage.clear(); + // if(localStorage.getItem('token') === null){ + // this.router.navigate(['/login']); + // } + // } +} \ No newline at end of file diff --git a/ng6-seed/src/app/app.module.ts b/ng6-seed/src/app/app.module.ts index 2bd1fbecb..2a253a156 100644 --- a/ng6-seed/src/app/app.module.ts +++ b/ng6-seed/src/app/app.module.ts @@ -30,7 +30,7 @@ import {MatCheckboxModule} from '@angular/material/checkbox'; import {MatProgressBarModule} from '@angular/material/progress-bar'; import {MatSelectModule} from '@angular/material/select'; import {MatCardModule} from '@angular/material/card'; -import { ChangePasswordDialog } from './settings/users/' +import {ChangePasswordDialog} from './settings/users/user-profile/user-profile.component'; import { AppRoutes } from './app.routing'; import { AppComponent } from './app.component'; @@ -40,6 +40,7 @@ import { SharedModule } from './shared/shared.module'; import { AwsService } from './AwsService/aws.service'; import { TokenInterceptor } from './_guard/token.interceptor'; import { NewPasswordDialog, ForgotPass } from './session/login/login.component'; +import { AuthGuard } from './_guard/auth.guard'; export function HttpLoaderFactory(http: HttpClient) { return new TranslateHttpLoader(http); } @@ -57,7 +58,7 @@ const DEFAULT_PERFECT_SCROLLBAR_CONFIG: PerfectScrollbarConfigInterface = { AppComponent, AdminLayoutComponent, AuthLayoutComponent, - NewPasswordDialog,ForgotPass,ChangePasswordDialog + NewPasswordDialog,ForgotPass,ChangePasswordDialog, ], imports: [ BrowserModule, @@ -93,7 +94,7 @@ const DEFAULT_PERFECT_SCROLLBAR_CONFIG: PerfectScrollbarConfigInterface = { provide: LocationStrategy, useClass: HashLocationStrategy, }, - AwsService, + AwsService,AuthGuard, TranslateService, // { // provide: PERFECT_SCROLLBAR_CONFIG, diff --git a/ng6-seed/src/app/app.routing.ts b/ng6-seed/src/app/app.routing.ts index 73226d27f..558eaf611 100644 --- a/ng6-seed/src/app/app.routing.ts +++ b/ng6-seed/src/app/app.routing.ts @@ -3,6 +3,7 @@ import { Routes } from '@angular/router'; import { AdminLayoutComponent } from './layouts/admin/admin-layout.component'; import { AuthLayoutComponent } from './layouts/auth/auth-layout.component'; import { CognitoService} from './AwsService/cognito.service'; +import { AuthGuard } from './_guard/auth.guard'; export const AppRoutes: Routes = [{ path: '', redirectTo: 'home', @@ -10,7 +11,7 @@ export const AppRoutes: Routes = [{ }, { path: '', component: AdminLayoutComponent, - canActivateChild:[CognitoService], + canActivateChild:[AuthGuard], children: [{ path: 'home', loadChildren: './dashboard/dashboard.module#DashboardModule' diff --git a/ng6-seed/src/app/session/login/login.component.ts b/ng6-seed/src/app/session/login/login.component.ts index b035b35e1..e03342224 100644 --- a/ng6-seed/src/app/session/login/login.component.ts +++ b/ng6-seed/src/app/session/login/login.component.ts @@ -20,7 +20,7 @@ import { CognitoService } from '../../AwsService/cognito.service'; styleUrls: ['./login-component.scss'], encapsulation: ViewEncapsulation.None, }) -export class LoginComponent { +export class LoginComponent implements OnInit{ changepass: FormGroup; email: string; password: string; @@ -43,6 +43,10 @@ export class LoginComponent { private router: Router, public aws: AwsService, public dialog: MatDialog, public shared:CognitoService,) { + } + ngOnInit(){ + this.loggedInCheck(); + } /** * @param email @@ -103,9 +107,17 @@ export class LoginComponent { console.log(res); }) - - - + } + loggedInCheck(){ + //alert(this.shared.isLoggedIn); + if(this.shared.isLoggedIn){ + // alert("if"); + this.router.navigate(['/home']); + return false; + }else{ + //alert("else"); + return true; + } } }