Skip to content

Commit d9db508

Browse files
author
glyphack
committed
use http header for authentication
1 parent ee4b4af commit d9db508

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

README.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -975,8 +975,8 @@ Implementation is pretty straightforward so we skip the explanation for this.
975975
976976
977977
#### Completing Our app <a name="completing-our-app"></a>
978-
Our CreateLink mutation left incomplete because we could not authorize users back then, so let's get back to it and complete the implementation.
979-
With what we did in [authentication middleware](#authentication-middleware) we can retrieve user in resolvers using ctx argument. so in CreateLink function add these lines:
978+
Our CreateLink mutation left incomplete because we could not authorize users back then, so let's get back to it and complete the implementation. With what we have now, we can check whether the user is logged in or not by checking the Authorization HTTP header.
979+
With what we did in authentication middleware we can retrieve user in resolvers using ctx argument. so in CreateLink function add these lines:
980980
981981
`resolver.go`:
982982
```go
@@ -1073,6 +1073,37 @@ func GetAll() []Link {
10731073
```
10741074
10751075
and Our app is finally complete.
1076+
To test the endpoint navigate to localhost:8080 and write the mutation to create link:
1077+
```graphql
1078+
mutation {
1079+
createLink(input: {title: "real link!", address: "www.graphl.org"}){
1080+
user{
1081+
name
1082+
}
1083+
}
1084+
}
1085+
```
1086+
if you try it now you will get a access denied message:
1087+
```json
1088+
{
1089+
"errors": [
1090+
{
1091+
"message": "access denied",
1092+
"path": [
1093+
"createLink"
1094+
]
1095+
}
1096+
],
1097+
"data": null
1098+
}
1099+
```
1100+
So you may realize that we prevented not logged in users from submitting links, To create link now you must set the Authorization header. From the bottom select HTTP Headers button and fill it like this:
1101+
```js
1102+
{
1103+
"Authorization": "" // use your own generated token
1104+
}
1105+
```
1106+
Try again you should be able to create a new link now.
10761107
10771108
## Summary <a name="summary"></a>
10781109
Congratulations on make it to here! You've learned about gqlgen library and some Graphql fundamentals. By implementing a HackerNews clone you've learned about queries, mutations, authentication and GraphQL query language.

internal/auth/middleware.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ type contextKey struct {
1818
func Middleware() func(http.Handler) http.Handler {
1919
return func(next http.Handler) http.Handler {
2020
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
21-
c, err := r.Cookie("token")
21+
header := r.Header.Get("Authorization")
2222

2323
// Allow unauthenticated users in
24-
if err != nil || c == nil {
24+
if header == "" {
2525
next.ServeHTTP(w, r)
2626
return
2727
}
2828

2929
//validate jwt token
30-
tokenStr := c.Value
30+
tokenStr := header
3131
username, err := jwt.ParseToken(tokenStr)
3232
if err != nil {
3333
http.Error(w, "Invalid token", http.StatusForbidden)

0 commit comments

Comments
 (0)