-
Hi, am using gqlgen with Echo and the JWT middleware, for graphql client am using urql, am getting a websocket error not connecting when I use JWT middleware. the routes: func Init(e *echo.Echo) {
e.POST("/login", auth.Login)
e.POST("/signup", auth.SignUp)
r := e.Group("/")
r.Use(auth.Protected())
r.Any("subscriptions", handlers.GraphqlWsHandler(db.DB))
r.GET("", handlers.PlaygroundHandler())
r.Any("query", handlers.GraphqlHandler(db.DB))
r.GET("ws", handlers.PlaygroundWsHandler())
} the protected handler func Protected() echo.MiddlewareFunc {
config := middleware.JWTConfig{
Claims: &Claims{},
SigningKey: []byte("secret"),
}
return middleware.JWTWithConfig(config)
} how am connecting to on the frontend using urql const wsClient = useMemo(
() =>
createWSClient({
url: "ws://localhost:3001/subscriptions",
connectionParams: async () => {
return { token: "Bearer " + token };
},
}),
[]
);
const client = useMemo(
() =>
createClient({
url: url + "/query",
fetchOptions: {
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + token,
},
},
exchanges: [
...defaultExchanges,
subscriptionExchange({
forwardSubscription: (operation) => ({
subscribe: (sink) => ({
unsubscribe: wsClient.subscribe(operation, sink),
}),
}),
}),
],
}),
[token]
); |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 18 replies
-
Websocket in browsers do not allow adding custom headers (ala JWT). See https://stackoverflow.com/a/4361358/2514290 Search for "Websocket + token auth" Basically you create one-time token before connecting to the websocket and when connecting to the websocket provide that token in url (as query param for example). |
Beta Was this translation helpful? Give feedback.
-
@oSethoum btw, which version on Node do you use for that client? also npm/yarn? |
Beta Was this translation helpful? Give feedback.
-
@oSethoum got it thanks. is is complete client? tried to run |
Beta Was this translation helpful? Give feedback.
Websocket in browsers do not allow adding custom headers (ala JWT). See https://stackoverflow.com/a/4361358/2514290
Search for "Websocket + token auth" Basically you create one-time token before connecting to the websocket and when connecting to the websocket provide that token in url (as query param for example).