Skip to content

Instantly share code, notes, and snippets.

@dapangmao
Last active June 6, 2017 15:58
Show Gist options
  • Save dapangmao/3067d9cd83161990bec69e83ee204d72 to your computer and use it in GitHub Desktop.
Save dapangmao/3067d9cd83161990bec69e83ee204d72 to your computer and use it in GitHub Desktop.
CREATE OR REPLACE FUNCTION public.login(email text, pass text)
RETURNS basic_auth.jwt_token
LANGUAGE plpgsql
AS $function$
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), 'mysecret'
) 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;
$function$
----------------------------------------------------
CREATE ROLE anonymous;
CREATE ROLE webuser;
GRANT anonymous, webuser TO owner;
GRANT EXECUTE ON FUNCTION public.login(text, text) TO anonymous;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment