Files
EventHubBack/src/handlers/handler_verify.erl
T

72 lines
2.5 KiB
Erlang

%%%-------------------------------------------------------------------
%%% @doc Обработчик подтверждения email.
%%% POST /v1/verify – активирует аккаунт по верификационному токену.
%%% @end
%%%-------------------------------------------------------------------
-module(handler_verify).
-behaviour(cowboy_handler).
-export([init/2]).
-export([trails/0]).
%%% cowboy_handler callback
init(Req, _Opts) ->
case cowboy_req:method(Req) of
<<"POST">> ->
{ok, Body, Req1} = cowboy_req:read_body(Req),
try jsx:decode(Body, [return_maps]) of
#{<<"token">> := Token} ->
case core_verification:verify_token(Token) of
{ok, UserId} ->
core_user:update(UserId, [{status, active}]),
core_verification:delete_token(Token),
case logic_calendar:ensure_default_calendar(UserId) of
ok ->
handler_utils:send_json(Req1, 200, #{<<"message">> => <<"Account verified">>});
{error, Reason} ->
handler_utils:send_error(Req1, 500, Reason)
end;
{error, expired} ->
handler_utils:send_error(Req1, 410, <<"Token expired">>);
{error, not_found} ->
handler_utils:send_error(Req1, 404, <<"Token not found">>)
end;
_ ->
handler_utils:send_error(Req1, 400, <<"Missing token">>)
catch
_:_ -> handler_utils:send_error(Req1, 400, <<"Invalid JSON">>)
end;
_ ->
handler_utils:send_error(Req, 405, <<"Method not allowed">>)
end.
%%% Swagger metadata
-spec trails() -> [map()].
trails() ->
[
#{
path => <<"/v1/verify">>,
method => <<"POST">>,
description => <<"Verify user account using email verification token">>,
tags => [<<"Auth">>],
requestBody => #{
required => true,
content => #{
<<"application/json">> => #{
schema => #{
type => object,
required => [<<"token">>],
properties => #{
token => #{type => string, description => <<"Verification token received by email">>}
}
}
}
}
},
responses => #{
200 => #{description => <<"Account verified successfully">>},
400 => #{description => <<"Missing token field or invalid JSON">>},
404 => #{description => <<"Token not found">>},
410 => #{description => <<"Token expired">>}
}
}
].