2020-01-26 21:36:50 +01:00
|
|
|
defmodule Mobilizon.Web.GraphQLSocket do
|
2019-12-03 11:29:51 +01:00
|
|
|
use Phoenix.Socket
|
|
|
|
|
|
|
|
use Absinthe.Phoenix.Socket,
|
2019-12-20 13:04:34 +01:00
|
|
|
schema: Mobilizon.GraphQL.Schema
|
2019-12-03 11:29:51 +01:00
|
|
|
|
|
|
|
alias Mobilizon.Users.User
|
|
|
|
|
|
|
|
def connect(%{"token" => token}, socket) do
|
|
|
|
with {:ok, authed_socket} <-
|
2020-01-26 21:36:50 +01:00
|
|
|
Guardian.Phoenix.Socket.authenticate(socket, Mobilizon.Web.Auth.Guardian, token),
|
2019-12-03 11:29:51 +01:00
|
|
|
%User{} = user <- Guardian.Phoenix.Socket.current_resource(authed_socket) do
|
|
|
|
authed_socket =
|
|
|
|
Absinthe.Phoenix.Socket.put_options(socket,
|
|
|
|
context: %{
|
|
|
|
current_user: user
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
{:ok, authed_socket}
|
|
|
|
else
|
|
|
|
{:error, _} ->
|
|
|
|
:error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-31 12:08:48 +02:00
|
|
|
def connect(_args, _socket), do: :error
|
|
|
|
|
2019-12-03 11:29:51 +01:00
|
|
|
def id(_socket), do: nil
|
|
|
|
end
|