2

I am building an app with Ionic 6, Angular 13, and using AWS Amplify Authenticator. I have successfully implemented this but the issue I am running in to is that the tab more does not render on mobile browsers.

I added the amplify authenticator component to the app.component.html file based on the documentation here.

app.component.html code:

<ion-app>
    <amplify-authenticator [socialProviders]="['apple', 'google']">
      <ng-template amplifySlot="header">
        <div style="padding: var(--amplify-space-large); text-align: center">
          <img
            class="amplify-image"
            alt="Amplify logo"
            src="https://docs.amplify.aws/assets/logo-dark.svg"
          />
        </div>
      </ng-template>
      <ng-template 
        amplifySlot="authenticated" 
        let-user="user"
        let-signOut="signOut"
      >
        <ion-router-outlet></ion-router-outlet> 
      </ng-template>
    </amplify-authenticator>
</ion-app>

The one problem I have with this is that the tab bar does not render on mobile browser e.g. iPhone Chrome or Safari browser. It will show on desktop browser without any issues and oddly enough if I turn the phone horizontally it will show the tab bar but not vertically.

iPhone:

Web (Desktop):

web

If I changed the app.component.html code to this, it will work:

<ion-app>
    <amplify-authenticator [socialProviders]="['apple', 'google']">
      <ng-template amplifySlot="header">
        <div style="padding: var(--amplify-space-large); text-align: center">
          <img
            class="amplify-image"
            alt="Amplify logo"
            src="https://docs.amplify.aws/assets/logo-dark.svg"
          />
        </div>
        <ion-router-outlet></ion-router-outlet> 
      </ng-template>
      
    </amplify-authenticator>
</ion-app>

However, the issue with this is, I will never see the login page and can access the app unauthenticated.

Any ideas on how to get this working correctly and to have the tab bar show on mobile browsers?

iamthestreets
  • 733
  • 1
  • 15
  • 38

1 Answers1

0

I would use a service to check if user is authenticated and redirecting user based on this condition.

Something like:

import { AuthenticatorService } from '@aws-amplify/ui-angular';

@Injectable({
  providedIn: 'root'
})
export class AuthService {

  constructor(private authenticator: AuthenticatorService) { }

  public isAuthenticated = () => this.authenticator?.authenticated;
}

Then you may implement a CanActivate guard:

@Injectable({
  providedIn: 'root'
})
export class CanActivateLoginGuard implements CanActivate {

  constructor(
    private authService: AuthService) {
  }

  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> {

    return this.authService.isAuthenticated() || this.router.createUrlTree(['/login']);
  }
}

Now you should use Angular Router properly in order to check if user can access the home page, something like:

const routes: Routes = [
  {
    path: '',
    canActivate: [CanActivateLoginGuard],
    loadChildren: () => import('./pages/tabs/tabs.module').then(m => m.TabsPageModule)
  },
  {
    path: 'login',
    loadChildren: () => import('./pages/login/login.module').then(m => m.LoginPageModule)
  }
];

@NgModule({
  imports: [
    RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })
  ],
  exports: [RouterModule]
})
export class AppRoutingModule { }

And finally TabsPageRoutingModule:

const routes: Routes = [
  {
    path: 'tabs',
    component: TabsPage,
    children: [
      {
        path: 'home',
        loadChildren: () => import('../home/home.module').then(m => m.HomePageModule)
      }
  {
    path: '',
    redirectTo: '/tabs/home',
    pathMatch: 'full'
  }
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule],
})
export class TabsPageRoutingModule {}
Ernesto Schiavo
  • 1,020
  • 2
  • 12
  • 33
  • My problem is not with routing. My problem is simply visual. I can not visually see the bottom tab bar when viewing on my mobile device. I can see the tab bar when viewing on my pc’s web browser. I have tried different padding’s, margins, safe zones, etc. nothing works. – iamthestreets Jun 17 '22 at 00:40
  • I understand but I suggest to change approach. Using a `CanActivateGuard` you can redirect user to homepage if authenticated, to login page otherwise. In that case it's simple to show the login / registration form on login page and others (including tab bar) on authenticated pages. This way you won't have the problem – Ernesto Schiavo Jun 17 '22 at 07:16
  • I understand now. I am getting this error in the `AuthService`: Property 'authenticated' does not exist on type 'AuthenticatorService' – iamthestreets Jun 17 '22 at 17:53
  • Also, if i implement this, does this mean i will need to handle signIn and signUp on my own instead of using ``? – iamthestreets Jun 17 '22 at 18:11
  • I was able to implement a solution using routing and guards but unfortunately this did not fix the issue. – iamthestreets Jun 20 '22 at 21:02
  • Then I think there is something wrong with the tabbar... – Ernesto Schiavo Jun 21 '22 at 10:32