back button issues fixed and missing app module fixed

This commit is contained in:
venbaSriram 2018-09-25 11:35:35 +05:30
parent f1eb384b9a
commit 3b244597ac
6 changed files with 77 additions and 57 deletions

View File

@ -167,9 +167,12 @@ export class AwsService {
* Logout function * Logout function
*/ */
logout(){ logout(){
return Observable.create(observe=>{ return Observable.create(observe=>{
this.shared.getCurrentUser().signOut(); this.shared.getCurrentUser().signOut();
observe.next(); observe.next();
observe.complete(); observe.complete();
}); });
} }

View File

@ -11,6 +11,7 @@ import {
CognitoUserSession CognitoUserSession
} from "amazon-cognito-identity-js"; } from "amazon-cognito-identity-js";
import { environment } from '../../environments/environment'; import { environment } from '../../environments/environment';
import { BehaviorSubject } from 'rxjs';
const sineedge_poolData = { const sineedge_poolData = {
UserPoolId: "ap-south-1_y7dt3iEaX", //ap-south-1_qQ85yQZJO UserName :SPARQ_SINEEDGE_DEV UserPoolId: "ap-south-1_y7dt3iEaX", //ap-south-1_qQ85yQZJO UserName :SPARQ_SINEEDGE_DEV
ClientId: "291qt4smn5pql6o8cc2hi201lj" // ClientName: SINEEDGE_DEV_WEB ClientId: "291qt4smn5pql6o8cc2hi201lj" // ClientName: SINEEDGE_DEV_WEB
@ -49,6 +50,7 @@ export interface Callback {
providedIn: 'root' providedIn: 'root'
}) })
export class CognitoService implements CanActivateChild { export class CognitoService implements CanActivateChild {
public loggedIn :boolean = false;
// public static _REGION = environment.region; // public static _REGION = environment.region;
// public static _IDENTITY_POOL_ID = environment.identityPoolId; // public static _IDENTITY_POOL_ID = environment.identityPoolId;
@ -59,6 +61,14 @@ export class CognitoService implements CanActivateChild {
// UserPoolId: CognitoService._USER_POOL_ID, // UserPoolId: CognitoService._USER_POOL_ID,
// ClientId: CognitoService._CLIENT_ID // ClientId: CognitoService._CLIENT_ID
// }; // };
get isLoggedIn() {
if(sineedgedevUserpool.getCurrentUser() !=null){
this.loggedIn = true;
}else{
this.loggedIn = false;
}
return this.loggedIn; // {2}
}
canActivateChild(){ canActivateChild(){
if(this.getToken()){ if(this.getToken()){
//alert('session valid') //alert('session valid')
@ -82,6 +92,7 @@ isloggin:boolean = false;
// } // }
getCurrentUser() { getCurrentUser() {
return sineedgedevUserpool.getCurrentUser(); return sineedgedevUserpool.getCurrentUser();
} }
constructor(public route:Router) { } constructor(public route:Router) { }

View File

@ -1,45 +1,37 @@
// import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
// import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, CanActivateChild } from '@angular/router'; import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, CanActivateChild } from '@angular/router';
// import { Observable , of } from 'rxjs'; import { Observable , of } from 'rxjs';
import { CognitoService } from '../AwsService/cognito.service';
@Injectable()
export class AuthGuard implements CanActivate, CanActivateChild {
// @Injectable() constructor(private router: Router,public shared:CognitoService) { }
// export class AuthGuard implements CanActivate, CanActivateChild {
// 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) { canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
// 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()) {
// if (this.loginService.loggedIn()) { // alert("if");
// let roles = route.data["role"] as Array<string>;
// console.log("dataRole: " +roles); return true;
// if(roles.length > -1){ } else {
// var match = this.loginService.roleMatch(roles); // alert("else");
// if(match){ this.router.navigate(['/login']);
// // this.router.navigate(['./dashboard']); return false;
// 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 { // public getToken(): string {
// return localStorage.getItem('token'); // return localStorage.getItem('token');
@ -51,4 +43,4 @@
// this.router.navigate(['/login']); // this.router.navigate(['/login']);
// } // }
// } // }
// } }

View File

@ -30,7 +30,7 @@ import {MatCheckboxModule} from '@angular/material/checkbox';
import {MatProgressBarModule} from '@angular/material/progress-bar'; import {MatProgressBarModule} from '@angular/material/progress-bar';
import {MatSelectModule} from '@angular/material/select'; import {MatSelectModule} from '@angular/material/select';
import {MatCardModule} from '@angular/material/card'; 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 { AppRoutes } from './app.routing';
import { AppComponent } from './app.component'; import { AppComponent } from './app.component';
@ -40,6 +40,7 @@ import { SharedModule } from './shared/shared.module';
import { AwsService } from './AwsService/aws.service'; import { AwsService } from './AwsService/aws.service';
import { TokenInterceptor } from './_guard/token.interceptor'; import { TokenInterceptor } from './_guard/token.interceptor';
import { NewPasswordDialog, ForgotPass } from './session/login/login.component'; import { NewPasswordDialog, ForgotPass } from './session/login/login.component';
import { AuthGuard } from './_guard/auth.guard';
export function HttpLoaderFactory(http: HttpClient) { export function HttpLoaderFactory(http: HttpClient) {
return new TranslateHttpLoader(http); return new TranslateHttpLoader(http);
} }
@ -57,7 +58,7 @@ const DEFAULT_PERFECT_SCROLLBAR_CONFIG: PerfectScrollbarConfigInterface = {
AppComponent, AppComponent,
AdminLayoutComponent, AdminLayoutComponent,
AuthLayoutComponent, AuthLayoutComponent,
NewPasswordDialog,ForgotPass,ChangePasswordDialog NewPasswordDialog,ForgotPass,ChangePasswordDialog,
], ],
imports: [ imports: [
BrowserModule, BrowserModule,
@ -93,7 +94,7 @@ const DEFAULT_PERFECT_SCROLLBAR_CONFIG: PerfectScrollbarConfigInterface = {
provide: LocationStrategy, provide: LocationStrategy,
useClass: HashLocationStrategy, useClass: HashLocationStrategy,
}, },
AwsService, AwsService,AuthGuard,
TranslateService, TranslateService,
// { // {
// provide: PERFECT_SCROLLBAR_CONFIG, // provide: PERFECT_SCROLLBAR_CONFIG,

View File

@ -3,6 +3,7 @@ import { Routes } from '@angular/router';
import { AdminLayoutComponent } from './layouts/admin/admin-layout.component'; import { AdminLayoutComponent } from './layouts/admin/admin-layout.component';
import { AuthLayoutComponent } from './layouts/auth/auth-layout.component'; import { AuthLayoutComponent } from './layouts/auth/auth-layout.component';
import { CognitoService} from './AwsService/cognito.service'; import { CognitoService} from './AwsService/cognito.service';
import { AuthGuard } from './_guard/auth.guard';
export const AppRoutes: Routes = [{ export const AppRoutes: Routes = [{
path: '', path: '',
redirectTo: 'home', redirectTo: 'home',
@ -10,7 +11,7 @@ export const AppRoutes: Routes = [{
}, { }, {
path: '', path: '',
component: AdminLayoutComponent, component: AdminLayoutComponent,
canActivateChild:[CognitoService], canActivateChild:[AuthGuard],
children: [{ children: [{
path: 'home', path: 'home',
loadChildren: './dashboard/dashboard.module#DashboardModule' loadChildren: './dashboard/dashboard.module#DashboardModule'

View File

@ -20,7 +20,7 @@ import { CognitoService } from '../../AwsService/cognito.service';
styleUrls: ['./login-component.scss'], styleUrls: ['./login-component.scss'],
encapsulation: ViewEncapsulation.None, encapsulation: ViewEncapsulation.None,
}) })
export class LoginComponent { export class LoginComponent implements OnInit{
changepass: FormGroup; changepass: FormGroup;
email: string; email: string;
password: string; password: string;
@ -43,6 +43,10 @@ export class LoginComponent {
private router: Router, public aws: AwsService, public dialog: MatDialog, private router: Router, public aws: AwsService, public dialog: MatDialog,
public shared:CognitoService,) { public shared:CognitoService,) {
}
ngOnInit(){
this.loggedInCheck();
} }
/** /**
* @param email * @param email
@ -103,9 +107,17 @@ export class LoginComponent {
console.log(res); 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;
}
} }
} }