docs: Simplify auth examples with OUT parameter
The jwt_token type was not created consistently in all examples, which can be confusing when following those. To return an object with a single key named token, it's enough to have an OUT parameter to the function. Resolves https://github.com/PostgREST/postgrest-docs/issues/280
This commit is contained in:
committed by
Wolfgang Walther
parent
7c6c056e92
commit
9b1ff2235a
@@ -166,7 +166,7 @@ Another option is to define the function with the :code:`SECURITY DEFINER` optio
|
|||||||
-- login as a user wich has privileges on the private schemas
|
-- login as a user wich has privileges on the private schemas
|
||||||
|
|
||||||
-- create a sample function
|
-- create a sample function
|
||||||
create or replace function login(email text, pass text) returns jwt_token as $$
|
create or replace function login(email text, pass text, out token text) as $$
|
||||||
begin
|
begin
|
||||||
-- access to a private schema called 'auth'
|
-- access to a private schema called 'auth'
|
||||||
select auth.user_role(email, pass) into _role;
|
select auth.user_role(email, pass) into _role;
|
||||||
|
|||||||
@@ -162,20 +162,15 @@ As described in :ref:`client_auth`, we'll create a JWT token inside our login fu
|
|||||||
|
|
||||||
.. code-block:: postgres
|
.. code-block:: postgres
|
||||||
|
|
||||||
CREATE TYPE basic_auth.jwt_token AS (
|
|
||||||
token text
|
|
||||||
);
|
|
||||||
|
|
||||||
-- if you are not using psql, you need to replace :DBNAME with the current database's name.
|
-- if you are not using psql, you need to replace :DBNAME with the current database's name.
|
||||||
ALTER DATABASE :DBNAME SET "app.jwt_secret" to 'reallyreallyreallyreallyverysafe';
|
ALTER DATABASE :DBNAME SET "app.jwt_secret" to 'reallyreallyreallyreallyverysafe';
|
||||||
|
|
||||||
|
|
||||||
CREATE FUNCTION public.login(username text, password text) RETURNS basic_auth.jwt_token
|
CREATE FUNCTION public.login(username text, password text, OUT token text)
|
||||||
LANGUAGE plpgsql security definer
|
LANGUAGE plpgsql security definer
|
||||||
AS $$
|
AS $$
|
||||||
DECLARE
|
DECLARE
|
||||||
_role name;
|
_role name;
|
||||||
result basic_auth.jwt_token;
|
|
||||||
BEGIN
|
BEGIN
|
||||||
-- check email and password
|
-- check email and password
|
||||||
SELECT basic_auth.check_user_pass(username, password) INTO _role;
|
SELECT basic_auth.check_user_pass(username, password) INTO _role;
|
||||||
@@ -190,8 +185,7 @@ As described in :ref:`client_auth`, we'll create a JWT token inside our login fu
|
|||||||
SELECT login.username as role,
|
SELECT login.username as role,
|
||||||
extract(epoch FROM now())::integer + 60*60 AS exp
|
extract(epoch FROM now())::integer + 60*60 AS exp
|
||||||
) r
|
) r
|
||||||
INTO result;
|
INTO token;
|
||||||
RETURN result;
|
|
||||||
END;
|
END;
|
||||||
$$;
|
$$;
|
||||||
|
|
||||||
|
|||||||
@@ -122,11 +122,7 @@ Next write a stored procedure that returns the token. The one below returns a to
|
|||||||
|
|
||||||
.. code-block:: postgres
|
.. code-block:: postgres
|
||||||
|
|
||||||
CREATE TYPE jwt_token AS (
|
CREATE FUNCTION jwt_test(OUT token text) AS $$
|
||||||
token text
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE FUNCTION jwt_test() RETURNS public.jwt_token AS $$
|
|
||||||
SELECT public.sign(
|
SELECT public.sign(
|
||||||
row_to_json(r), 'reallyreallyreallyreallyverysafe'
|
row_to_json(r), 'reallyreallyreallyreallyverysafe'
|
||||||
) AS token
|
) AS token
|
||||||
@@ -161,17 +157,11 @@ As described in `JWT from SQL`_, we'll create a JWT inside our login function. N
|
|||||||
|
|
||||||
.. code-block:: postgres
|
.. code-block:: postgres
|
||||||
|
|
||||||
-- add type
|
|
||||||
CREATE TYPE basic_auth.jwt_token AS (
|
|
||||||
token text
|
|
||||||
);
|
|
||||||
|
|
||||||
-- login should be on your exposed schema
|
-- login should be on your exposed schema
|
||||||
create or replace function
|
create or replace function
|
||||||
login(email text, pass text) returns basic_auth.jwt_token as $$
|
login(email text, pass text, out token text) as $$
|
||||||
declare
|
declare
|
||||||
_role name;
|
_role name;
|
||||||
result basic_auth.jwt_token;
|
|
||||||
begin
|
begin
|
||||||
-- check email and password
|
-- check email and password
|
||||||
select basic_auth.user_role(email, pass) into _role;
|
select basic_auth.user_role(email, pass) into _role;
|
||||||
@@ -186,8 +176,7 @@ As described in `JWT from SQL`_, we'll create a JWT inside our login function. N
|
|||||||
select _role as role, login.email as email,
|
select _role as role, login.email as email,
|
||||||
extract(epoch from now())::integer + 60*60 as exp
|
extract(epoch from now())::integer + 60*60 as exp
|
||||||
) r
|
) r
|
||||||
into result;
|
into token;
|
||||||
return result;
|
|
||||||
end;
|
end;
|
||||||
$$ language plpgsql security definer;
|
$$ language plpgsql security definer;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user