gauth

command module
v0.0.0-...-d0a4001 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 18, 2019 License: MIT Imports: 10 Imported by: 0

README

Minimalistic Go JWT Auth example

Dependencies
  • github.com/dgrijalva/jwt-go
  • github.com/garyburd/redigo/redis
  • github.com/gorilla/mux
  • golang.org/x/crypto/bcrypt
Installation
  • Install redis
  • Clone repo to $GOPATH/src/github.com/octohedron/gauth
  • Install dependencies
$ go get
  • Set environment variables
$ export AUTH_PORT=YOUR_PORT # i.e. 8000
$ export SIGN_KEY=secret # your jwt sign key
Usage
$ go build && ./gauth

This will run the server and you can try it out with curl

Register
$ curl -X POST -F 'email=a@a.com' -F 'password=password' http://192.168.1.43:4200/register
# Should print out a token, similar to 
{ "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ... " }
Login
$ curl -X POST -F 'email=a@a.com' -F 'password=password' http://192.168.1.43:4200/login
# Should print out a token, similar to 
{ "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ... " }

Example AJAX usage

//
// Note: For this to work you might need to uncomment the CORS headers in the
// setHeaders func
//
// button, on click ... 
var formData = new FormData();
formData.append("email", "a@a.com");
formData.append("password", "hunter2");
// make the request
fetch("http://192.168.1.43:4200/login", {
  method: "POST",
  body: formData
}).then(result => {
  result.json().then(result => {
    if (result.error) {
      alert(result.error); // "Wrong password" or "Email not found"
    } else {
      alert("Your token is: " + result.access_token);
    }
  });
});

LICENSE: MIT

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL