簡體   English   中英

Node.js-如何使用訪問/身份驗證令牌?

[英]Node.js - How to use access / auth tokens?

我已經構建了第一個應該安裝在Shopify商店中的Node.js應用程序。 如果您想查看我的實際代碼(app.js)的外觀,可以在此處查看 這真的很基礎,因此通讀不會困難。

我知道如何對應用程序的安裝進行身份驗證 (按照Shopify的說明),但我不知道如何使用成功安裝后提供給我的永久access token所有后續請求進行身份驗證

在后續請求中,我指的是渲染應用程序或安裝應用程序的請求,即使該應用程序已安裝。

現在,我商店的名稱(唯一的)以及Shopify發送給我的永久令牌存儲​​在數據庫中 但是我不知道這是否必要 如果我沒記錯的話, 只是使用瀏覽器的會話就可以了? 但是我該怎么做呢? 以及每當有請求通過時,如何使用此令牌檢查它是否有效?

感謝您的幫助/建議!

下面的代碼有點像我的實際代碼的樣子,以便讓您了解我的問題所在:

db.once('open', function(callback)
{  
   app.get('/', function (req, res)
   {
      var name = getNameFrom(req);

      if (existsInDB(name) && tokenExistsInDBfor(name))
      {
         res.redirect('/render');

         /*
            Is checking that the shop (along with a permanent token)
            exists in my DB enough ?
            Shouldn't I check whether the current request comes with 
            a token that is equal to the one in my DB ?
            What if the token received with this request is different       
            from the one stored in my DB ?
         */

      }
      else res.redirect('/auth');
   });

   app.get('/auth', function (req, res)
   {    
      if (authenticated(req))
      {
          var token = getPermanentToken(); 
          storeItInDB(nameFrom(req), token);
          res.redirect('/render');

          /*
            aren't I supposed to do anything more 
            with the token I've received ? send it
            back/store it in the browser session as well maybe?
            is storing it in the db necessary ?
          */
      }
   }); 

   app.get('/render', function (req, res)
   {   
      /*
      How do I check that this request is coming 
      from an authorised shop that has the necessary token ?
      Simply checking my DB will not do 
      because there might be some inconsistency correct ?
      */

      res.sendFile(*file that will build app on the client*);
   });
});

從Shopify獲取access token是一次性的過程。

access tokenshop's name保存在數據庫中,並根據某種算法生成並保存“身份驗證令牌”。 將生成的身份驗證令牌返回給客戶端。 確保客戶端在每個請求中發送此身份驗證令牌。

現在,當客戶端訪問您的服務器時,請驗證身份驗證令牌; 驗證后,請使用適當的“訪問令牌”和商店名稱調用Shopify API。

身份驗證流程如下:

  • 從Shopify獲取訪問令牌
  • 生成令牌(我指這是驗證令牌)的Shopify店,是指
  • 現在將shopify的訪問令牌,shopify商店名稱和您生成的令牌保存到數據庫中
  • 現在將您生成的令牌發送到客戶端(將其保存在Cookie或本地存儲中)

驗證流程:

  • 客戶端點擊您的服務器以使用您的身份驗證令牌獲取數據
  • 在您的數據庫中驗證此身份驗證令牌,並獲取該身份驗證令牌的訪問令牌和商店名稱
  • 現在使用此訪問令牌和商店名稱調用Shopify API

希望這種方法有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM