![](/img/trans.png)
[英]How to generate, store and use Access Tokens in Node.js/Express?
[英]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 token
和shop's name
保存在數據庫中,並根據某種算法生成並保存“身份驗證令牌”。 將生成的身份驗證令牌返回給客戶端。 確保客戶端在每個請求中發送此身份驗證令牌。
現在,當客戶端訪問您的服務器時,請驗證身份驗證令牌; 驗證后,請使用適當的“訪問令牌”和商店名稱調用Shopify API。
身份驗證流程如下:
驗證流程:
希望這種方法有幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.