feat(api): optional auth for public search and studio week. Refs EventHub/EventHubFront#58

This commit is contained in:
2026-08-14 11:02:58 +03:00
parent 8a5f254c2a
commit 520af23cc6
7 changed files with 32 additions and 15 deletions
+2 -2
View File
@@ -115,13 +115,13 @@ calendar_update_schema() ->
%%% Internal functions
get_calendar(Req) ->
case handler_utils:auth_user(Req) of
case handler_utils:auth_user_optional(Req) of
{ok, UserId, Req1} ->
CalendarId = cowboy_req:binding(id, Req1),
case logic_calendar:get_calendar(UserId, CalendarId) of
{ok, Calendar} ->
Json0 = handler_utils:calendar_to_json(Calendar),
Following = logic_calendar_follow:is_following(UserId, CalendarId),
Following = UserId =/= <<>> andalso logic_calendar_follow:is_following(UserId, CalendarId),
handler_utils:send_json(Req1, 200, Json0#{following => Following});
{error, access_denied} ->
handler_utils:send_error(Req1, 403, <<"Access denied">>);
@@ -94,7 +94,7 @@ has_user_binding(Req) ->
cowboy_req:binding(user_id, Req) =/= undefined.
list_specialists(Req) ->
case handler_utils:auth_user(Req) of
case handler_utils:auth_user_optional(Req) of
{ok, UserId, Req1} ->
CalendarId = cowboy_req:binding(id, Req1),
case logic_calendar_specialist:list(UserId, CalendarId) of
+1 -1
View File
@@ -225,7 +225,7 @@ create_event(Req) ->
%% @doc GET /v1/calendars/:calendar_id/events — список событий.
-spec list_events(cowboy_req:req()) -> {ok, binary(), cowboy_req:req()}.
list_events(Req) ->
case handler_utils:auth_user(Req) of
case handler_utils:auth_user_optional(Req) of
{ok, UserId, Req1} ->
CalendarId = cowboy_req:binding(calendar_id, Req1),
Qs = cowboy_req:parse_qs(Req1),
+1 -1
View File
@@ -74,7 +74,7 @@ handle(Req, _Opts) ->
%% @doc GET /v1/search — полнотекстовый поиск с фильтрами.
-spec search(cowboy_req:req()) -> {ok, binary(), cowboy_req:req()}.
search(Req) ->
case handler_utils:auth_user(Req) of
case handler_utils:auth_user_optional(Req) of
{ok, UserId, Req1} ->
Qs = cowboy_req:parse_qs(Req1),
Type = proplists:get_value(<<"type">>, Qs, undefined),
+12
View File
@@ -10,6 +10,7 @@
-export([
auth_admin/1,
auth_user/1,
auth_user_optional/1,
send_json/3,
send_json/4,
send_error/3,
@@ -75,6 +76,17 @@ is_superadmin(Req) ->
auth_user(Req) ->
handler_auth:authenticate(Req).
%% @doc Как auth_user/1, но без заголовка Authorization — гость (`<<>>`).
%% Невалидный Bearer по-прежнему 401.
-spec auth_user_optional(cowboy_req:req()) ->
{ok, binary(), cowboy_req:req()} | {error, integer(), binary(), cowboy_req:req()}.
auth_user_optional(Req) ->
case cowboy_req:header(<<"authorization">>, Req) of
undefined -> {ok, <<>>, Req};
<<>> -> {ok, <<>>, Req};
_ -> auth_user(Req)
end.
%%%===================================================================
%%% HTTP‑ответы
%%%===================================================================