- Tech: React + Typescript + NodeJS + Express + Google Auth
Create a simple React App to Login with Google Account.
- Save login data in
localStorage
to remember user login - Create backend API using
node
andexpress
to authenticate user - Publish on heroku and test it on Production
- create an App on Google Cloud Platform
- using React & Bootstrap to build Frontend
- using NodeJS & Express to build Backend
- create an Account on Heroku and Publish
- Login to Google
- Go to https://console.cloud.google.com
- Create a project & Configure consent screen
- User Type: Extenal
- Add
Project Name
andDeveloper Contact
- Create an
API Credential
withOauth Client ID
, selectingWeb Application
- add Authorized JavaScript origins for
Web Application
- add Authorized JavaScript origins for
- Craete
.env
file and save it asREACT_APP_CLIENT_ID
- crate a React project with typescript tample
npx create-react-app react-google-login --template typescript
- import packages (bootstrap)
npm install react-bootstrap bootstrap --save
- edit App.js
- use React Bootstrap to build layout
- define loginData state hook, read data from localStorage
- check loginData and render content
- if loginData is null render
GoogleLogin
component - if loginData isn't null render you are logged in message
- if loginData is null render
- implement function
handleSuccess
,handleLogout
,handleFailure
- install packages
npm install -save express dotenv google-auth-library
- create
server/index.js
file - define
dotenv
andclient
inserver/index.js
const dotenv = require('dotenv'); const path = require('path'); const { OAuth2Client } = require('google-auth-library');
- config
express
serverconst express = require('express'); ... use express.json() define app.post('/api/google-auth', ... define app.listen
- *need to add
proxy
inpackage.json
, it allows FE request to push data into backend"proxy": "http://127.0.0.1:5000/"
REACT_APP_CLIENT_ID=googleid-uuid.apps.googleusercontent.com
- Create Procfile and add
web: node server/index.js
- Add config in backend server
app.use(express.static(path.join(__dirname, '..', 'build'))); app.get('*', (req, res) => res.sendFile(path.join(__dirname, '..', 'build', 'index.html')) );
- Publish to Github
- Create an account on
heroku
and connect it with Github repo - Config Vars https://dashboard.heroku.com/apps/{app-name}/settings
- key: REACT_APP_CLIENT_ID
- value: Google app #{client_id}
- Manual deploy: Deploy a GitHub branch
Copyright © 2022-01 Encore Shao. See LICENSE for details.