wandanli / plantwiki

0 stars 0 forks source link

How to request a client-side token (JWT token) #1

Closed wandanli closed 3 years ago

wandanli commented 3 years ago

Trefle doc:

If you need to perform client-side requests you will have to request a client-side token from your own backend and get a JWT token in return. This token will be usable on the client side. This call needs your secret access token and the url of the website that the client side requests will come from.

wandanli commented 3 years ago

stackoverflow origin must be your website url, eg: http://localhost:3000/

requst JWT token 
https://trefle.io/api/auth/claim?token=YOUR-TOKEN&origin=YOUR-WEBSITE-URL
  1. Create a backend server.js to get the JWT token (can use dependencies: "express", "node-fetch")

    trefle example code

  2. Deploy the backend app to glitch. Now we can get the JWT by open the URL on browse

    Deploy to glitch

    The response header mush contain the

    "Access-Control-Allow-Origin": "*",
    "Access-Control-Allow-Credentials":true,
    "Access-Control-Allow-Methods": "GET, PATCH, POST, PUT, DELETE, OPTIONS"

    The origin parameter must be your website URL that you call the request, eg: http://localhost:3000/

  3. On react app, fetch the server side URL to get the JWT token, and fetch data from Trefle

wandanli commented 3 years ago

after deploying, need to chang the origin URL:

const originUrl = request.headers.origin;
  const params = {
    origin: originUrl,
    token: process.env.SECRET,
  };