Search code examples
jwtpostgrest

function sign(json, unknown) does not exist


I'm following the tutorial on https://postgrest.org/en/v5.0/auth.html#jwt-from-sql

I created this function:

create or replace function
login(email text, pass text) returns basic_auth.jwt_token
  language plpgsql
  as $$
declare
  _role name;
  result basic_auth.jwt_token;
begin
  -- check email and password
  select basic_auth.user_role(email, pass) into _role;
  if _role is null then
    raise invalid_password using message = 'invalid user or password';
  end if;

  select sign(
      row_to_json(r), 'reallyreallyreallyreallyverysafe'
    ) as token
    from (
      select _role as role, login.email as email,
         extract(epoch from now())::integer + 60*60 as exp
    ) r
    into result;
  return result;
end;
$$;

When I try to do the login request, I get the following error message:

{ "hint": "No function matches the given name and argument types. You might need to add explicit type casts.", "details": null, "code": "42883", "message": "function public.sign(json, unknown) does not exist" }

I don't see the sign function anywhere in the tutorial, does anyone know what is going on here?


Solution

  • The sign function is created when you install pgjwt.

    Also, make sure that you install the extension on a schema that is included in your search_path(a list of imported schemas). This can be specified by doing CREATE EXTENSION pgjwt WITH SCHEMA public.

    PostgREST adds the public schema by default on the search_path, so the extension should work with no additional config.