Showing posts with label firebase. Show all posts
Showing posts with label firebase. Show all posts

Monday, February 26, 2018

How to Configure Firebase Cloud Storage bucket for cross-origin access (CORS)

If you have ever tried to download data stored in Firebase Cloud Storage directly in your browser you may have come across this error:

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '' is therefore not allowed access

In order to fix this error you will need to run through a couple of steps to configure Firebase Cloud Storage for CORS. The following steps can be taken in order to do this:

GSUtil

There are at least two approaches to configure the cloud storage with GSUtil:

Cloud shell within Google Cloud Platform

You can manage Compute Engine resources like virtual machine instances using the gcloud command-line tool in a Cloud Shell session.
  • The following instructions outline how to start the session
  • Google Cloud Shell is free for customers of Google Cloud Platform which isn't free

Install GSUtil

Gsutil is a tool that enables you to access Google Cloud Storage from the command-line and is needed to configure Firebase Cloud Storage for CORS.
  • If you are behind a corporate proxy or firewall, the tool may not be able to access the internet with its default settings during installation. In this case you could use one of the self-contained versioned archives as detailed here.
  • Once you have successfully run gcloud init and been authenticated you can configure CORS
  • With the cors.json file created run the following command to deploy the configuration to your storage: 
    • bin/gsutil.cmd -d cors set  gs://

That should be all you need to do. 

Monday, February 12, 2018

Vue.js, NUXT.js, Vuex, Firebase persisted authentication, Axios

I created the following application to demonstrate how to handle authentication with Firebase within a Nuxt (Vue) application. I wanted to make sure the application stayed logged in as long as the Firebase authentication token hadn't expired. User should not be logged out when:
  • The browser is refreshed
  • The browser is closed and restarted and the Firebase token is still valid (persisted sessions)
You can grab the example from Github and you can see below for a brief outline of the steps I took to setup configure the application:

Firebase Project

  • Signup / Login to the Firebase console and Create a new project by clicking the Add project icon and following the instructions for creating your project. 
  • Setup authentication within your new Firebase project by clicking on the authentication menu in the left navigation bar. Click on the sign-in method tab and select the Email/Password provider from the list of sign-in methods. Enable it and click save.

Create Vue+NUXT application

Install the necessary dependencies

  • yarn add @nuxtjs/axios body-parser express express-session firebase jwt-decode vuex  

Configure and initialise Firebase

  • Add Firebase configuration settings by creating a development (and production) configuration file to your application and adding your Firebase  project settings to it
  • Locate your Firebase application configuration settings by logging into your Firebase console and searching for: Add Firebase to you Web Application
  • Create a file that will export the instance of your Firebase application
  • The example application also makes use query the Firebase realtime database. If you are wanting to run the example application be sure to create your own database in your Firebase project

Create express server API 

  • Create an express server API to handle the login and logout posts for the user. On receiving a post request from the client the user ID is stored in the session and the access token is stored in a cookie
  • Modify your nuxt.config.js file so that it includes a section for the serverMiddleware and also has @nuxtjs/axios module defined

Create Vuex actions and mutations

  • Add Vuex to your application and choose between the classic or module mode. I opted for the module mode
  • Inside your root store file define the nuxtServerInit action. This action is used to save the currently logged in user to the session
  • Inside your stores users module add the necessary getters, actions and mutations needed to save the user ID to the store. We include our login and logout actions here that use axios to send a post request to the server 

Create check authentication middleware

  • Create a check authentication middleware action and add the necessary configuration to nuxt.config.js so that it will be called for every route
  • The middleware component will check to see if the user is stored in the session or if the access token is found in the cookie. If it finds a match the store is initialised with the user ID. 

Create protected resource

  • Create a page that will only be accessible if a user has successfully been authenticated
  • Create the authenticated middleware action that will be called for every protected page. This action checks to see if a user is authenticated, if not the user is redirected to the sign in page

Create SignIn and SignUp pages

  • Create a page to allow the user to sign in to the application
  • Create a page to allow a user to register as a new user to the application

Run your Application

  • npm run dev
  • If all was configured successfully you should be able to sign up, sign in, reload page and still be signed in, close browser and reopen browser and still be logged in (as long as access token is still valid) and finally be able to sign out of the application

Thursday, November 2, 2017

