Redirect people when they have allready logged in


#1

Hello people

Im strugling a little bit with something in my code.
The scenario is that i want to make people push the register button. Then register with firebase email and password and after that they go to login page and login.

The first time they log in they need to fill out information about them self, then next time they log in they dont need to fill out all the stuff this is all working when i check for auth. But the problem im facing is that when people have registered they get redirected into the app because i written this:

App.component.ts

 this.auth.getAuthenticatedUser().subscribe(auth => {
  !auth ? this.rootPage = 'LoginPage' : this.rootPage = 'MyPage';
})

This works if i log back into the app then it knows that i have allready registered and takes my to MyPage.

I want my app to check if there is a boolean stored inside the firebase storage.
When people have registered in the after login in it changes in firebase under their UID “IsRegistered: True” but if they have not registered yet then this will be Undefined or the UID is not found cause they have not stored or registered yet.

It only stores after first login.
Is there a smart way to do this with like IF ELSE or simpler?

My setup is a follow:

Login -> Edit Profile -> MyPage
-> Sign Up -> Login again

Any help is GREATLY appreciated! Been trying to set this up for 3 days now and im stucked.

Regards


#2

Figured it out myself :smiley:


#3

In your AppComponent.ts:

import { Component } from '@angular/core';

import { Config, Nav, Platform } from 'ionic-angular';

import { AuthService, ServiceWorkerService, LoggerService } from '@core/index';

@Component({
  templateUrl: 'app.component.html'
})
export class AppComponent {

  // [root]="rootPage"
  public rootPage: any = 'SignInPage';

  // [class]="theme"
  public theme: String = 'orange-theme'; 

  constructor(public config: Config,
              public platform: Platform,
              private authService: AuthService,
              private swService: ServiceWorkerService,
              private logger: LoggerService) {

    this.initialiseApp();
  }

  private initialiseApp() {

    this.platform.ready().then(() => {
      this.swService.run();
    });

    this.authService.afAuth.authState.subscribe(user => {

        if (user) {
          this.rootPage = 'TabsPage';
        } else {
          this.rootPage = 'SignInPage';
        }
      }, () => {
        this.rootPage = 'SignInPage';
      }
    );

  }
}

app.component.html:

<ion-nav [root]="rootPage" [class]="theme"></ion-nav>

Register / Sign Up:

Sign In: