首頁  >  文章  >  web前端  >  Pocketbase pb_hooks - 檢查使用者身份驗證

Pocketbase pb_hooks - 檢查使用者身份驗證

PHPz
PHPz原創
2024-08-10 06:39:33466瀏覽

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