Vue Setup Facebook Login in Nuxt + Firebase

Vue Setup Facebook Login in Nuxt + Firebase
Vue Setup Facebook Login in Nuxt + Firebase , One of the challenge of using Firebase authentication on SSR architect (Nuxt.js) is understanding how the data flow goes in deep

This story is inspired by another tutorial written by David, I strongly recommend to read of his Story and Github. Most of the content here are based on his work.

Best way to understand what happening in programing, is looking at the code. This pull-request is setting up Facebook and Google accounts login, build on top of the Vue template generated by vue-cli vuetifyjs/nuxt.

If you are looking for a ready cooked boilerplate for quick development, then clone this Repo, and set up the Firebase key, secrete in the config here ./services/fireinit.js.

Firebase have done a very great job in authentication for the developers, great SDK, and great integration. Facebook, Google could be implemented just within few clicks on browser.

However, data in Firebase Web application is mostly happened on the browser, think about that, all the Facebook login pages, login popup is all about opening pages and redirecting pages, nothing to do with our awesome APIs. One of the challenge of using Firebase authentication on SSR architect (Nuxt.js) is understanding how the data flow goes in deep, carrying data from the browser to web server is a bit complex, topics such as Vuex, middleware, express-middleware and nuxt-plugin can not miss.

1- Setup Facebook Page and Facebook Application

2- Connecting Firebase and Facebook

Authorize Firebase domain via your Firebase Console in Authentication settings via your Firebase Console under the Sign-In Methods Tab.

3. Set up spa mode in nuxt.config.js

Using SPA mode with Nuxt.js which is implemented by setting the option mode: 'spa' in nuxt.config.js.

4. Set up Firebase service in Nuxt

To setup/initialize the firebase app with credentials, I created services/fireinit.js.

5. Handling User Authentication State

Firebase provides onAuthStateChanged to handle user state. I created plugins/fireauth.js to setup this functionality

6. Protecting Authenticated Routes

Scenario 1: A user is already signed-in and the next route is /login

Redirect to _/admin_

Scenario 2: There is not a user signed-in and the next route is /admin

Redirect to _/login_

We will tell Nuxt.js to use this file by declaring it in nuxt.config.js as follows:

With all of this in place, we are able to use Firebase Authentication with Nuxt.js to protect the /admin route

7. Set up component FacebookLogin.vue

This component is triggering action signInWithFacebook , by calling this.$store.dispatch

8. Setup action and store for Facebook Login Action

Done

Basically, everything is setup within the above 8 steps.

When you open the page localhost:3000/admin, you are redirected to localhost:3000/login to require login

When you open the page _localhost:3000/admin_, you are redirected to _localhost:3000/login to_ require login

localhost:3000/admin

Reference:

Learn Web Development Using VueJS

Learn by Doing: Vue JS 2.0 the Right Way

Vue.js 2 Essentials: Build Your First Vue App

Getting started with Vuejs for development

Horizontal Feed Component with Sass and VueJs 2

Learn Vue 1 JS introduction to simple reactive JavaScript

Suggest:

Web Development Tutorial - JavaScript, HTML, CSS

E-Commerce JavaScript Tutorial - Shopping Cart from Scratch

Javascript Project Tutorial: Budget App

Firebase - Ultimate Beginner's Guide

Web Development Trends 2020

JavaScript Programming Tutorial Full Course for Beginners