@ forwardpath/节点
FrontPath 收录 API 的服务器端助手. 用于签名用户
可出版关键会话 createUserHash或薄荷短寿命摄入量
带有您的 API 密钥的会话, 因此密钥永远不会到达浏览器 。 这个
next.js App路由器(或任何运行时间)的现成路线处理器
网络 Request编号 :Response) (中文(简体) ).
安装
npm install @forwardpath/node
验证用户为 createUserHash
使用可公布的密钥,浏览器会自定会话并命名为
用户。 没有证据, ForwardPath 标记该用户 claimed,即未经核实.
来标记他们 verified,用密钥在服务器上签名
身份秘密(fpiv_…,在创建密钥时显示一次)并传递
散列到浏览器 .
import { createUserHash } from "@forwardpath/node";
const userHash = createUserHash({
identitySecret: process.env.FORWARDPATH_IDENTITY_SECRET!,
user: { id: user.id, email: user.email },
});
// Render it into the page:
// <ForwardPathProvider publishableKey="fppk_…" user={user} userHash={userHash}>
散列是小写 HMAC-SHA256的 JSON.stringify([id, email]),与
id被剪切,电子邮件被剪切和小写(null (在缺席时)。 这个
签名 ID 和电子邮件, 而不是名称 。 浏览器必须发送相同的 ID 和
电子邮件,或会话失败为 401 . createUserHash 丢入浏览器
如果秘密不是从 fpiv_保持一个秘密
(原始内容存档于2017-03-21). only server-env var.
在服务器上保留您的 API 密钥
您的 API 密钥( E)fp_<keyId>.<secret>)可以为任何用户提供薄荷会话. 收起来
在只使用服务器的env var. createForwardPathClient 如果运行在一个
浏览器, 或者如果密钥形状不当 。 路由处理器只用
语句符号及其过期,绝不是密钥.
下一个.js路线
// app/api/forwardpath/session/route.ts
import {
createForwardPathClient,
createIntakeSessionHandler,
} from "@forwardpath/node";
import { auth } from "@/auth"; // your own auth
const client = createForwardPathClient({
apiKey: process.env.FORWARDPATH_API_KEY!,
baseUrl: "https://app.forwardpath.ai",
});
export const POST = createIntakeSessionHandler({
client,
projectSlug: "acme-app",
getUser: async () => {
const session = await auth();
if (!session?.user) return null; // responds 401
return {
externalId: session.user.id,
email: session.user.email,
name: session.user.name ?? undefined,
};
},
});
浏览器调用它 POST /api/forwardpath/session 回来
{ token, expiresAt }。将符号传递给 @forwardpath/js 或者说
@forwardpath/react尸体可以携带 { clientContext } (例如来自
collectClientContext());处理器将其转发到前方Path.
处理器响应 :
| 状态 | 何时 |
|---|---|
| 200个 | { token, expiresAt } |
| 400个 | 身体不是JSON物体, 或者ForwardPath拒绝了有效载荷 |
| 401 (中文(简体) ) | getUser 已返回 null |
| 502 (中文(简体) ) | FrontPath 失败的另一个原因( 如不良的 API 密钥) |
自己去打一场
const session = await client.createIntakeSession({
projectSlug: "acme-app",
user: { externalId: "u_123", email: "[email protected]", name: "Ada" },
clientContext: { appName: "Acme", appVersion: "2.4.0" }, // optional
metadata: { plan: "pro" }, // optional, stored on the reporter
});
// { token, expiresAt, projectSlug, reporter }
非-2xx回复拒绝使用 ForwardPathApiError (从
@forwardpath/js)),已经 status, (中文(简体) ), code 财务报告和审定财务报表 title。 。 。 .