Firebase Authentication provides the backend services that are easy-to-use SDKs and ready-made UI libraries to authenticate users to your app.
Setup: First we need to create a Firebase Project, head over to firebase Console and create a new project.
I am going to name it SignInWithGoogle
Click on the web to create a web app.
Go to Firebase Settings>Service Account >Generate a new Key. This Key should be remain private, It is advised to keep this in environment variables.
Now go to Authentication tab and turn on Sign in with Google.
Now Create a new project having success.html (with a simple anchor tag, directing to “/ ” root ) and login.html [leave a blank division with id “firebaseui-auth-container”, the place where Firebase UI will be Initialized]
Change console directory to root of your project type by using the following commands in the console
$npm init $npm install express firebase-admin cookie-parser https fs
Note: The last two packages are only needed if you wanna save cookies in local-host, however, if you will be running a backend on https then there is no need.
Here is app.js file, If you look it closely, you will find that there is no port listening to our request. This is where we need those two node modules.
Most browsers does’t allow you to save cookies in local host, that’s why we will setup HTTPS connection for our localhost.
Make sure you are in root directory of project.
Open bash and type
$openssl req -nodes -new -x509 -keyout server.key -out server.cert
Two files (server.key and server.cert) will be generated.
Add the following code to app.js file-
Load this script before body tag in login page
Load this script in login page after body tag (fill the config data that you got from firebase console).
Now hit save and run the command $node app.js
Now go to https://localhost:3000/ and sign in, then close the tab or browser and type https://localhost:3000/success, you will see that you are not redirected to the sign-in page again, instead, you are taken to the success page.
Note: Here it didn’t ask me to select which account to login with the app because I was signed in with only one account, in-case you have signed in with multiple accounts, it will ask you to choose an account to proceed with.
Download My Completed project in case of any error you face or write in the comment
- Nodejs | authentication using Passportjs and passport-local-mongoose
- PHP | Sessions
- Nodejs | Automatic restart NodeJs server with nodemon
- Remove a cookie using PHP
- Express Cookie-Parser - Signed and Unsigned Cookies
- How to create and read value from cookie ?
- HTML | DOM cookie Property
- HTTP headers | Cookie
- HTTP headers | Set-Cookie
- Express.js res.cookie() Function
- How to access HTTP Cookie in Node.js ?
- How to implement Google Login in your Web app with Firebase ?
- Saving What Saves Our Passwords – Two-Factor Authentication
- Broken Authentication Vulnerability
- Signup Form Using Nodejs and MongoDB
- NodeJs CRUD Operations Using Mongoose and MongoDB Atlas
- Nodejs - Connect MongoDB with Node app using MongooseJS
- Cowsay in Nodejs using Requests library
- Nodejs | Web Crawling using Cheerio
- Generating Lighthouse Scores using NodeJS
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : nidhi_biet