Incorrect note in authentication recipe doc
- Ngôn ngữ chính
- Go
- Star
- 10.8k
- Fork
- 1.3k
- Merge trung bình
- 2 ngày 36 phút
- Pull request đã merge (30 ngày)
- 26
Mô tả
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)
Hướng dẫn đóng góp
Đánh giá
Issue này chưa được đánh giá.