77 lines
2.7 KiB
Erlang
Executable File
77 lines
2.7 KiB
Erlang
Executable File
%%%-------------------------------------------------------------------
|
|
%%% @doc POST /v1/reset-password — установка нового пароля по токену.
|
|
%%% @end
|
|
%%%-------------------------------------------------------------------
|
|
-module(handler_reset_password).
|
|
-behaviour(cowboy_handler).
|
|
-export([init/2]).
|
|
-export([trails/0]).
|
|
|
|
init(Req, _Opts) ->
|
|
case cowboy_req:method(Req) of
|
|
<<"POST">> -> reset(Req);
|
|
_ -> handler_utils:send_error(Req, 405, <<"Method not allowed">>)
|
|
end.
|
|
|
|
reset(Req) ->
|
|
case cowboy_req:has_body(Req) of
|
|
false ->
|
|
handler_utils:send_error(Req, 400, <<"Missing request body">>);
|
|
true ->
|
|
{ok, Body, Req1} = cowboy_req:read_body(Req),
|
|
try jsx:decode(Body, [return_maps]) of
|
|
#{<<"token">> := Token, <<"password">> := Password}
|
|
when is_binary(Token), is_binary(Password) ->
|
|
case logic_password_reset:reset_password(Token, Password) of
|
|
ok ->
|
|
handler_utils:send_json(Req1, 200, #{<<"message">> => <<"Password updated">>});
|
|
{error, expired} ->
|
|
handler_utils:send_error(Req1, 410, <<"Token expired">>);
|
|
{error, not_found} ->
|
|
handler_utils:send_error(Req1, 404, <<"Token not found">>);
|
|
{error, invalid_password} ->
|
|
handler_utils:send_error(Req1, 400, <<"Invalid password">>);
|
|
{error, forbidden} ->
|
|
handler_utils:send_error(Req1, 403, <<"Account cannot reset password">>);
|
|
{error, _} ->
|
|
handler_utils:send_error(Req1, 500, <<"Internal server error">>)
|
|
end;
|
|
_ ->
|
|
handler_utils:send_error(Req1, 400, <<"Missing token or password">>)
|
|
catch
|
|
_:_ -> handler_utils:send_error(Req1, 400, <<"Invalid JSON">>)
|
|
end
|
|
end.
|
|
|
|
trails() ->
|
|
[
|
|
#{
|
|
path => <<"/v1/reset-password">>,
|
|
method => <<"POST">>,
|
|
description => <<"Reset password using token from email">>,
|
|
tags => [<<"Auth">>],
|
|
requestBody => #{
|
|
required => true,
|
|
content => #{
|
|
<<"application/json">> => #{
|
|
schema => #{
|
|
type => object,
|
|
required => [<<"token">>, <<"password">>],
|
|
properties => #{
|
|
token => #{type => string},
|
|
password => #{type => string, format => <<"password">>, minLength => 8}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
},
|
|
responses => #{
|
|
200 => #{description => <<"Password updated">>},
|
|
400 => #{description => <<"Missing fields or invalid password">>},
|
|
403 => #{description => <<"Account not eligible">>},
|
|
404 => #{description => <<"Token not found">>},
|
|
410 => #{description => <<"Token expired">>}
|
|
}
|
|
}
|
|
].
|