我已经构建了我的第一个Node.js
应用程序,该应用程序应该安装在Shopify
商店中。如果你想看看我的实际代码是什么样的(app.js),你可以查看它here。这是非常基本的,所以阅读并不难。Node.js - 如何使用访问/授权令牌?
我知道如何验证安装应用程序的(继Shopify说明),但我没怎么使用永久access token
,一个成功的安装为我提供了所有后续请求验证。
通过后续请求,我指的是呈现应用或请求安装应用的请求,即使应用已安装。
现在,我存储店铺的名字(这是唯一的)与永久性令牌Shopify将在我数据库一起。但我不知道这是否是必要的。如果我没有错,只需使用浏览器的会话就可以做到吗?但我该怎么做?我怎么在每次请求到来时使用这个令牌来检查它是否有效?
谢谢任何帮助/建议!
下面的代码是那种什么my actual code样子,以便给你的我的问题是什么的想法表示:
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 API。尽管如此,我并不真正理解'access token'和'auth token'之间的区别。我在Shopify上得到的所有东西都是一个“临时标记”,然后我换成了“永久标记”,这是我在DB中保存的一个商标名称。所以我不知道如何获得你所指的'auth token'。我也不知道如何将'token'返回给客户端,就像你所建议的一样,我不知道如何在客户端发出的每个请求中都存在'check token'。我在OP中包含了一个到我的实际代码的链接,以防有所帮助。 –
如果在客户端和服务器之间移动'永久令牌'不是好的做法,那我该怎么做呢?我如何生成你正在谈论的'auth token',我该怎么处理呢? –
编辑我的答案详细的工作流程 –