59 lines
1.9 KiB
Erlang
59 lines
1.9 KiB
Erlang
%%%-------------------------------------------------------------------
|
|
%%% @doc POST /v1/admin/logout — отзыв admin refresh-сессии (вариант A / Back#69).
|
|
%%% @end
|
|
%%%-------------------------------------------------------------------
|
|
-module(admin_handler_logout).
|
|
-behaviour(cowboy_handler).
|
|
-export([init/2, trails/0]).
|
|
|
|
-spec init(cowboy_req:req(), any()) -> {ok, cowboy_req:req(), any()}.
|
|
init(Req, _Opts) ->
|
|
case cowboy_req:method(Req) of
|
|
<<"POST">> -> logout(Req);
|
|
_ -> handler_utils:send_error(Req, 405, <<"Method not allowed">>)
|
|
end.
|
|
|
|
-spec trails() -> [map()].
|
|
trails() ->
|
|
[
|
|
#{
|
|
path => <<"/v1/admin/logout">>,
|
|
method => <<"POST">>,
|
|
description => <<"Revoke current admin refresh session">>,
|
|
tags => [<<"Admin Auth">>],
|
|
requestBody => #{
|
|
required => true,
|
|
content => #{
|
|
<<"application/json">> => #{
|
|
schema => #{
|
|
type => object,
|
|
required => [<<"refresh_token">>],
|
|
properties => #{refresh_token => #{type => string}}
|
|
}
|
|
}
|
|
}
|
|
},
|
|
responses => #{
|
|
200 => #{description => <<"Session revoked">>},
|
|
400 => #{description => <<"Missing refresh_token or invalid JSON">>},
|
|
401 => #{description => <<"Invalid refresh token">>}
|
|
}
|
|
}
|
|
].
|
|
|
|
logout(Req) ->
|
|
{ok, Body, Req1} = cowboy_req:read_body(Req),
|
|
try jsx:decode(Body, [return_maps]) of
|
|
#{<<"refresh_token">> := RefreshToken} ->
|
|
case logic_auth_session:logout_admin(RefreshToken) of
|
|
ok ->
|
|
handler_utils:send_json(Req1, 200, #{ok => true});
|
|
{error, _} ->
|
|
handler_utils:send_error(Req1, 401, <<"Invalid refresh token">>)
|
|
end;
|
|
_ ->
|
|
handler_utils:send_error(Req1, 400, <<"Missing refresh_token field">>)
|
|
catch
|
|
_:_ -> handler_utils:send_error(Req1, 400, <<"Invalid JSON">>)
|
|
end.
|