」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > Pocketbase pb_hooks - 檢查使用者身份驗證

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

發佈於2024-08-28
瀏覽:978

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)
}
版本聲明 本文轉載於:https://dev.to/calumk/pocketbase-pbhooks-checking-user-authentication-1fo2?1如有侵犯,請聯絡[email protected]刪除
最新教學 更多>

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3