Comments (3)
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.
- Resolver structure
🤔I think I need more time or idea about this issue. I totally agree with your thought.
- 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
}
- 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.
Thanks for your suggestions.
from go-graphql-api-boilerplate.
Related Issues (4)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-graphql-api-boilerplate.