首页 > web前端 > js教程 > Pocketbase pb_hooks - 检查用户身份验证

Pocketbase pb_hooks - 检查用户身份验证

PHPz
发布: 2024-08-10 06:39:33
原创
863 人浏览过

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

例子 :

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

// 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 调用路线即可

1

2

3

4

5

6

//

let fetchData = async () => {

    let resp = await pocketBaseClient.pb.send("/private", {

    });

    console.log(resp)

}

登录后复制

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

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板