99designs / 99designs/gqlgen

Incorrect note in authentication recipe doc

未关闭
#2,474 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
Go
星标
10.8k
派生
1.3k
平均合并
2 天 36 分钟
30 天内合并 PR
26

描述

Hello!

From [authentication recipe](https://gqlgen.com/recipes/authentication/):

> Subscriptions are long lived, if your tokens can timeout or need to be refreshed you should keep the token in context too and verify it is still valid in `auth.ForContext`.

This seems incorrect. If I'm not wrong, once the subscription has been set then there is no way to call `auth.ForContext` from the subscriber's context. And as rightly pointed out by that note - subscriptions are long lived. So the token could expire meanwhile and subscription would have no way to know that.

Fortunately, @RobinCPel has pushed the code to close websocket when context is done/cancelled. So we can use `context.WithDeadline` as [suggested](https://github.com/99designs/gqlgen/issues/774#issuecomment-980033736) by them:
```go
func WebsocketAuthInitFunc(ctx context.Context, initPayload transport.InitPayload) (context.Context, error) {
...
newCtx, _ := context.WithDeadline(transport.AppendCloseReason(ctx, "authentication token has expired"), time.Unix(token.ExpiresAt, 0))
return newCtx, nil
}
```

Let's add that to the authentication recipe code and remove above note. (I'm also interested in knowing how to discard the cancel function and silence the vet)

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。