0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ionic + Firebase カレンダーアプリ その7 ログインチェック

Last updated at Posted at 2019-11-16

完成イメージ

はじめに

環境は以下の通り
・ionic4
・Nativeとの橋渡しにはcapacitorを使用
・カレンダーについてはionic2-calendarというライブラリーを使用

:point_right:前回の記事は[こちら]
その5 ユーザー作成と認証
その6 ログアウト処理とサイドバー

概要

前回と前々回からログイン等の認証関係の実装をやってきた。
だが、ログインしている人以外がアクセスできると厄介なので、ログインしていないユーザーを強制的にログイン画面にリダイレクトさせたい。

今回はログインチェックを実装したい。

AngularのGuardを作成

ionic g guard auth/auth

Guardというのはざっくり言うと、ログインしていない人はアクセスできないようにする機能だ。

これでauthフォルダの中身はこうなった。
スクリーンショット 2019-11-16 15.20.19.jpg

auth.guard.ts
import { AuthService } from './auth.service';
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router';
import { Observable } from 'rxjs';
import { map, take, tap } from 'rxjs/operators';
import { AngularFireAuth } from '@angular/fire/auth';

@Injectable({
  providedIn: 'root'
})
export class AuthGuard implements CanActivate {
  constructor(
    private afAuth: AngularFireAuth,
    private router: Router,
    private authService: AuthService
  ) { }

  canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ): Observable<boolean> | Promise<boolean> | boolean {
    return this.authService.user$.pipe(
      take(1),
      map(user => !!user),
      tap(loggedIn => {
        if (!loggedIn) {
          console.log('ログインしていない');
          this.router.navigateByUrl('/auth');
        }
      })
    );
  }
}

このGuardを適応させるページをapp-outing.module.tsで設定

app-routing.module.ts
import { AuthGuard } from './auth/auth.guard';
import { NgModule } from '@angular/core';
import { PreloadAllModules, RouterModule, Routes } from '@angular/router';

const routes: Routes = [
  { path: '', redirectTo: 'home', pathMatch: 'full' },
  {
    path: 'home',
    loadChildren: './home/home.module#HomePageModule',
    canActivate: [AuthGuard] // これがあるページはログインしていないとアクセスできない
  },
  { path: 'day', loadChildren: './day/day.module#DayPageModule' },
  { path: 'day/:id', loadChildren: './day/day.module#DayPageModule' },
  { path: 'auth', loadChildren: './auth/auth.module#AuthPageModule' },
  { path: 'signup', loadChildren: './signup/signup.module#SignupPageModule' },
];

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

最後に

続き:point_down:
その8 ログインユーザーのデータを取得

各ストアで配信されてます。
興味があれば使ってみてください。:relieved:

Apple Store

Google Play

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?