Skip to content

Commit

Permalink
Added information about it under recipes/authentication doc
Browse files Browse the repository at this point in the history
  • Loading branch information
Gisle Martin Aune committed Sep 20, 2018
1 parent 575f28e commit 25268ef
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions docs/content/recipes/authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,28 @@ func (r *queryResolver) Hero(ctx context.Context, episode Episode) (Character, e
return r.droid["2001"], nil
}
```

Things are different with websockets, and if you do things in the vein of the above example, you have to compute this at every call to `auth.ForContext`. It's a bit inefficient if you have multiple calls to this function. What you might do to mitigate that is to have a session manager on the context and have that check the vailidity in the first `auth.ForContext` call.

```golang
// ForContext finds the user from the context. REQUIRES Middleware to have run.
func ForContext(ctx context.Context) *User {
raw, ok := ctx.Value(userCtxKey).(*User)

if !ok {
payload := graphql.GetInitPayload(ctx)
if payload == nil {
return nil
}

userId, err := validateAndGetUserID(payload["token"])
if err != nil {
return nil
}

return getUserByID(db, userId)
}

return raw
}
```

0 comments on commit 25268ef

Please sign in to comment.