流程:
1.云函数生成 token 返回给前端。
2.前端请求时带上 token 。
3.云函数中根据 ctx.user 来判断是否传 token 是否过期。
示例代码:
1.云函数生成 token 返回给前端。
import cloud from '@lafjs/cloud'
export async function main(ctx: FunctionContext) {
const payload = {
// uid 一般用表里用户的 id 这里演示随便写
uid: 1,
// 这里做演示 过期时间设置为10s
// 这样写就是过期时间7天 exp: Math.floor(Date.now() / 1000) + 60 * 60 * 24 * 7,
exp: Math.floor(Date.now() / 1000) + 10,
};
// 生成 access_token
const access_token = cloud.getToken(payload);
return { access_token }
}
2.前端请求时带上 token 。
第一种使用 laf-client-sdk.
import { Cloud } from "laf-client-sdk"; // 引入laf-client-sdk
import axios from "axios";
// 创建cloud对象
const cloud = new Cloud({
baseUrl: "https://a3uvbk.laf.dev",
// 传入access_token
getAccessToken: () => localStorage.getItem("access_token"),
});
// invoke 调用云函数时会自动带上access_token
const res = await cloud.invoke("test");
第二种通过 axios
import axios from "axios";
const token = localStorage.getItem("access_token");
axios({
method: "get",
url: "functionUrl",
headers: {
// 这里带上token
Authorization: `token ${token}`,
},
})
3.云函数中根据 ctx.user 来判断是否传 token 是否过期。
import cloud from '@lafjs/cloud'
export async function main(ctx: FunctionContext) {
console.log(ctx.user)
// 如果前端传了 token 并且没过期: { uid: 1, exp: 1683861234, iat: 1683861224 }
// 如果前端没传 token 或者 token 不在有效期: null
}