Add test protected path

This commit is contained in:
Amarpreet Minhas 2019-02-07 23:43:26 -05:00
parent 017861422b
commit 793ad43c4b
3 changed files with 22 additions and 9 deletions

View file

@ -28,6 +28,7 @@ func main() {
// initiate jwt token // initiate jwt token
auth.TokenAuth = jwtauth.New("HS256", []byte("secret"), nil) auth.TokenAuth = jwtauth.New("HS256", []byte("secret"), nil)
users.TokenAuth = auth.TokenAuth
_, tokenString, _ := auth.TokenAuth.Encode(jwt.MapClaims{"asara": 123}) _, tokenString, _ := auth.TokenAuth.Encode(jwt.MapClaims{"asara": 123})
log.Printf("DEBUG: a sample jwt is %s\n\n", tokenString) log.Printf("DEBUG: a sample jwt is %s\n\n", tokenString)
// initiate the routes // initiate the routes
@ -59,6 +60,7 @@ func Routes() *chi.Mux {
router.Route("/v1", func(r chi.Router) { router.Route("/v1", func(r chi.Router) {
r.Mount("/api/auth", auth.Routes()) r.Mount("/api/auth", auth.Routes())
r.Mount("/api/users", users.Routes())
}) })
return router return router

View file

@ -29,12 +29,12 @@ func Init() {
func Routes() *chi.Mux { func Routes() *chi.Mux {
router := chi.NewRouter() router := chi.NewRouter()
router.Post("/signin", Signin) router.Post("/signin", signin)
router.Post("/signup", Signup) router.Post("/signup", signup)
return router return router
} }
func Signup(w http.ResponseWriter, r *http.Request) { func signup(w http.ResponseWriter, r *http.Request) {
creds := &Credentials{} creds := &Credentials{}
err := json.NewDecoder(r.Body).Decode(creds) err := json.NewDecoder(r.Body).Decode(creds)
if err != nil { if err != nil {
@ -53,7 +53,7 @@ func Signup(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusCreated) w.WriteHeader(http.StatusCreated)
} }
func Signin(w http.ResponseWriter, r *http.Request) { func signin(w http.ResponseWriter, r *http.Request) {
creds := &Credentials{} creds := &Credentials{}
err := json.NewDecoder(r.Body).Decode(creds) err := json.NewDecoder(r.Body).Decode(creds)
if err != nil { if err != nil {

View file

@ -4,12 +4,14 @@ import (
"fmt" "fmt"
"database/sql" "database/sql"
"github.com/go-chi/chi" "github.com/go-chi/chi"
"github.com/go-chi/jwtauth"
"github.com/go-chi/render" "github.com/go-chi/render"
"net/http" "net/http"
) )
var ( var (
DB *sql.DB DB *sql.DB
TokenAuth *jwtauth.JWTAuth
) )
type User struct { type User struct {
@ -29,17 +31,26 @@ func Init() {
} }
func Routes() *chi.Mux { func Routes() *chi.Mux {
router := chi.NewRouter() r := chi.NewRouter()
router.Get("/{username}", GetUser) r.Group(func(r chi.Router) {
return router r.Use(jwtauth.Verifier(TokenAuth))
r.Use(jwtauth.Authenticator)
r.Get("/{username}", getUser)
})
r.Post("/{username}", updateUser)
return r
} }
func GetUser(w http.ResponseWriter, r *http.Request) { func getUser(w http.ResponseWriter, r *http.Request) {
username := chi.URLParam(r, "username") username := chi.URLParam(r, "username")
user := User{ user := User{
Username: username, Username: username,
} }
render.JSON(w, r, user) // A chi router helper for serializing and returning json render.JSON(w, r, user)
}
func updateUser(w http.ResponseWriter, r *http.Request) {
return
} }
func CreateProfile(username string) { func CreateProfile(username string) {