You have unlimited access as a PRO member
You are receiving a free preview of 3 lessons
Your free preview as expired - please upgrade to PRO
Contents
Recent Posts
- Object Oriented Programming With TypeScript
- Angular Elements Advanced Techniques
- TypeScript - the Basics
- The Real State of JavaScript 2018
- Cloud Scheduler for Firebase Functions
- Testing Firestore Security Rules With the Emulator
- How to Use Git and Github
- Infinite Virtual Scroll With the Angular CDK
- Build a Group Chat With Firestore
- Async Await Pro Tips
Ngrx With Firebase Auth Google OAuth Login
Episode 46 written by Jeff DelaneyIn the third lesson installment of Ngrx, we are going to add Firebase Auth to our basic redux app. It will give our users the ability to authenticate via Google OAuth and logout, while managing the side effects of these actions with @ngrx/effects.
If you’re brand new to Ngrx, make sure to check out these related lessons.
Update App Module
Before doing anything, let’s update the app module with the requried files. Here’s a partial glimpse of the NgModule that includes only the pieces needed for Ngrx and AngularFire2. We will be using the AngularFireAuthModule provided by AngularFire2, but these principles could be used with the regular Firebase SDK also.
@NgModule({ |
Modeling User Data
We are going to keep our users as simple as possible - just 2 required attributes (uid, displayName) and 2 optional ones (loading, error).
You can always determine if a user is logged in or not based on their uid. If the uid is empty, you know the user is NOT authenticated.
export interface IUser { |
User Authentication Actions in Redux
We are going to reuse actions when possible - specifically AUTHENTICATED
and NOT_AUTHENTICATED
. Depending on the complexity of app login process, you many need more actions to handle other auth login methods and processes.
user.actions.ts
import { Action } from '@ngrx/store'; |
User Reducer
The reducer function will combine update the user details on the data store based on changes to the Firebase auth state.
user.reducer.ts
We can use our user class to create a default GUEST
user with a null
uid. The reducer will also toggle the loading state for actions that await asynchronous data.
import * as userActions from '../actions/user.actions'; |
User Effects
Now we can get to the fun part - Ngrx Effects. There are three basic problems we need to solve.
- Determine if user is logged in or not
- Login/Signup with Google OAuth
- Logout
Here’s a visual flow of the process using the Redux Chrome plugin.
0. Basic Effects Skeleton
First, let’s import the necessary files and decorate our effects class with @Injectable
.
import { Injectable } from '@angular/core'; |
1. Get User Effect
First, we need a way to get the user from Firebase if they are already authenticated. We don’t want the user to have to re-authenticate every time they visit the app. This effect will check the auth state in firebase, then update the data store accordingly.
@Effect() |
2. Login User with Google OAuth Effect
Now we need a way for new users to signin/signup. Firebase makes this really easy with Google OAuth. The magic happens in googleLogin
method, which triggers the OAuth popup window. Once authenticated, we can simple re-trigger the GET_USER
action to update the data store.
@Effect() |
3. Logout User Effect
Logout is basically identical to login, except the NOT_AUTHENTICATED
action is triggered.
@Effect() |
Logging in on the Frontend
With all of the Ngrx login in place, it’s time update the app component to actually give the user a way to login.
app.component.ts
First, add the user to the AppState
interface. Second, dispatch the the GET_USER
action during the OnInit
lifecycle hook to determine if the user is already logged in. Last, create a couple event handlers so the user can click login and logout buttons.
import { Component, OnInit } from '@angular/core'; |
app.component.html
And finally, let’s put this together in the HTML.
<div *ngIf="user$ | async as user"> |
Up Next
Let me know what Ngrx topics you want to see covered next. I hope to cover Ngrx file uploads and routing in the near future.