package middleware import ( "encoding/json" "log" "net/http" "github.com/dgrijalva/jwt-go" "strings" "github.com/jinzhu/gorm" "os" "golang.org/x/crypto/bcrypt" ) /* JWT claims struct */ type Token struct { UserId uint jwt.StandardClaims } //a struct to rep user account type Account struct { gorm.Model Email string `json:"email"` Password string `json:"password"` Token string `json:"token";sql:"-"` } //Validate incoming user details... func (account *Account) Validate() (map[string]interface{}, bool) { if !strings.Contains(account.Email, "@") { return Message(false, "Email address is required"), false } if len(account.Password) < 1 { return Message(false, "Password is required"), false } //Email must be unique temp := &Account{} //check for errors and duplicate emails err := GetDB().Table("accounts").Where("email = ?", account.Email).First(temp).Error if err != nil && err != gorm.ErrRecordNotFound { return Message(false, "Connection error. Please retry"), false } if temp.Email != "" { return Message(false, "Email address already in use by another user."), false } return Message(false, "Requirement passed"), true } func (account *Account) Create() map[string]interface{} { if resp, ok := account.Validate(); !ok { return resp } hashedPassword, _ := bcrypt.GenerateFromPassword([]byte(account.Password), bcrypt.DefaultCost) account.Password = string(hashedPassword) GetDB().Create(account) if account.ID <= 0 { return Message(false, "Failed to create account, connection error.") } //Create new JWT token for the newly registered account tk := &Token{UserId: account.ID} token := jwt.NewWithClaims(jwt.GetSigningMethod("HS256"), tk) tokenString, _ := token.SignedString([]byte(os.Getenv("token_password"))) account.Token = tokenString account.Password = "" //delete password response := Message(true, "Account has been created") response["account"] = account return response } func Login(email, password string) map[string]interface{} { account := &Account{} err := GetDB().Table("accounts").Where("email = ?", email).First(account).Error if err != nil { if err == gorm.ErrRecordNotFound { return Message(false, "Email address not found") } return Message(false, "Connection error. Please retry") } err = bcrypt.CompareHashAndPassword([]byte(account.Password), []byte(password)) if err != nil && err == bcrypt.ErrMismatchedHashAndPassword { //Password does not match! return Message(false, "Invalid login credentials. Please try again") } //Worked! Logged In account.Password = "" //Create JWT token tk := &Token{UserId: account.ID} token := jwt.NewWithClaims(jwt.GetSigningMethod("HS256"), tk) tokenString, _ := token.SignedString([]byte(os.Getenv("token_password"))) account.Token = tokenString //Store the token in the response resp := Message(true, "Logged In") resp["account"] = account return resp } func GetUser(u uint) *Account { acc := &Account{} GetDB().Table("accounts").Where("id = ?", u).First(acc) if acc.Email == "" { //User not found! return nil } acc.Password = "" return acc } var CreateAccount = func(w http.ResponseWriter, r *http.Request) { account := &Account{} err := json.NewDecoder(r.Body).Decode(account) //decode the request body into struct and failed if any error occur log.Println(err) if err != nil { Respond(w, Message(false, "Invalid request")) return } resp := account.Create() //Create account Respond(w, resp) } var Authenticate = func(w http.ResponseWriter, r *http.Request) { account := &Account{} err := json.NewDecoder(r.Body).Decode(account) //decode the request body into struct and failed if any error occur log.Println(err) if err != nil { Respond(w, Message(false, "Invalid request")) return } resp := Login(account.Email, account.Password) Respond(w, resp) } func Message(status bool, message string) map[string]interface{} { log.Println(message) return map[string]interface{}{"status": status, "message": message} } func Respond(w http.ResponseWriter, data map[string]interface{}) { log.Println(data) w.Header().Add("Content-Type", "application/json") json.NewEncoder(w).Encode(data) }