This article describes how to do the following 3 points using Firebase Auth.
- Sign in with your Google account
- Sign out
- Get user ID
The related resources are shown below.
The workflow is shown below.
- Creating a Firebase project
- Creating a Firebase app
- Enabling Authentication
- Coding preparation
- Coding
- Operation check
Access Firebase Web Console and click "Add project". The "Add project" screen will be displayed. Enter the project name (e.g. firebase-auth-google-(8-digit date)).
When the "Google Analytics" screen is displayed, uncheck "Enable Google Analytics for this project" and then click the "Create" button.
Click the "Continue" button when creating the project have finished.
The project overview page for the created project is displayed.
Click the "Web" button next to iOS or Android under "Get started by adding Firebase to your app".
"Add Firebase to your web app"
When the "Add Firebase to your web app" screen appears, enter your app's nickname (eg firebase-auth-google-(8-digit date)-app) and then click the "Register app" button. At this time, do not check "Also set up Firebase Hosting for this app".
When the registration of the application is completed, the sample code including firebaseConfig will be displayed, so copy the contents of firebaseConfig for later steps. It is convenient to use the copy button.
When you have copyed the contents of firebase Config, click the "x" button at the top left of the screen to close the "Add Firebase to your web app" screen.
Click "Authentication" in the menu on the left side of the page to go to the Authentication page.
Activate Authentication by clicking the "Get started" button included on the Authentication page.
Once Authentication has been activated, click the "Google" button in the Sign-in providers section.
When the settings screen appears, click the "Enable" toggle button to enable the Google sign-in provider, then select "Project support email" and then click the "Save" button.
Run the following commands to prepare for coding.
mkdir firebase-auth-google
cd firebase-auth-google
npm init -y
npm install --save firebase
npm install --save-dev webpack webpack-cli webpack-dev-server
touch app.js firebase-config.json index.html webpack.config.js
Open webpack.config.js in the editor and enter the following content.
Click to go to webpack.config.js
Open index.html in the editor and enter the following content.
Open firebase-config.json in the editor and enter the copied content.
Click to go to firebase-config.json
Open app.js in the editor and enter the following content.
The points are as follows:
- Load firebase-config using fetch function.
- Call the initializeApp function to initialize the application.
- Call the getAuth function to initialize Authentication.
- Use the onAuthStateChanged function to register an event handler that will be called when the authentication state changes. In this example, in addition to showing / hiding the section contained in the web page depending on whether you are signed in, we are accessing user.uid to get the user ID.
- When the user clicks the "Sign in" button (buttonSignin), call the signInWithRedirect function to go to the Google login page.
- When the user clicks the "Sign out" button (buttonSignout), call the signOut function to sign out.
Run the following command in the terminal to start the server.
npx webpack serve
Go to http://localhost:8080/ in the browser and make sure you only see the sign in / sign up section.
Check that clicking the "Sign in" button will take you to the Google login page.
After signing in, you will be returned to the app page and check that your user ID and "Sign out" button are displayed.
Check that clicking the "Sign out" button hides the user information and sign out section and shows the sign in section.
I was wondering if an authorization code was passed using the query parameter when returning from the Google login page to the application page, but it seems that it is not so and I am curious about how it works. I would appreciate your guidance if you have any details, and I welcome your comments on other opinions and impressions. Thank you for reading!