Skip to content

Instantly share code, notes, and snippets.

@begriffs
Created February 15, 2016 05:51
Show Gist options
  • Select an option

  • Save begriffs/99903cfe057d565a849c to your computer and use it in GitHub Desktop.

Select an option

Save begriffs/99903cfe057d565a849c to your computer and use it in GitHub Desktop.
create or replace function
reset_password(email text, token uuid, pass text)
returns void
language plpgsql
as $$
declare
tok uuid;
begin
if exists(select 1 from basic_auth.tokens
where tokens.email = reset_password.email
and tokens.token = reset_password.token
and token_type = 'reset') then
update basic_auth.users set pass=reset_password.pass
where users.email = reset_password.email;
delete from basic_auth.tokens
where tokens.email = reset_password.email
and tokens.token = reset_password.token
and token_type = 'reset';
else
raise invalid_password using message =
'invalid user or token';
end if;
delete from basic_auth.tokens
where token_type = 'reset'
and tokens.email = reset_password.email;
select uuid_generate_v4() into tok;
insert into basic_auth.tokens (token, token_type, email)
values (tok, 'reset', reset_password.email);
perform pg_notify('reset',
json_build_object(
'email', reset_password.email,
'token', tok
)::text
);
end;
$$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment