DEV Community

Cover image for Pocketbase pb_hooks - checking user authentication
Calum Knott
Calum Knott

Posted on

1

Pocketbase pb_hooks - checking user authentication

The documentation for pocketbase is not as clear as it could be about how to check for an authenticated user when using the pb_hooks

It turns out that it is really simple

Docs :

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

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

Example :

// 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)
});
Enter fullscreen mode Exit fullscreen mode

Then simply call the route using the pocketbase

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

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more