首页  >  文章  >  web前端  >  Pocketbase pb_hooks - 检查用户身份验证

Pocketbase pb_hooks - 检查用户身份验证

PHPz
PHPz原创
2024-08-10 06:39:33465浏览

Pocketbase pb_hooks - checking user authentication

pocketbase 的文档对于如何在使用 pb_hooks 时检查经过身份验证的用户而言并不清晰

事实证明真的简单

文件:

https://pocketbase.io/docs/js-routing/#sending-request-to-custom-routes-using-the-sdks

https://pocketbase.io/jsvm/functions/_apis.requireAdminOrRecordAuth.html

例子 :

// main.pb.js
// This is a simple GET route, that is protected
routerAdd("GET", "/private", (c) => {
    const data = { 
        message : "This can only be accessed if you are logged in"
    }
    return c.json(200, data)
    // Adding the $apis.requireAdminOrRecordAuth() argument, ensures the route is protected unless the user is logged in.
}, $apis.requireAdminOrRecordAuth());


// This is a simple GET route, that is public
routerAdd("GET", "/public", (c) => {
    const data = { 
        message : "This can be be accessed by public"
    }
    return c.json(200, data)
});

然后只需使用 pocketbase 调用路线即可

// 
let fetchData = async () => {
    let resp = await pocketBaseClient.pb.send("/private", {
    });
    console.log(resp)
}

以上是Pocketbase pb_hooks - 检查用户身份验证的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn