Home > Web Front-end > JS Tutorial > body text

Pocketbase pb_hooks - checking user authentication

PHPz
Release: 2024-08-10 06:39:33
Original
494 people have browsed it

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)
});
Copy after login

Then simply call the route using the pocketbase

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

The above is the detailed content of Pocketbase pb_hooks - checking user authentication. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template