3️⃣Step 3 - Integrating Eartho into your server

1. Before we start, you need to import Eartho into your app.

2. Integrating Eartho into your backend environment with code

If you use Eartho One with an app or site that communicates with a backend server, you can protect your routes easily with our SDK as well. After your users finish logging in with Eartho, they'll be returned to your application at the callback route.

On the client side

Every request that your client makes to your server, send with the ID token on the client side

const idToken = await earthoOne.getIdToken();
const response = await fetch('https://api.example.com/products', {
headers: {
      Authorization: `Bearer ${idToken}`
    }
  });

On the backend side

Check and verify the integrity of the ID token and use it to protect the API routes that you want to protect.

Step 1: Installing Required Packages

npm install @eartho/one-server-node

Step 2: Protecting Your Routes

Use the Eartho SDK to protect routes that require user authentication. For example, to protect a route that fetches user data, you would verify the user's ID token sent with the request.

const {EarthoOne} = require('@eartho/one-server-node');

const serverEarthoOneOptions = {
  clientId: "client id",
  clientSecret: "secret key"
}
const serverEarthoOne = new EarthoOne(serverEarthoOneOptions);

//Or check by yourself
getEarthoOne()
  .getVerifiedUser(idToken)
  .then((decodedToken) => {
    const uid = decodedToken.uid;
    const displayName = decodedToken.displayName;
    const email = decodedToken.email;
    const photoURL = decodedToken.photoURL;
// ...
  })
  .catch((error) => {
    // Handle error
  });

Last updated