Перенести все админские эндпоинты на порт 8445 и добавить отдельную авторизацию для админов. Часть 2. Final #3
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
-module(ws_handler).
|
||||
-behaviour(cowboy_websocket).
|
||||
|
||||
-export([init/2]).
|
||||
-export([websocket_init/1]).
|
||||
-export([websocket_handle/2]).
|
||||
@@ -13,15 +12,13 @@
|
||||
}).
|
||||
|
||||
init(Req, _Opts) ->
|
||||
% Аутентификация через query параметр token
|
||||
Qs = cowboy_req:parse_qs(Req),
|
||||
case proplists:get_value(<<"token">>, Qs) of
|
||||
undefined ->
|
||||
{ok, cowboy_req:reply(401, #{}, <<"Missing token">>, Req), undefined};
|
||||
Token ->
|
||||
case logic_auth:verify_jwt(Token) of
|
||||
{ok, Claims} ->
|
||||
UserId = maps:get(<<"user_id">>, Claims),
|
||||
{ok, UserId, _Role} ->
|
||||
{cowboy_websocket, Req, #state{user_id = UserId}};
|
||||
{error, _} ->
|
||||
{ok, cowboy_req:reply(401, #{}, <<"Invalid token">>, Req), undefined}
|
||||
@@ -29,7 +26,6 @@ init(Req, _Opts) ->
|
||||
end.
|
||||
|
||||
websocket_init(State) ->
|
||||
% Регистрируем процесс в pg для получения уведомлений
|
||||
pg:join(eventhub_ws, self()),
|
||||
{ok, State}.
|
||||
|
||||
@@ -39,9 +35,9 @@ websocket_handle({text, Msg}, State) ->
|
||||
#{<<"action">> := <<"subscribe">>, <<"calendar_id">> := CalendarId} ->
|
||||
io:format("Subscribe to calendar: ~s~n", [CalendarId]),
|
||||
NewSubs = case lists:member(CalendarId, State#state.subscriptions) of
|
||||
true -> State#state.subscriptions;
|
||||
false -> [CalendarId | State#state.subscriptions]
|
||||
end,
|
||||
true -> State#state.subscriptions;
|
||||
false -> [CalendarId | State#state.subscriptions]
|
||||
end,
|
||||
Reply = jsx:encode(#{status => <<"subscribed">>, calendar_id => CalendarId}),
|
||||
io:format("Sending reply: ~s~n", [Reply]),
|
||||
{reply, {text, Reply}, State#state{subscriptions = NewSubs}};
|
||||
@@ -77,7 +73,6 @@ terminate(_Reason, _Req, _State) ->
|
||||
pg:leave(eventhub_ws, self()),
|
||||
ok.
|
||||
|
||||
%% Проверка, нужно ли отправлять уведомление пользователю
|
||||
should_notify(calendar_update, #{calendar_id := CalId}, State) ->
|
||||
lists:member(CalId, State#state.subscriptions);
|
||||
should_notify(booking_update, #{user_id := UserId}, State) ->
|
||||
|
||||
Reference in New Issue
Block a user