Skip to content

Authorization example using directive #1875

Answered by txbrown
theprobugmaker asked this question in Q&A
Discussion options

You must be logged in to vote

@theprobugmaker I believe I created mine based on the official docs here

If you follow the example you should be able to arrive to this.

	c := generated.Config{
		Resolvers: gql.NewResolver(),
	}

	// Schema Directive - @isAuthenticated
	c.Directives.IsAuthenticated = func(ctx context.Context, obj interface{}, next graphql.Resolver) (res interface{}, err error) {
		ctxUserID := ctx.Value(utils.ProjectContextKeys.UserCtxKey)
		if ctxUserID == nil {
			return nil, common.GqlUnauthorizedError(ctx)
		}
		return next(ctx)
	}

	h := handler.GraphQL(generated.NewExecutableSchema(c))

	return func(c *gin.Context) {
		h.ServeHTTP(c.Writer, c.Request)
	}
	```
	
	on your middleware that checks token…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@wilhelmeek
Comment options

Answer selected by wilhelmeek
Comment options

You must be logged in to vote
1 reply
@txbrown
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants
Converted from issue

This discussion was converted from issue #1874 on January 31, 2022 19:11.