Fireship · @fireship

Angular Ngrx with Firebase Google OAuth User Authentication

Uploaded September 6, 2017 · Length 8:40

Angular Ngrx with Firebase Google OAuth User Authentication

Summary

Implement Google OAuth user authentication with Firebase in Angular using Ngrx for state management.

  • angular ngrx firebase authentication
  • google oauth angular
  • firebase angular user authentication
  • ngrx state management

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    User authentication involves some of the most critical data that a JavaScript app will have

  2. 0:04

    to manage.

  3. 0:05

    In this lesson, we're going to implement Google OAuth via Firebase, but we're going to do

  4. 0:09

    it using NGRX and the one-way data flow of Redux.

  5. 0:13

    I'd like to point out that this video is going to cover a lot of code very quickly,

  6. 0:17

    so if you're brand new to NGRX, check out some of the links in the description for an

  7. 0:21

    easier introduction into Redux.

  8. 0:23

    The feature we're building today will check if a user signed in, and if not, they can sign

  9. 0:27

    in via Google and then sign out.

  10. 0:29

    And all this will be kept in a single data store through NGRX.

  11. 0:33

    The first step is to add the necessary files to the NG module.

  12. 0:37

    We're going to need AngularFire 2 along with NGRX Store and Effects.

  13. 0:42

    Then we're going to build our own User Effects and User Reducer.

  14. 0:46

    So we add everything to the import section and we're good to go.

  15. 0:50

    The next step is to model our user data.

  16. 0:53

    We're going to keep the user interface as simple as possible.

  17. 0:55

    It'll just have a user ID display name and then an optional property for loading and an optional property for error

  18. 1:02

    Then we'll create a user class that uses these properties and build it by passing it a user ID and display name that we get back from

  19. 1:09

    Firebase

  20. 1:10

    From here we can start building our user actions

  21. 1:13

    The first and probably most important action is get user which will determine if the user is currently authenticated in Firebase

  22. 1:20

    If they are authenticated then we'll run an authenticated action that will update

  23. 1:25

    the current user data on the data store. If they're not authenticated then we'll

  24. 1:29

    run a not authenticated action that will just set a default guest user on the

  25. 1:34

    data store. Then to log in or sign up user we'll set up a Google login action

  26. 1:39

    and we'll also give the user the ability to log out and we'll set one more action

  27. 1:44

    for auth error just to handle any errors that might come up. Now each of these

  28. 1:49

    actions will get its own class that implements the action interface. It will have a read-only

  29. 1:54

    type that corresponds to the constant above, and the constructor allows it to handle a data

  30. 1:58

    payload if necessary. To keep this part brief, we're just going to follow the same exact