vuruniverse.blogg.se

React node js tutorial
React node js tutorial









React is used for building frontend apps that can communicate with the backend server. This will be handled in the useEffect hook with a function called handleTokenFromQueryParams() and logs the user in.React is a JavaScript library used to build user interfaces and develop single-page applications using reusable UI components. This creates the link in your server, sends the auth link back to the client, and redirects the React app to the Google auth link on success.Īfter authenticating with Google, another link should be sent back containing the access token and the refresh token. On App.js, start authentication with a button Login that calls the function createGoogleAuthLink. Handle expired and non expired tokens when sending API requests.Store token credentials on sessionStorage.Login and get credentials back from Query Params.There’s basically 3 steps in the client app. That’s all the project structure setup! React Client: Mkdir server & touch server/index.js & touch server/.env & cd server & npm init -y & yarn add express dotenv googleapis cors body-parser Npx create-react-app client & touch client/src/tokens.js & touch client/src/calendarApi.js Mkdir google-auth-tokens & cd google-auth-tokens You can copy the following commands in order to setup the project. Then I installed the express, googleapis, cors, and dotenv dependencies on the server.

react node js tutorial

I set up a folder called google-auth-tokens, then created a React app inside called client, and a Node.js app called server.

  • Create a new Node.js server with Express.js.
  • Then check if the token is expired every time we make a request to a Google API, and handle it accordingly.

    react node js tutorial react node js tutorial

    You can also store this on your user database file. We will authenticate with Google on a Node.js server, send the access_token and refresh_token to the user on the front end after the authentication is done, and store the refresh_token on localStorage. After that, the token expires and you need a new one. That means that if you authenticate with Google, get an access token, and use it to access your Google Calendar API, you will only be able to use that access token for 1 hour. Google access tokens expire after 1 hour.











    React node js tutorial