Skip to content

Commit

Permalink
Improve REAEDME & example code
Browse files Browse the repository at this point in the history
  • Loading branch information
VojtechVitek committed Aug 9, 2021
1 parent 33ed226 commit d18c95f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 31 deletions.
28 changes: 10 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
**This is a fork of https://github.com/go-chi/jwtauth without the API-breaking changes introduced in https://github.com/go-chi/jwtauth/releases/tag/v1.1.0.**
**This is a fork of [github.com/go-chi/jwtauth](https://github.com/go-chi/jwtauth) without the API breaking changes introduced in [v1.1.0](https://github.com/go-chi/jwtauth/releases/tag/v1.1.0).**

- This repo uses community-driven JWT package [github.com/golang-jwt/jwt](https://github.com/golang-jwt/jwt), which is now [officially recognized](https://github.com/dgrijalva/jwt-go#this-repository-is-no-longer-maintaned) successor to the original `github.com/dgrijalva/jwt-go repository`.
- The upstream repository [switched to `github.com/lestrrat-go/jwx`](https://github.com/go-chi/jwtauth/releases/tag/v1.1.0), which is a major breaking change that might lead to lots of code refactoring.
- This repo uses community-maintained JWT package [`github.com/golang-jwt/jwt`](https://github.com/golang-jwt/jwt), which is now an [officially recognized](https://github.com/dgrijalva/jwt-go#this-repository-is-no-longer-maintaned) successor to the original `github.com/dgrijalva/jwt-go` package.
- The upstream repository [switched to `github.com/lestrrat-go/jwx`](https://github.com/go-chi/jwtauth/releases/tag/v1.1.0) package instead, which has a different API, and thus introduces major breaking changes that might lead to lots of code refactoring.

We hope this fork will save you some precious time. Enjoy!
We hope this fork will save you some of your precious time. Enjoy!

---

Expand Down Expand Up @@ -61,25 +61,17 @@ import (
"fmt"
"net/http"

"github.com/go-chi/chi"
"github.com/go-chi/chi/v5"
"github.com/golang-cz/jwtauth"
jwt "github.com/golang-jwt/jwt/v4"
)

var tokenAuth *jwtauth.JWTAuth

func init() {
tokenAuth = jwtauth.New("HS256", []byte("secret"), nil)

// For debugging/example purposes, we generate and print
// a sample jwt token with claims `user_id:123` here:
_, tokenString, _ := tokenAuth.Encode(jwt.MapClaims{"user_id": 123})
fmt.Printf("DEBUG: a sample jwt is %s\n\n", tokenString)
}
var tokenAuth = jwtauth.New("HS256", []byte("secret"), nil)

func main() {
addr := ":3333"
fmt.Printf("Starting server on %v\n", addr)
http.ListenAndServe(addr, router())
fmt.Println("Starting server on http://localhost:3333")

http.ListenAndServe(":3333", router())
}

func router() http.Handler {
Expand Down
19 changes: 6 additions & 13 deletions _example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,20 @@ import (
jwt "github.com/golang-jwt/jwt/v4"
)

var tokenAuth *jwtauth.JWTAuth
var tokenString string

func init() {
tokenAuth = jwtauth.New("HS256", []byte("secret"), nil)

// For debugging/example purposes, we generate and print
// a sample jwt token with claims `user_id:123` here:
_, tokenString, _ = tokenAuth.Encode(jwt.MapClaims{"user_id": 123})
}
var tokenAuth = jwtauth.New("HS256", []byte("secret"), nil)

func main() {
addr := ":3333"
fmt.Printf("Starting server on %v\n\n", addr)
fmt.Printf("Starting server on http://localhost:3333\n\n")

// Generate JWT token for debugging purposes.
_, tokenString, _ := tokenAuth.Encode(jwt.MapClaims{"user_id": 123})

fmt.Printf("Try the following commands in new terminal window:\n")
fmt.Printf("curl http://localhost:3333/\n")
fmt.Printf("curl http://localhost:3333/admin\n")
fmt.Printf("curl -H \"Authorization: BEARER %s\" http://localhost:3333/admin\n", tokenString)

http.ListenAndServe(addr, router())
http.ListenAndServe(":3333", router())
}

func router() http.Handler {
Expand Down

0 comments on commit d18c95f

Please sign in to comment.