Giter Club home page Giter Club logo

Comments (3)

TerminalFi avatar TerminalFi commented on May 29, 2024 1

I agree we might put them in the JWT / Context. I think it would be great addition to this boiler plate. It is a great addition to boilerplate code. As many examples have user registration and authentication, but lack user permission examples such as "Administrator", "Editor", "Reviewer"

from go-graphql-api-boilerplate.

mununki avatar mununki commented on May 29, 2024
  1. Resolver structure

🤔I think I need more time or idea about this issue. I totally agree with your thought.

  1. Auth on some queries

Yes, it is already implemented in. Authenticate handler is parsing and validating Authorization header of request and put it into Context for resolvers to auth this request sender.

// Request Context : auth.go

func Authenticate(h http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// var userID *string

		ctx := r.Context()
		userID, err := validateAuthHeader(ctx, r)
		if err != nil {
			// should do something here
		}

		if userID != nil {
			ctx = context.WithValue(ctx, ContextKey("userID"), *userID)
		}

		h.ServeHTTP(w, r.WithContext(ctx))
	})
}

func validateAuthHeader(ctx context.Context, r *http.Request) (*string, error) {
	tokenString := r.Header.Get("Authorization")
	if tokenString == "" {
		return nil, nil
	}

	userID, err := utils.ValidateJWT(&tokenString)
	return userID, err
}

// Resolver side
func (r *Resolvers) GetMyProfile(ctx context.Context) (*GetMyProfileResponse, error) {
	userID := ctx.Value(handler.ContextKey("userID"))

	if userID == nil {
		msg := "Not Authorized"
		return &GetMyProfileResponse{Status: false, Msg: &msg, User: nil}, nil
	}
  1. Auth for group membership

I think it is possible with validateAuthHeader handler. When it checks the JWT in header and query the DB to know which user is and role, etc. Then we can put this kind of information into Context for resolvers.

What do you think?

from go-graphql-api-boilerplate.

mununki avatar mununki commented on May 29, 2024

Thanks for your suggestions.

from go-graphql-api-boilerplate.

Related Issues (4)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.