Bhrungarajni
Bhrungarajni

Reputation: 2535

How to make the page to work with signOut even after refresh

I have home page, in which i have signin and signout column. I have user page, in which one more signUp form is there. Now, if i signOut, it works fine but once i refresh the page, it works as still the user is not signOut. Can anyone help me to solve my issue.

HTML of User page:

<ul>
<li class="signUp" *ngIf = "loggedIn">{{userName}}</li>
<li class="signIn" (click)="signOut()" *ngIf = "loggedIn">Sign Out</li>
<li class="signUp" (click)="openSignUp(user)"  *ngIf = "!loggedIn">Sign Up</li>
<li class="signIn" (click)="openSignIn(logedin)"  *ngIf = "!loggedIn">Sign In</li>
</ul>

TS:

**In service:** 



 private signOutSource = new Subject<any>(); 
    signoutDone$ = this.signOutSource.asObservable(); 

signOut() { 
this.signOutSource.next(); 
} 

home.ts:

export class HomeComponent implements OnDestroy { 
constructor(private ApiService:ApiService) { 
this.subscription = ApiService.signinDone$.subscribe( 
user => { 
this.userName = user.name; 
localStorage.setItem('loggedin', 'true'); 
localStorage.setItem('user', JSON.stringify(user)); 
this.loggedIn = true; 
this.isLoadingSignUp = false; 
this.ApiService.getUserData(user); 
}); 
if(localStorage.getItem('loggedin')){ 
this.loggedIn = true; 
} else { 
this.loggedIn = false; 
} 
if(localStorage.getItem('user')){ 
this.userName = JSON.parse(localStorage.getItem('user')).name; 
} 
this.logoutSubscription = ApiService.signoutDone$.subscribe( 
user => { 
localStorage.setItem('loggedin', 'false'); 
this.loggedIn = false; 
this.ApiService.getUserData({}); 
}); 
} 

ngOnDestroy() { 
this.subscription.unsubscribe(); 
} 

signOut() { 
this.ApiService 
.logout() 
.subscribe( 
user => { 
localStorage.removeItem('user'); 
localStorage.removeItem('loggedin'); 
this.ApiService.getUserData({}); 
this.loggedIn = false; 
this.router.navigate(['/home']); 
this.toasterService.pop('success', 'SignOut Successfull'); 
this.ApiService.signOut(); 
},error => { 
if(error.data && error.data.length > 0) { 
this.toasterService.pop('error', error.data); 
} else { 
this.toasterService.pop('error', 'Something went wrong!'); 
} 
}); 
} 

} 

User.componet:

export class UserComponent implements OnDestroy { 
constructor(private ApiService:ApiService) { 
this.subscription = ApiService.signinDone$.subscribe( 
user => { 
this.userName = user.name; 
localStorage.setItem('loggedin', 'true'); 
localStorage.setItem('user', JSON.stringify(user)); 
this.loggedIn = true; 
this.isLoadingSignUp = false; 
this.ApiService.getUserData(user); 
}); 
if(localStorage.getItem('loggedin')){ 
this.loggedIn = true; 
} else { 
this.loggedIn = false; 
} 
if(localStorage.getItem('user')){ 
this.userName = JSON.parse(localStorage.getItem('user')).name; 
} 
this.logoutSubscription = ApiService.signoutDone$.subscribe( 
user => { 
localStorage.setItem('loggedin', 'false'); 
this.loggedIn = false; 
this.ApiService.getUserData({}); 
}); 
} 

ngOnDestroy() { 
this.subscription.unsubscribe(); 
} 

}

HTML of user Page:

 <div class="favourate" *ngIf="!loggedIn">
    <h1>User</h1>
    <hr />
    <div class="backImage">
      <form name="signUpForm" class="signUpForm" #signUpForm="ngForm" novalidate>
        <div class="form-group">
          <h3>Sign Up</h3>
        </div>
        <div class="form-group input">
          <mat-form-field> 
            <mat-icon>perm_identity</mat-icon>
            <input matInput type="text" placeholder="Name" name="name" [(ngModel)]="name" #Name="ngModel" required>
          </mat-form-field>
        </div>
        <div class="form-group input">
          <mat-form-field> 
            <mat-icon>email</mat-icon>
            <input matInput type="email" placeholder="Email" name="email" [(ngModel)]="user.email" #Email="ngModel" pattern="^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$" required>
          </mat-form-field>
        </div>
        <div class="form-group input">
          <mat-form-field> 
            <mat-icon>lock</mat-icon>
            <input matInput type="password" placeholder="Password" name="password" [(ngModel)]="user.password" #Password="ngModel" required>
          </mat-form-field>
        </div>          
      </form>
    </div>
  </div>

Upvotes: 1

Views: 53

Answers (1)

Sravan
Sravan

Reputation: 18647

Since you are storing loggedIn details in local storage, Just add localstorage check in ngOnOnit.

export class AppComponent implements OnInit, OnDestroy { 

    ngOnInit() { 
        if(localStorage.getItem('loggedin')){ 
            if(localStorage.getItem('loggedin') == 'true') 
            { 
                this.loggedIn = true; 
            } 
            else
            { 
                this.loggedIn = false; 
            } 
        }else{
            this.loggedIn = false; 
        }
    }
}

Upvotes: 1

Related Questions