diff --git a/docs/examples/external_auth.md b/docs/examples/external_auth.md index 0c7a8c202..4c560416e 100644 --- a/docs/examples/external_auth.md +++ b/docs/examples/external_auth.md @@ -50,7 +50,7 @@ and the anonymous user, we will have two aditional roles: Bellow we have the commands to create all roles that will be used: ```sql -CREATE USER postgrest; +CREATE USER authenticator NOINHERIT; CREATE ROLE anonymous; CREATE ROLE admin; CREATE ROLE customer; @@ -95,7 +95,8 @@ class ApiTokensController < ApplicationController def claims # I'm assuming a boolean field admin in the user model indicating wheter the # user has administrative privileges. - { role: current_user.role, user_id: current_user.id.to_s } + # This token will expire 1 hour after being issued + { role: current_user.role, user_id: current_user.id.to_s, exp: (Time.now + 1.hour).to_i } end end ``` @@ -117,7 +118,7 @@ the logged in user. ```sql ALTER DATABASE mydb SET postgrest.claims.user_id TO ''; -CREATE OR REPLACE FUNCTION user_id() +CREATE OR REPLACE FUNCTION current_user_id() RETURNS integer STABLE LANGUAGE SQL @@ -125,14 +126,6 @@ AS $$ SELECT nullif(current_setting('postgrest.claims.user_id'), '')::integer; $$; -CREATE OR REPLACE FUNCTION is_owner_or_admin(user_id int) -RETURNS boolean -STABLE -LANGUAGE SQL -AS $$ - SELECT current_user = 'admin' OR is_owner_or_admin.user_id = user_id(); -$$; - CREATE SCHEMA private; CREATE TABLE private.orders ( @@ -148,7 +141,7 @@ SELECT FROM private.orders o WHERE - is_owner_or_admin(o.user_id); + current_user = 'admin' OR o.user_id = current_user_id(); ``` ### Using the JWT