# Problem with authenticating WebSockets

**URL:** <https://forum.elixirforum.com/t/problem-with-authenticating-websockets/33124>\
**Category:** Questions\
**Tags:** channels, phoenix, websockets\
**Created:** [July 15, 2020, 7:10pm UTC](https://forum.elixirforum.com/t/problem-with-authenticating-websockets/33124 "2020-07-15T19:10:09Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![km751](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/km751/32/20344_2.png) [@km751](https://forum.elixirforum.com/u/km751)\
**Post date:** [July 15, 2020, 7:10pm UTC](https://forum.elixirforum.com/t/problem-with-authenticating-websockets/33124/1 "2020-07-15T19:10:09Z")

</div>

I have a problem with authenticating WebSocket connection.

I use Phoenix and Vue + phoenix-socket as my front-end, and everything was fine before adding authentication.

The connection can be established and data is transferred properly but after that, it starts sending errors and I don’t understand why.

My browser console looks like this:

```elixir
    receive: ok feed:1 phx_reply (1) {response: {…}, status: "ok"}

    Joined successfully {feed: Array(3)}

    WebSocket connection to 'ws://localhost:4000/socket/websocket?vsn=1.0.0' failed: Error during WebSocket handshake: Unexpected response code: 403

    push: phoenix heartbeat (2) {}

    receive: ok phoenix phx_reply (2) {response: {…}, status: "ok"}

    WebSocket connection to 'ws://localhost:4000/socket/websocket?vsn=1.0.0' failed: Error during WebSocket handshake: Unexpected response code: 403

```

Phoenix:

```elixir
[info] CONNECTED TO TweeterApiWeb.UserSocket in 0┬Ás
  Transport: :websocket
  Serializer: Phoenix.Socket.V1.JSONSerializer
  Parameters: %{"token" => "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJ0d2VldGVyX2FwaSIsImV4cCI6MTU5NjkxMjAxNiwiaWF0IjoxNTk0NDkyODE2LCJpc3MiOiJ0d2VldGVyX2FwaSIsImp0aSI6IjViYWFlMDRlLTBjMTYtNDEyMi05Y2VlLWZmMzQ2OWM1YWE1YiIsIm5iZiI6MTU5NDQ5MjgxNSwic3ViIjoiMSIsInR5cCI6ImFjY2VzcyJ9.-ZJMyyEBKd0_nHYUBGdaI0qdHn1nuWtpG8sEUHqikBuWTB2sKw9Sk36OsUpXBS5ozRpe2l2VXq8NI58HydIhZA", "vsn" => "1.0.0"}
[debug] QUERY OK source="tweets" db=0.0ms idle=875.0ms
SELECT t0."id", t0."content", t0."comment_count", t0."retweet_count", t0."like_count", t0."profile_id", t0."inserted_at", t0."updated_at" FROM "tweets" AS t0 WHERE (t0."profile_id" = $1) [1]
[info] JOINED feed:1 in 0┬Ás
  Parameters: %{}
[info] REFUSED CONNECTION TO TweeterApiWeb.UserSocket in 0┬Ás
  Transport: :websocket
  Serializer: Phoenix.Socket.V1.JSONSerializer
  Parameters: %{"vsn" => "1.0.0"}

```

It looks like the connection is refused because there is no token in params but I only check the authentication when the socket connects, so the token should be completely unnecessary once the connection is established, right?

Here is my user\_socket.ex:

```elixir
defmodule TweeterApiWeb.UserSocket do
      use Phoenix.Socket
    
      alias TweeterApi.Accounts
      alias TweeterApi.Accounts.Guardian
    
      ## Channels
      channel "feed:*", TweeterApiWeb.FeedChannel
    
      @impl true
      def connect(%{"token" => token}, socket, _connect_info) do
        case Guardian.resource_from_token(token) do
          {:ok, user, _claims} ->
            current_profile = Accounts.get_profile_by(user_id: user.id)
    
            {:ok, assign(socket, :current_profile_id, current_profile.id)}
    
          {:error, _reason} ->
            :error
        end
      end
    
      def connect(_params, _socket, _connect_info), do: :error
    
      @impl true
      def id(socket), do: "users_socket:#{socket.assigns.current_profile_id}"
end

```

The channel code:

```elixir
defmodule TweeterApiWeb.FeedChannel do
      use TweeterApiWeb, :channel
    
      alias TweeterApi.Tweets
    
      def join("feed:" <> current_profile_id, _params, socket) do
        if String.to_integer(current_profile_id) === socket.assigns.current_profile_id do
          current_profile_tweets = Tweets.list_profile_tweets(current_profile_id)
    
          response = %{
            feed:
              Phoenix.View.render_many(current_profile_tweets, TweeterApiWeb.TweetView, "tweet.json")
          }
    
          {:ok, response, socket}
        else
          {:error, %{reason: "Not authorized"}}
        end
      end
    
      def terminate(_reason, socket) do
        {:ok, socket}
      end
end

```

and Vue.js code:

```elixir
<script>
    import UserProfileSection from '@/components/sections/UserProfileSection.vue'
    import TimelineSection from '@/components/sections/TimelineSection.vue'
    import FollowPropositionsSection from '@/components/sections/FollowPropositionsSection.vue'
    import NewTweetForm from '@/components/sections/NewTweetForm.vue'
    import { mapGetters } from 'vuex'
    import { Socket } from 'phoenix-socket'
    
    export default {
        name: 'AppFeed',
        components: {
            UserProfileSection,
            TimelineSection,
            FollowPropositionsSection,
            NewTweetForm,
        },
        data() {
            return {
                tweets: [],
            }
        },
        computed: {
            ...mapGetters('auth', ['currentProfileId', 'token'])
        },
        mounted() {
            const WEBSOCKET_URL = 'ws://localhost:4000'
    
            const socket = new Socket(`${WEBSOCKET_URL}/socket`, {
                params: { token: this.token },
                logger: (kind, msg, data) => {
                    console.log(`${kind}: ${msg}`, data)
                },
            })
    
            socket.connect()
    
            this.channel = socket.channel('feed:' + this.currentProfileId, {})
    
            this.channel
                .join()
                .receive('ok', (resp) => {
                    console.log('Joined successfully', resp)
                    console.log(resp)
                    this.tweets = resp.feed
                })
                .receive('error', (resp) => {
                    console.log('Unable to join', resp)
                })
        }
    }
</script>

```

---

<div class="post-metadata">

**Author:** ![kokolegorille](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kokolegorille/32/4784_2.png) [@kokolegorille](https://forum.elixirforum.com/u/kokolegorille)\
**Post date:** [July 15, 2020, 7:46pm UTC](https://forum.elixirforum.com/t/problem-with-authenticating-websockets/33124/2 "2020-07-15T19:46:08Z")

</div>

> [@km751](#):
>
> `import { Socket } from 'phoenix-socket'`

Hello and welcome…

It seems You are using an outdated library.

This one should be better

[https://www.npmjs.com/package/phoenix](https://www.npmjs.com/package/phoenix)

BTW You did not mention your phoenix version 🙂

---

<div class="post-metadata">

**Author:** ![km751](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/km751/32/20344_2.png) [@km751](https://forum.elixirforum.com/u/km751)\
**Post date:** [July 15, 2020, 8:24pm UTC](https://forum.elixirforum.com/t/problem-with-authenticating-websockets/33124/3 "2020-07-15T20:24:45Z")

</div>

My phoenix version is 1.5.3.

I have switched the library, but nothing changed.

---

<div class="post-metadata">

**Author:** ![kokolegorille](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kokolegorille/32/4784_2.png) [@kokolegorille](https://forum.elixirforum.com/u/kokolegorille)\
**Post date:** [July 15, 2020, 8:32pm UTC](https://forum.elixirforum.com/t/problem-with-authenticating-websockets/33124/4 "2020-07-15T20:32:59Z")

</div>

I am not using vue, but I always pass the token…

---

<div class="post-metadata">

**Author:** ![km751](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/km751/32/20344_2.png) [@km751](https://forum.elixirforum.com/u/km751)\
**Post date:** [July 15, 2020, 8:43pm UTC](https://forum.elixirforum.com/t/problem-with-authenticating-websockets/33124/5 "2020-07-15T20:43:04Z")

</div>

So what should the code look like?

Isn’t this part of code supposed to take care of passing the token with the WebSocket?

```elixir
            const socket = new Socket(`${WEBSOCKET_URL}/socket`, {
                params: { token: this.token },
                logger: (kind, msg, data) => {
                    console.log(`${kind}: ${msg}`, data)
                },
            })

```

---

<div class="post-metadata">

**Author:** ![lud](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lud/32/14382_2.png) [@lud](https://forum.elixirforum.com/u/lud)\
**Post date:** [July 15, 2020, 8:44pm UTC](https://forum.elixirforum.com/t/problem-with-authenticating-websockets/33124/6 "2020-07-15T20:44:40Z")

</div>

> [@km751](#):
>
> `Parameters: %{"vsn" => "1.0.0"}`

Can you see vsn as 2.0 now ?

---

<div class="post-metadata">

**Author:** ![km751](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/km751/32/20344_2.png) [@km751](https://forum.elixirforum.com/u/km751)\
**Post date:** [July 15, 2020, 8:45pm UTC](https://forum.elixirforum.com/t/problem-with-authenticating-websockets/33124/7 "2020-07-15T20:45:46Z")

</div>

Yes but the rest stayed the same.

---

<div class="post-metadata">

**Author:** ![kokolegorille](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kokolegorille/32/4784_2.png) [@kokolegorille](https://forum.elixirforum.com/u/kokolegorille)\
**Post date:** [July 15, 2020, 8:46pm UTC](https://forum.elixirforum.com/t/problem-with-authenticating-websockets/33124/8 "2020-07-15T20:46:21Z")

</div>

My code is very similar…

```elixir
    const socketOptions = {
      params: { token },
      logger: (kind, msg, data) => (
        // eslint-disable-next-line no-console
        console.log(`${kind}: ${msg}`, data)
      ),
    };

```

But your log shows no token in params for the second call.

---

<div class="post-metadata">

**Author:** ![sb8244](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sb8244/32/18642_2.png) [@sb8244](https://forum.elixirforum.com/u/sb8244)\
**Post date:** [July 15, 2020, 8:59pm UTC](https://forum.elixirforum.com/t/problem-with-authenticating-websockets/33124/9 "2020-07-15T20:59:21Z")

</div>

What does the web request that the JavaScript makes to initiate the WebSocket look like? It will include `token` as a GET parameter.

Your initial request makes it look like token is being passed up, but then a second request is made that fails. Do you have 2 Socket connections by chance?

> [@km751](#):
>
> ```elixir
> [info] CONNECTED TO TweeterApiWeb.UserSocket in 0┬Ás
> Transport: :websocket
> Serializer: Phoenix.Socket.V1.JSONSerializer
> Parameters: %{"token" => "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJ0d2VldGVyX2FwaSIsImV4cCI6MTU5NjkxMjAxNiwiaWF0IjoxNTk0NDkyODE2LCJpc3MiOiJ0d2VldGVyX2FwaSIsImp0aSI6IjViYWFlMDRlLTBjMTYtNDEyMi05Y2VlLWZmMzQ2OWM1YWE1YiIsIm5iZiI6MTU5NDQ5MjgxNSwic3ViIjoiMSIsInR5cCI6ImFjY2VzcyJ9.-ZJMyyEBKd0_nHYUBGdaI0qdHn1nuWtpG8sEUHqikBuWTB2sKw9Sk36OsUpXBS5ozRpe2l2VXq8NI58HydIhZA", "vsn" => "1.0.0"}
> [debug] QUERY OK source="tweets" db=0.0ms idle=875.0ms
> SELECT t0."id", t0."content", t0."comment_count", t0."retweet_count", t0."like_count", t0."profile_id", t0."inserted_at", t0."updated_at" FROM "tweets" AS t0 WHERE (t0."profile_id" = $1) [1]
> [info] JOINED feed:1 in 0┬Ás
> Parameters: %{}
> [info] REFUSED CONNECTION TO TweeterApiWeb.UserSocket in 0┬Ás
> Transport: :websocket
> Serializer: Phoenix.Socket.V1.JSONSerializer
> Parameters: %{"vsn" => "1.0.0"}
> 
> ```

Why does `"token" => "eyJhbGciOiJIUzU..."` differ from the second call? Why are there 2 calls at all?

Few high level thoughts are that you are on an old serializer version (1 instead of 2) and using phoenix-socket library. I would suggest making sure you’re on the latest versions possible—in general a good thing—although that is likely not the actual issue at hand.

edit: Have you tried `console.log(this.token)` in the mounted function? What if it is being mounted without a token?

---

<div class="post-metadata">

**Author:** ![km751](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/km751/32/20344_2.png) [@km751](https://forum.elixirforum.com/u/km751)\
**Post date:** [July 15, 2020, 9:26pm UTC](https://forum.elixirforum.com/t/problem-with-authenticating-websockets/33124/10 "2020-07-15T21:26:12Z")

</div>

I had a second socket connection in my vuex module used in one of the child components.

Thank you for your help!
