{"id":5452,"date":"2018-07-19T07:00:39","date_gmt":"2018-07-19T14:00:39","guid":{"rendered":"https:\/\/www.couchbase.com\/blog\/?p=5452"},"modified":"2025-06-13T18:32:51","modified_gmt":"2025-06-14T01:32:51","slug":"authorization-with-graphql-golang-and-couchbase-nosql-using-jwt","status":"publish","type":"post","link":"https:\/\/www.couchbase.com\/blog\/authorization-with-graphql-golang-and-couchbase-nosql-using-jwt\/","title":{"rendered":"Using JWT for Authorization With GraphQL, Go, and Couchbase"},"content":{"rendered":"<p>Over the past few months I&#8217;ve been writing a GraphQL series using the Go programming language. First we saw how to <a href=\"https:\/\/www.couchbase.com\/blog\/using-graphql-with-golang-and-a-nosql-database\/\" target=\"_blank\" rel=\"noopener noreferrer\">get started with GraphQL and Go<\/a>, followed by an alternative way to <a href=\"https:\/\/www.couchbase.com\/blog\/data-relationships-with-graphql-and-nosql-in-a-golang-application\/\" target=\"_blank\" rel=\"noopener noreferrer\">handle data relationships by using resolvers<\/a> on GraphQL objects. Going a step further we saw how to include <a href=\"https:\/\/www.thepolyglotdeveloper.com\/2018\/07\/jwt-authorization-graphql-api-using-golang\" target=\"_blank\" rel=\"noopener noreferrer\">JSON web tokens (JWT) for authorization<\/a> on GraphQL objects, but without a database.<\/p>\n<p>The logical next step in this GraphQL with Golang journey would be to wire up <a href=\"https:\/\/www.couchbase.com\" target=\"_blank\" rel=\"noopener noreferrer\">Couchbase<\/a> to a fully functional GraphQL powered API that includes authorization with JSON web tokens (JWT). We&#8217;re going to see how to handle account creation, JWT validation, and working with live data through <a href=\"https:\/\/www.couchbase.com\/blog\/processing-graphql-queries-with-java-spring-boot-and-nosql\/\">GraphQL queries<\/a>.<\/p>\n<p><!--more--><\/p>\n<p>Before diving into some design and development, if you haven&#8217;t seen my previous tutorials on the subject, you probably should. I wouldn&#8217;t recommend getting into the JWT side of things until you have an understanding of using <a href=\"https:\/\/graphql.org\/\" target=\"_blank\" rel=\"noopener noreferrer\">GraphQL<\/a> with Golang.<\/p>\n<h2>Including Couchbase in a GraphQL with JWT Application<\/h2>\n<p>Instead of reiterating on the process of creating a GraphQL powered application, we&#8217;re going to start from where we left off in the series. The <a href=\"https:\/\/www.thepolyglotdeveloper.com\/2018\/07\/jwt-authorization-graphql-api-using-golang\" target=\"_blank\" rel=\"noopener noreferrer\">previous JWT tutorial<\/a> in the series left us with the following code:<\/p>\n<pre class=\"lang:default decode:true \">package main\r\n\r\nimport (\r\n\t\"context\"\r\n\t\"encoding\/json\"\r\n\t\"errors\"\r\n\t\"fmt\"\r\n\t\"net\/http\"\r\n\r\n\tjwt \"github.com\/dgrijalva\/jwt-go\"\r\n\t\"github.com\/graphql-go\/graphql\"\r\n\t\"github.com\/mitchellh\/mapstructure\"\r\n)\r\n\r\ntype User struct {\r\n\tId       string `json:\"id\"`\r\n\tUsername string `json:\"username\"`\r\n\tPassword string `json:\"password\"`\r\n}\r\n\r\ntype Blog struct {\r\n\tId        string `json:\"id\"`\r\n\tTitle     string `json:\"title\"`\r\n\tContent   string `json:\"content\"`\r\n\tAuthor    string `json:\"author\"`\r\n\tPageviews int32  `json:\"pageviews\"`\r\n}\r\n\r\nvar jwtSecret []byte = []byte(\"thepolyglotdeveloper\")\r\n\r\nvar accountsMock []User = []User{\r\n\tUser{\r\n\t\tId:       \"1\",\r\n\t\tUsername: \"nraboy\",\r\n\t\tPassword: \"1234\",\r\n\t},\r\n\tUser{\r\n\t\tId:       \"2\",\r\n\t\tUsername: \"mraboy\",\r\n\t\tPassword: \"5678\",\r\n\t},\r\n}\r\n\r\nvar blogsMock []Blog = []Blog{\r\n\tBlog{\r\n\t\tId:        \"1\",\r\n\t\tAuthor:    \"nraboy\",\r\n\t\tTitle:     \"Sample Article\",\r\n\t\tContent:   \"This is a sample article written by Nic Raboy\",\r\n\t\tPageviews: 1000,\r\n\t},\r\n}\r\n\r\nvar accountType *graphql.Object = graphql.NewObject(graphql.ObjectConfig{\r\n\tName: \"Account\",\r\n\tFields: graphql.Fields{\r\n\t\t\"id\": &amp;graphql.Field{\r\n\t\t\tType: graphql.String,\r\n\t\t},\r\n\t\t\"username\": &amp;graphql.Field{\r\n\t\t\tType: graphql.String,\r\n\t\t},\r\n\t\t\"password\": &amp;graphql.Field{\r\n\t\t\tType: graphql.String,\r\n\t\t},\r\n\t},\r\n})\r\n\r\nvar blogType *graphql.Object = graphql.NewObject(graphql.ObjectConfig{\r\n\tName: \"Blog\",\r\n\tFields: graphql.Fields{\r\n\t\t\"id\": &amp;graphql.Field{\r\n\t\t\tType: graphql.String,\r\n\t\t},\r\n\t\t\"title\": &amp;graphql.Field{\r\n\t\t\tType: graphql.String,\r\n\t\t},\r\n\t\t\"content\": &amp;graphql.Field{\r\n\t\t\tType: graphql.String,\r\n\t\t},\r\n\t\t\"author\": &amp;graphql.Field{\r\n\t\t\tType: graphql.String,\r\n\t\t},\r\n\t\t\"pageviews\": &amp;graphql.Field{\r\n\t\t\tType: graphql.Int,\r\n\t\t\tResolve: func(params graphql.ResolveParams) (interface{}, error) {\r\n\t\t\t\t_, err := ValidateJWT(params.Context.Value(\"token\").(string))\r\n\t\t\t\tif err != nil {\r\n\t\t\t\t\treturn nil, err\r\n\t\t\t\t}\r\n\t\t\t\treturn params.Source.(Blog).Pageviews, nil\r\n\t\t\t},\r\n\t\t},\r\n\t},\r\n})\r\n\r\nfunc ValidateJWT(t string) (interface{}, error) {\r\n\tif t == \"\" {\r\n\t\treturn nil, errors.New(\"Authorization token must be present\")\r\n\t}\r\n\ttoken, _ := jwt.Parse(t, func(token *jwt.Token) (interface{}, error) {\r\n\t\tif _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {\r\n\t\t\treturn nil, fmt.Errorf(\"There was an error\")\r\n\t\t}\r\n\t\treturn jwtSecret, nil\r\n\t})\r\n\tif claims, ok := token.Claims.(jwt.MapClaims); ok &amp;&amp; token.Valid {\r\n\t\tvar decodedToken interface{}\r\n\t\tmapstructure.Decode(claims, &amp;decodedToken)\r\n\t\treturn decodedToken, nil\r\n\t} else {\r\n\t\treturn nil, errors.New(\"Invalid authorization token\")\r\n\t}\r\n}\r\n\r\nfunc CreateTokenEndpoint(response http.ResponseWriter, request *http.Request) {\r\n\tvar user User\r\n\t_ = json.NewDecoder(request.Body).Decode(&amp;user)\r\n\ttoken := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{\r\n\t\t\"username\": user.Username,\r\n\t\t\"password\": user.Password,\r\n\t})\r\n\ttokenString, error := token.SignedString(jwtSecret)\r\n\tif error != nil {\r\n\t\tfmt.Println(error)\r\n\t}\r\n\tresponse.Header().Set(\"content-type\", \"application\/json\")\r\n\tresponse.Write([]byte(`{ \"token\": \"` + tokenString + `\" }`))\r\n}\r\n\r\nfunc main() {\r\n\tfmt.Println(\"Starting the application at :12345...\")\r\n\trootQuery := graphql.NewObject(graphql.ObjectConfig{\r\n\t\tName: \"Query\",\r\n\t\tFields: graphql.Fields{\r\n\t\t\t\"account\": &amp;graphql.Field{\r\n\t\t\t\tType: accountType,\r\n\t\t\t\tResolve: func(params graphql.ResolveParams) (interface{}, error) {\r\n\t\t\t\t\taccount, err := ValidateJWT(params.Context.Value(\"token\").(string))\r\n\t\t\t\t\tif err != nil {\r\n\t\t\t\t\t\treturn nil, err\r\n\t\t\t\t\t}\r\n\t\t\t\t\tfor _, accountMock := range accountsMock {\r\n\t\t\t\t\t\tif accountMock.Username == account.(User).Username {\r\n\t\t\t\t\t\t\treturn accountMock, nil\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t}\r\n\t\t\t\t\treturn &amp;User{}, nil\r\n\t\t\t\t},\r\n\t\t\t},\r\n\t\t\t\"blogs\": &amp;graphql.Field{\r\n\t\t\t\tType: graphql.NewList(blogType),\r\n\t\t\t\tResolve: func(params graphql.ResolveParams) (interface{}, error) {\r\n\t\t\t\t\treturn blogsMock, nil\r\n\t\t\t\t},\r\n\t\t\t},\r\n\t\t},\r\n\t})\r\n\tschema, _ := graphql.NewSchema(graphql.SchemaConfig{\r\n\t\tQuery: rootQuery,\r\n\t})\r\n\thttp.HandleFunc(\"\/graphql\", func(response http.ResponseWriter, request *http.Request) {\r\n\t\tresult := graphql.Do(graphql.Params{\r\n\t\t\tSchema:        schema,\r\n\t\t\tRequestString: request.URL.Query().Get(\"query\"),\r\n\t\t\tContext:       context.WithValue(context.Background(), \"token\", request.URL.Query().Get(\"token\")),\r\n\t\t})\r\n\t\tjson.NewEncoder(response).Encode(result)\r\n\t})\r\n\thttp.HandleFunc(\"\/login\", CreateTokenEndpoint)\r\n\thttp.ListenAndServe(\":12345\", nil)\r\n}<\/pre>\n<p>Our goal now is to swap out all that mock data with real data that exists in Couchbase. We won&#8217;t worry about creating blog data in this tutorial, but if you want to learn about mutations, check out one of the previous tutorials.<\/p>\n<p>The obvious first step towards using dynamic data is to set up our database, Couchbase. Create the following global variable to be used in each of our GraphQL objects:<\/p>\n<pre class=\"lang:default decode:true \">var bucket *gocb.Bucket<\/pre>\n<p>With the global Bucket reference created, let&#8217;s establish a connection to our Couchbase cluster and open a bucket. This can be done in our project&#8217;s <code>main<\/code> function:<\/p>\n<pre class=\"lang:default decode:true \">cluster, _ := gocb.Connect(\"couchbase:\/\/localhost\")\r\ncluster.Authenticate(gocb.PasswordAuthenticator{Username: \"example\", Password: \"123456\"})\r\nbucket, _ = cluster.OpenBucket(\"example\", \"\")<\/pre>\n<p>The above code assumes a locally running cluster and RBAC as well as Bucket information already created and defined. If you haven&#8217;t properly configured your Couchbase instance for this application, take a moment to do so.<\/p>\n<p>Since we&#8217;re working with a NoSQL database and no longer mock data, our native Go structures need to change slightly:<\/p>\n<pre class=\"lang:default decode:true \">type User struct {\r\n\tId       string `json:\"id,omitempty\"`\r\n\tUsername string `json:\"username\"`\r\n\tPassword string `json:\"password\"`\r\n\tType     string `json:\"type\"`\r\n}\r\n\r\ntype Blog struct {\r\n\tId        string `json:\"id,omitempty\"`\r\n\tTitle     string `json:\"title\"`\r\n\tContent   string `json:\"content\"`\r\n\tAuthor    string `json:\"author\"`\r\n\tPageviews int32  `json:\"pageviews\"`\r\n\tType      string `json:\"type\"`\r\n}<\/pre>\n<p>By adding a <code>Type<\/code> property, we can write better queries because we can differentiate our data. Changing the Go data structures does not mean we need to update our GraphQL objects. What we expect to return versus what we expect to work with can be different.<\/p>\n<p>In the previous example we were generating our JSON web token with passed information. In reality, we want to generate our JWT with actual account information. To make this possible, we need to create an endpoint for account creation:<\/p>\n<pre class=\"lang:default decode:true \">func CreateAccountEndpoint(response http.ResponseWriter, request *http.Request) {\r\n\tresponse.Header().Set(\"content-type\", \"application\/json\")\r\n\tvar account User\r\n\tjson.NewDecoder(request.Body).Decode(&amp;account)\r\n\thash, _ := bcrypt.GenerateFromPassword([]byte(account.Password), 10)\r\n\taccount.Password = string(hash)\r\n\tid, _ := uuid.NewV4()\r\n\tbucket.Insert(id.String(), account, 0)\r\n\tresponse.Write([]byte(`{ \"id\": \"` + id.String() + `\" }`))\r\n}<\/pre>\n<p>The above function will take a username and password, hash the password with bcrypt, and insert it into the database. We&#8217;ll be querying the database for this account and comparing the hash with a password as a means of authentication. To do this, we should probably update our <code>CreateTokenEndpoint<\/code> function:<\/p>\n<pre class=\"lang:default decode:true \">func CreateTokenEndpoint(response http.ResponseWriter, request *http.Request) {\r\n\tresponse.Header().Set(\"content-type\", \"application\/json\")\r\n\tvar user User\r\n\t_ = json.NewDecoder(request.Body).Decode(&amp;user)\r\n\tquery := gocb.NewN1qlQuery(\"SELECT example.* FROM example WHERE type = 'account' AND username = $1\")\r\n\tvar params []interface{}\r\n\tparams = append(params, user.Username)\r\n\tresults, _ := bucket.ExecuteN1qlQuery(query, params)\r\n\tvar account User\r\n\tresults.One(&amp;account)\r\n\tif bcrypt.CompareHashAndPassword([]byte(account.Password), []byte(user.Password)) != nil {\r\n\t\tresponse.Write([]byte(`{ \"message\": \"incorrect password\" }`))\r\n\t\treturn\r\n\t}\r\n\ttoken := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{\r\n\t\t\"Username\": account.Username,\r\n\t})\r\n\ttokenString, error := token.SignedString(jwtSecret)\r\n\tif error != nil {\r\n\t\tfmt.Println(error)\r\n\t}\r\n\tresponse.Write([]byte(`{ \"token\": \"` + tokenString + `\" }`))\r\n}<\/pre>\n<p>Notice that instead of taking the passed username and password and creating a JWT from it, we&#8217;re doing a database query. If the information doesn&#8217;t match what was passed, we&#8217;ll return an error, otherwise we&#8217;ll continue to create a JWT based on our username.<\/p>\n<p>Assuming that we have a solid way to create accounts and generate JSON web tokens from them, we can begin altering our GraphQL objects to use Couchbase rather than mock data.<\/p>\n<p>Inside the <code>main<\/code> function we have a <code>rootQuery<\/code> object with a <code>blogs<\/code> query as well as an <code>account<\/code> query. We&#8217;ll be defining our <code>blogs<\/code> query first and it would look something like this:<\/p>\n<pre class=\"lang:default decode:true \">\"blogs\": &amp;graphql.Field{\r\n\tType: graphql.NewList(blogType),\r\n\tResolve: func(params graphql.ResolveParams) (interface{}, error) {\r\n\t\tquery := gocb.NewN1qlQuery(\"SELECT example.* FROM example WHERE type = 'blog'\")\r\n\t\tresults, _ := bucket.ExecuteN1qlQuery(query, nil)\r\n\t\tvar result Blog\r\n\t\tvar blogs []Blog\r\n\t\tfor results.Next(&amp;result) {\r\n\t\t\tblogs = append(blogs, result)\r\n\t\t}\r\n\t\treturn blogs, nil\r\n\t},\r\n},<\/pre>\n<p>Instead of returning a mock list of blog data we are doing a N1QL query and returning the results. The Go data structure is mapped to our GraphQL object.<\/p>\n<p>Even though we&#8217;re returning blog data through our N1QL query, the <code>pageviews<\/code> property is still protected with JWT as defined in the object.<\/p>\n<p>The final query we have looks something like this:<\/p>\n<pre class=\"lang:default decode:true \">\"account\": &amp;graphql.Field{\r\n\tType: accountType,\r\n\tResolve: func(params graphql.ResolveParams) (interface{}, error) {\r\n\t\taccount, err := ValidateJWT(params.Context.Value(\"token\").(string))\r\n\t\tif err != nil {\r\n\t\t\treturn nil, err\r\n\t\t}\r\n\t\tvar user User\r\n\t\tmapstructure.Decode(account, &amp;user)\r\n\t\tquery := gocb.NewN1qlQuery(\"SELECT example.* FROM example WHERE type = 'account' AND username = $1\")\r\n\t\tvar n1qlParams []interface{}\r\n\t\tn1qlParams = append(n1qlParams, user.Username)\r\n\t\tresults, _ := bucket.ExecuteN1qlQuery(query, n1qlParams)\r\n\t\tresults.One(&amp;user)\r\n\t\treturn user, nil\r\n\t},\r\n},<\/pre>\n<p>Notice that we&#8217;re retrieving the decoded token information and using it as a parameter in our N1QL query. This is how we can query for a particular account based on the token data, or the currently signed in user.<\/p>\n<p>Try creating some data in the database and see what happens.<\/p>\n<h2>Conclusion<\/h2>\n<p>We brought our GraphQL series with Go to a close by configuring <a href=\"https:\/\/www.couchbase.com\" target=\"_blank\" rel=\"noopener noreferrer\">Couchbase<\/a> in our JWT authorization example. In reality, adding Couchbase didn&#8217;t change any of our JWT example, it just gave us a source of data to be used. If you dig through the previous tutorials in this series, you&#8217;ll get a deep dive into GraphQL which includes querying, mutating, and protecting queries as well as pieces of data. All the things you&#8217;d expect in a production ready API, but with GraphQL instead of a traditional REST API approach.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Over the past few months I&#8217;ve been writing a GraphQL series using the Go programming language. First we saw how to get started with GraphQL and Go, followed by an alternative way to handle data relationships by using resolvers on [&hellip;]<\/p>\n","protected":false},"author":63,"featured_media":13873,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[1814,1815,1816,1820,1812],"tags":[2210,1261,2251],"ppma_author":[9032],"class_list":["post-5452","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-application-design","category-best-practices-and-tutorials","category-couchbase-server","category-golang","category-n1ql-query","tag-graphql","tag-json","tag-jwt"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v25.9 (Yoast SEO v25.9) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Using JWT for Authorization With GraphQL, Go, and Couchbase<\/title>\n<meta name=\"description\" content=\"Learn how to create a web application that uses Couchbase NoSQL, Golang, GraphQL, and JSON web tokens (JWT) for the authorization component.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.couchbase.com\/blog\/authorization-with-graphql-golang-and-couchbase-nosql-using-jwt\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Using JWT for Authorization With GraphQL, Go, and Couchbase\" \/>\n<meta property=\"og:description\" content=\"Learn how to create a web application that uses Couchbase NoSQL, Golang, GraphQL, and JSON web tokens (JWT) for the authorization component.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.couchbase.com\/blog\/authorization-with-graphql-golang-and-couchbase-nosql-using-jwt\/\" \/>\n<meta property=\"og:site_name\" content=\"The Couchbase Blog\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/thepolyglotdeveloper\" \/>\n<meta property=\"article:published_time\" content=\"2018-07-19T14:00:39+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-06-14T01:32:51+00:00\" \/>\n<meta name=\"author\" content=\"Nic Raboy, Developer Advocate, Couchbase\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@nraboy\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Nic Raboy, Developer Advocate, Couchbase\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/authorization-with-graphql-golang-and-couchbase-nosql-using-jwt\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/authorization-with-graphql-golang-and-couchbase-nosql-using-jwt\/\"},\"author\":{\"name\":\"Nic Raboy, Developer Advocate, Couchbase\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/bb545ebe83bb2d12f91095811d0a72e1\"},\"headline\":\"Using JWT for Authorization With GraphQL, Go, and Couchbase\",\"datePublished\":\"2018-07-19T14:00:39+00:00\",\"dateModified\":\"2025-06-14T01:32:51+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/authorization-with-graphql-golang-and-couchbase-nosql-using-jwt\/\"},\"wordCount\":823,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/authorization-with-graphql-golang-and-couchbase-nosql-using-jwt\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/11\/couchbase-nosql-dbaas.png\",\"keywords\":[\"graphql\",\"JSON\",\"jwt\"],\"articleSection\":[\"Application Design\",\"Best Practices and Tutorials\",\"Couchbase Server\",\"GoLang\",\"SQL++ \/ N1QL Query\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.couchbase.com\/blog\/authorization-with-graphql-golang-and-couchbase-nosql-using-jwt\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/authorization-with-graphql-golang-and-couchbase-nosql-using-jwt\/\",\"url\":\"https:\/\/www.couchbase.com\/blog\/authorization-with-graphql-golang-and-couchbase-nosql-using-jwt\/\",\"name\":\"Using JWT for Authorization With GraphQL, Go, and Couchbase\",\"isPartOf\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/authorization-with-graphql-golang-and-couchbase-nosql-using-jwt\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/authorization-with-graphql-golang-and-couchbase-nosql-using-jwt\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/11\/couchbase-nosql-dbaas.png\",\"datePublished\":\"2018-07-19T14:00:39+00:00\",\"dateModified\":\"2025-06-14T01:32:51+00:00\",\"description\":\"Learn how to create a web application that uses Couchbase NoSQL, Golang, GraphQL, and JSON web tokens (JWT) for the authorization component.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/authorization-with-graphql-golang-and-couchbase-nosql-using-jwt\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.couchbase.com\/blog\/authorization-with-graphql-golang-and-couchbase-nosql-using-jwt\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/authorization-with-graphql-golang-and-couchbase-nosql-using-jwt\/#primaryimage\",\"url\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/11\/couchbase-nosql-dbaas.png\",\"contentUrl\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/11\/couchbase-nosql-dbaas.png\",\"width\":1800,\"height\":630},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/authorization-with-graphql-golang-and-couchbase-nosql-using-jwt\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.couchbase.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Using JWT for Authorization With GraphQL, Go, and Couchbase\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#website\",\"url\":\"https:\/\/www.couchbase.com\/blog\/\",\"name\":\"The Couchbase Blog\",\"description\":\"Couchbase, the NoSQL Database\",\"publisher\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.couchbase.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#organization\",\"name\":\"The Couchbase Blog\",\"url\":\"https:\/\/www.couchbase.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2023\/04\/admin-logo.png\",\"contentUrl\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2023\/04\/admin-logo.png\",\"width\":218,\"height\":34,\"caption\":\"The Couchbase Blog\"},\"image\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/bb545ebe83bb2d12f91095811d0a72e1\",\"name\":\"Nic Raboy, Developer Advocate, Couchbase\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/image\/8863514d8bed0cf6080f23db40e00354\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/bedeb68368d4681aca4c74fe5f697f0c423b80d498ec50fd915ba018b72c101f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/bedeb68368d4681aca4c74fe5f697f0c423b80d498ec50fd915ba018b72c101f?s=96&d=mm&r=g\",\"caption\":\"Nic Raboy, Developer Advocate, Couchbase\"},\"description\":\"Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand.\",\"sameAs\":[\"https:\/\/www.thepolyglotdeveloper.com\",\"https:\/\/www.facebook.com\/thepolyglotdeveloper\",\"https:\/\/x.com\/nraboy\"],\"url\":\"https:\/\/www.couchbase.com\/blog\/author\/nic-raboy-2\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Using JWT for Authorization With GraphQL, Go, and Couchbase","description":"Learn how to create a web application that uses Couchbase NoSQL, Golang, GraphQL, and JSON web tokens (JWT) for the authorization component.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.couchbase.com\/blog\/authorization-with-graphql-golang-and-couchbase-nosql-using-jwt\/","og_locale":"en_US","og_type":"article","og_title":"Using JWT for Authorization With GraphQL, Go, and Couchbase","og_description":"Learn how to create a web application that uses Couchbase NoSQL, Golang, GraphQL, and JSON web tokens (JWT) for the authorization component.","og_url":"https:\/\/www.couchbase.com\/blog\/authorization-with-graphql-golang-and-couchbase-nosql-using-jwt\/","og_site_name":"The Couchbase Blog","article_author":"https:\/\/www.facebook.com\/thepolyglotdeveloper","article_published_time":"2018-07-19T14:00:39+00:00","article_modified_time":"2025-06-14T01:32:51+00:00","author":"Nic Raboy, Developer Advocate, Couchbase","twitter_card":"summary_large_image","twitter_creator":"@nraboy","twitter_misc":{"Written by":"Nic Raboy, Developer Advocate, Couchbase","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.couchbase.com\/blog\/authorization-with-graphql-golang-and-couchbase-nosql-using-jwt\/#article","isPartOf":{"@id":"https:\/\/www.couchbase.com\/blog\/authorization-with-graphql-golang-and-couchbase-nosql-using-jwt\/"},"author":{"name":"Nic Raboy, Developer Advocate, Couchbase","@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/bb545ebe83bb2d12f91095811d0a72e1"},"headline":"Using JWT for Authorization With GraphQL, Go, and Couchbase","datePublished":"2018-07-19T14:00:39+00:00","dateModified":"2025-06-14T01:32:51+00:00","mainEntityOfPage":{"@id":"https:\/\/www.couchbase.com\/blog\/authorization-with-graphql-golang-and-couchbase-nosql-using-jwt\/"},"wordCount":823,"commentCount":0,"publisher":{"@id":"https:\/\/www.couchbase.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.couchbase.com\/blog\/authorization-with-graphql-golang-and-couchbase-nosql-using-jwt\/#primaryimage"},"thumbnailUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/11\/couchbase-nosql-dbaas.png","keywords":["graphql","JSON","jwt"],"articleSection":["Application Design","Best Practices and Tutorials","Couchbase Server","GoLang","SQL++ \/ N1QL Query"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.couchbase.com\/blog\/authorization-with-graphql-golang-and-couchbase-nosql-using-jwt\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.couchbase.com\/blog\/authorization-with-graphql-golang-and-couchbase-nosql-using-jwt\/","url":"https:\/\/www.couchbase.com\/blog\/authorization-with-graphql-golang-and-couchbase-nosql-using-jwt\/","name":"Using JWT for Authorization With GraphQL, Go, and Couchbase","isPartOf":{"@id":"https:\/\/www.couchbase.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.couchbase.com\/blog\/authorization-with-graphql-golang-and-couchbase-nosql-using-jwt\/#primaryimage"},"image":{"@id":"https:\/\/www.couchbase.com\/blog\/authorization-with-graphql-golang-and-couchbase-nosql-using-jwt\/#primaryimage"},"thumbnailUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/11\/couchbase-nosql-dbaas.png","datePublished":"2018-07-19T14:00:39+00:00","dateModified":"2025-06-14T01:32:51+00:00","description":"Learn how to create a web application that uses Couchbase NoSQL, Golang, GraphQL, and JSON web tokens (JWT) for the authorization component.","breadcrumb":{"@id":"https:\/\/www.couchbase.com\/blog\/authorization-with-graphql-golang-and-couchbase-nosql-using-jwt\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.couchbase.com\/blog\/authorization-with-graphql-golang-and-couchbase-nosql-using-jwt\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.couchbase.com\/blog\/authorization-with-graphql-golang-and-couchbase-nosql-using-jwt\/#primaryimage","url":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/11\/couchbase-nosql-dbaas.png","contentUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/11\/couchbase-nosql-dbaas.png","width":1800,"height":630},{"@type":"BreadcrumbList","@id":"https:\/\/www.couchbase.com\/blog\/authorization-with-graphql-golang-and-couchbase-nosql-using-jwt\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.couchbase.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Using JWT for Authorization With GraphQL, Go, and Couchbase"}]},{"@type":"WebSite","@id":"https:\/\/www.couchbase.com\/blog\/#website","url":"https:\/\/www.couchbase.com\/blog\/","name":"The Couchbase Blog","description":"Couchbase, the NoSQL Database","publisher":{"@id":"https:\/\/www.couchbase.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.couchbase.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.couchbase.com\/blog\/#organization","name":"The Couchbase Blog","url":"https:\/\/www.couchbase.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2023\/04\/admin-logo.png","contentUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2023\/04\/admin-logo.png","width":218,"height":34,"caption":"The Couchbase Blog"},"image":{"@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/bb545ebe83bb2d12f91095811d0a72e1","name":"Nic Raboy, Developer Advocate, Couchbase","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/image\/8863514d8bed0cf6080f23db40e00354","url":"https:\/\/secure.gravatar.com\/avatar\/bedeb68368d4681aca4c74fe5f697f0c423b80d498ec50fd915ba018b72c101f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/bedeb68368d4681aca4c74fe5f697f0c423b80d498ec50fd915ba018b72c101f?s=96&d=mm&r=g","caption":"Nic Raboy, Developer Advocate, Couchbase"},"description":"Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand.","sameAs":["https:\/\/www.thepolyglotdeveloper.com","https:\/\/www.facebook.com\/thepolyglotdeveloper","https:\/\/x.com\/nraboy"],"url":"https:\/\/www.couchbase.com\/blog\/author\/nic-raboy-2\/"}]}},"authors":[{"term_id":9032,"user_id":63,"is_guest":0,"slug":"nic-raboy-2","display_name":"Nic Raboy, Developer Advocate, Couchbase","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/bedeb68368d4681aca4c74fe5f697f0c423b80d498ec50fd915ba018b72c101f?s=96&d=mm&r=g","author_category":"","last_name":"Raboy","first_name":"Nic","job_title":"","user_url":"https:\/\/www.thepolyglotdeveloper.com","description":"Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand."}],"_links":{"self":[{"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/posts\/5452","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/comments?post=5452"}],"version-history":[{"count":0,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/posts\/5452\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/media\/13873"}],"wp:attachment":[{"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/media?parent=5452"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/categories?post=5452"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/tags?post=5452"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=5452"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}