Storytellers - a Vue, NUXT, SSR, Vuetify, Firebase [Auth, Firestore, Storage, Functions] application

I am trying to develop an web application from the ground up in order for me to really learn and understand modern web development. I wrote a blog entry when I first started out and I have continued to work on the sample application.

There is very little you can do with the application but it covers a lot of ground already. I have managed to covers all the tiers I am trying to learn:


Hopefully, if I have time, I can add more functionality to it so that it looks and feels like a proper functional application.

What is the web application about? Well at the moment it allows a user to upload images to Firebase storage and displays thumbnails on the home screen. Sounds amazing. Ultimately I would like to 'expand' on that :-) and end up with a community driven application providing a platform for the creative and artistic to share and collaborate their stories and ideas with one another.

Yeah so arguably that idea is pointless and ... yeah it might be ... but it allows me to try many different things and covers many different technology stacks so I am going to learn a lot by doing this.

I don't expect anyone to be interested in "collaborating" with me in development but I want to keep this opensource and invite anyone out there who is keen and able to contribute to the development of this project. Feel free to fork the repository, raise issues and submit pull requests.

Monday, October 23, 2017

Vue.js, NUXT.js, Vuetify.js, Firebase authentication

Please note I have a more recent blog entry regarding authentication with Firebase + Nuxt that not only uses the session to store the user ID but a cookie to store the access token. It also makes use of the Nuxt Axios module to make server posts on login and logoff. 

I previously wrote a blog detailing a few steps to get a sample Vue + Vuetify + Nuxt application running on Heroku cloud platform. In this blog I want to share a sample application I did that has all of the above but also Firebase authentication.

Framework Overview

In a nutshell this is what the example application covers:

Vue.js

The sample application is built using the Vue.js framework, a progressive framework for building user interfaces.

NUXT.js

It is also built on top of NUXT.js, a framework for creating Universal Vue.js Applications. I chose NUXT.js because I wanted to build a server-rendered Vue application. I didn't need to use NUXT, there is an in-depth guide I could have followed but NUXT is supposed to make building universal web application easier. That sounds good to me.

Vuetify

For the UI components and page layouts I make use of Vuetify.js, a semantic component framework for Vue.js. Why Vuetify and not many other options that are available, like what can be seen in this comparison blog post? A lot of these frameworks are new to me, Vuetify seemed like a popular choice with a stable backing and it supported server side rendering with NUXT.

Firebase Authentication 

The application integrates with Firebase in order to authenticate users.

Vue CLI Templates

Getting familiar with NUXT and Vuetify I went over the following Vue CLI templates:
They were both very useful and instrumental in getting an initial understanding into their framework. But things started to get a little less simple and a lot more fuzzy when I started to integrate Firebase authentication with my app. Of course there is a lot I need to learn (and still need to learn). GitHub issues for both these projects were quite helpful in searching for answers to questions I had:

The problem

I needed a way to authenticate users through Firebase authentication and to handle state on the server as well as the client. Firebase web authentication is run on the client side. I wanted to be able to logout a user when the user logs out and also keep a user logged in when the user refreshes their browser. There seems to be a number of different approaches to this problem and I settled on the following:
  1. Configure the application to run an express server so that I could handle server POSTS and  GETS if needs be (example sign in and sign out post) and store any information in the session in case I need to access it again from the server. The following link on the NUXT website helped me configure my custom server: https://nuxtjs.org/examples/auth-routes/. Oh and yeah it has an example of an auth use case except it didn't quite fit my scenario.
  2. Configure a pages middleware function that gets called for every secure page, not for sign in and sign up pages. This function will be responsible to see if the user is authenticated or not by either checking the session (server) or analysing the firebaseApp variable to see if the current user exists or not. 
The above means I don't store the logged in user in any store like Vuex, Firebase seems to store the user in localStorage in a variable called "authuser". That way it can keep the user signed in.

Sample Code

The sample code for the project I did can be found here:
Once cloned you should be able to change into the ui folder and run the following commands to test it out:
  • npm install
  • npm run build
  • npm run dev-custom-server
You could also just examine the code...maybe there is something useful there you will find. Like I said at the beginning this is all very new to me so most likely there are a lot of things wrong with the way I have done it. I hope not. But nevertheless I learned a lot.