Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlaRod committed Jan 31, 2017
1 parent 746a4c8 commit eee8fb0
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 139 deletions.
8 changes: 4 additions & 4 deletions app/api/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
"github.com/urfave/negroni"

//Custom packages
"bitbucket.org/golang-project/todova_go_service/app/controllers"
"bitbucket.org/golang-project/todova_go_service/app/models"
"bitbucket.org/golang-project/todova_go_service/app/helpers"
"bitbucket.org/rtbathula/golang-project/app/controllers"
"bitbucket.org/rtbathula/golang-project/app/models"
"bitbucket.org/rtbathula/golang-project/app/validations"
)

// *****************************************************************************
Expand Down Expand Up @@ -63,7 +63,7 @@ var RegisterUser = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request)

/*Desc : Login Admin
Params : model User{email,password}
Returns: User or error
Returns: JWT token or error
*/
var LoginUser = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

Expand Down
73 changes: 25 additions & 48 deletions app/controllers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,42 @@ package controllers
import (
// Standard library packages
"errors"
_"net/url"
_"fmt"

// Third party packages
"gopkg.in/mgo.v2/bson"
"gopkg.in/mgo.v2"
_"github.com/mattbaird/gochimp"
_"github.com/rs/xid"
"gopkg.in/mgo.v2/bson"

//Custom packages
"bitbucket.org/golang-project/todova_go_service/app/models"
"bitbucket.org/golang-project/todova_go_service/app/helpers"
_"bitbucket.org/golang-project/todova_go_service/app/microservices"
"bitbucket.org/rtbathula/golang-project/app/models"
"bitbucket.org/rtbathula/golang-project/app/helpers"
_"bitbucket.org/rtbathula/golang-project/app/microservices"
)

// ****************************************************************************
// Controllers Logic
// *****************************************************************************

type AuthAdminResp struct {
Admin models.Admin `json:"admin"`
JwtToken string `json:"jwtToken"`
}

func RegisterUser(user models.User) (models.Response,error) {

var response models.Response
var err error

//Check email already exist
query:=bson.M{"email":admin.Details.Email}
query:=bson.M{"email":user.Email}
queryVisibleFields := bson.M{}

_, err := models.UserFindOne(query,queryVisibleFields)
_, err = models.UserFindOne(query,queryVisibleFields)
if (err == nil) {
response.Status = "error"
response.Message = "Email already exist"
return response,errors.New("Email already exist")
}

//SET Parameters to user
pwdSalt, pwdStr := helpers.EncryptPassword(admin.Details.Password)
pwdSalt, pwdStr := helpers.EncryptPassword(user.Password)
user.Password = pwdStr
user.PasswordSalt = pwdSalt

user_insert,err := models.UserInsert(user)
_,err = models.UserInsert(user)
if(err!= nil){
response.Status = "error"
response.Message = "failed to register user"
Expand All @@ -62,47 +53,33 @@ func RegisterUser(user models.User) (models.Response,error) {

func LoginUser(email string, password string)(models.Response,error){

queryEmail := bson.M{"details.email":email,"details.userType": "admin"}

var queryVisibleFields bson.M
queryVisibleFields= make(map[string]interface {})
queryVisibleFields["details.createdTime"] = 0
queryVisibleFields["details.emailConfirmationToken"] = 0
queryVisibleFields["details.createdTime"] = 0
queryVisibleFields["details.facebookAccessToken"] = 0
queryVisibleFields["details.updatedTime"] = 0
queryVisibleFields["details.resetPasswordToken"] = 0

admin,err := models.AdminFindOne(queryEmail,queryVisibleFields)
var response models.Response

if(err!= nil){
var response models.Response
queryEmail := bson.M{"email":email}
queryVisibleFields := bson.M{}

user,err := models.UserFindOne(queryEmail,queryVisibleFields)
if(err!= nil){
response.Status = "error"
response.Message = err.Error()
return response, err
}

isValid:=helpers.ValidatePassword(password,admin.Details.PasswordSalt,admin.Details.Password)
isValid:=helpers.ValidatePassword(password,user.PasswordSalt,user.Password)

if(!isValid){
var response models.Response
if(!isValid){
response.Status = "error"
response.Message = "Invalid Password!"
return response, err
}

var response models.Response
return response, errors.New("Invalid Password!")
}

response.Status = "success"
response.Message = "login successfully!"
response.Result.DataType = "json"
response.Message = "logged in successfully!"
response.Result.DataType = "string"

/* Create the token */
jwtToken,err:=helpers.MakeJwtToken(admin.Id)

loginResponse := AuthAdminResp{}
loginResponse.Admin = admin
loginResponse.JwtToken = jwtToken

response.Result.Data = loginResponse
jwtToken,err:=helpers.MakeJwtToken(user.Id)
response.Result.Data = jwtToken

return response, err
}
15 changes: 5 additions & 10 deletions app/helpers/password.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
package helpers

import (
// Standard library packages
_"encoding/json"
_"io/ioutil"
"fmt"
_"time"
// Standard library packages
"fmt"
"os"
"io"
_"reflect"
"io"
"strings"
"crypto/rand"
_"crypto/sha1"
"crypto/md5"
"crypto/rand"
"encoding/base32"

// Third party packages
//Third party packages
//Custom packages
)

Expand Down
3 changes: 1 addition & 2 deletions app/helpers/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (
"os"
"io/ioutil"
"path/filepath"
"encoding/json"
"encoding/base64"
"encoding/json"

//Third party packages
"github.com/jmoiron/jsonq"
Expand Down
2 changes: 1 addition & 1 deletion app/microservices/amazonS3.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/aws/aws-sdk-go/aws/awsutil"

//Custom packages
"bitbucket.org/golang-project/todova_go_service/app/helpers"
"bitbucket.org/rtbathula/golang-project/app/helpers"
)

func AmazonS3FileUpload(configName string, fileName string, buffer []uint8) (string,error) {
Expand Down
15 changes: 15 additions & 0 deletions app/models/helperModels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package models

type (

Response struct {
Status string `json:"status"`
Message string `json:"message"`
Result Result `json:"result"`
}

Result struct {
DataType string `json:"dataType"`
Data interface{} `json:"data"`
}
)
18 changes: 9 additions & 9 deletions app/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,27 @@ import (
"gopkg.in/mgo.v2/bson"

//Custom packages
"bitbucket.org/golang-project/todova_go_service/app/helpers"
"bitbucket.org/golang-project/todova_go_service/databases"
"bitbucket.org/rtbathula/golang-project/app/helpers"
"bitbucket.org/rtbathula/golang-project/databases"
)

type (
// User represents the structure of our resource
User struct {
Id bson.ObjectId `json:"id,omitempty" bson:"_id,omitempty"`
Email User `json:"details" bson:"details"`
Password User `json:"details" bson:"details"`
PasswordSalt User `json:"details" bson:"details"`
CreatedTime time.Time `json:"createdTime" bson:"createdTime"`
UpdatedTime time.Time `json:"updatedTime" bson:"updatedTime"`
Id bson.ObjectId `json:"id,omitempty" bson:"_id,omitempty"`
Email string `json:"email" bson:"email"`
Password string `json:"password" bson:"password"`
PasswordSalt string `json:"passwordSalt" bson:"passwordSalt"`
CreatedTime time.Time `json:"createdTime" bson:"createdTime"`
UpdatedTime time.Time `json:"updatedTime" bson:"updatedTime"`
}
)

// *****************************************************************************
// Model Methods
// *****************************************************************************

func UserInsert(user User) (ser,error) {
func UserInsert(user User) (User,error) {

//Get databaseName
keysJson := helpers.GetConfigKeys()
Expand Down
56 changes: 30 additions & 26 deletions app/validations/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,22 @@ import (
"io"
"io/ioutil"

// Third party packages
"github.com/gorilla/mux"
"github.com/crowl/rut"
// Third party packages
"github.com/asaskevich/govalidator"

//Custom packages
"bitbucket.org/golang-project/todova_go_service/app/models"
"bitbucket.org/rtbathula/golang-project/app/models"
)

func RegisterCustomer(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
func RegisterUser(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {

b := bytes.NewBuffer(make([]byte, 0))
reader := io.TeeReader(r.Body, b)

decoder := json.NewDecoder(reader)

customer := models.Customer{}
err := decoder.Decode(&customer)
var user models.User
err := decoder.Decode(&user)

r.Body = ioutil.NopCloser(b)

Expand All @@ -40,7 +38,7 @@ func RegisterCustomer(w http.ResponseWriter, r *http.Request, next http.HandlerF
return
}

if (customer.Email == "") {
if (user.Email == "") {
var response models.Response
response.Status = "error"
response.Message = "Email is required"
Expand All @@ -51,7 +49,7 @@ func RegisterCustomer(w http.ResponseWriter, r *http.Request, next http.HandlerF
return
}

if !govalidator.IsEmail(customer.Email) {
if !govalidator.IsEmail(user.Email) {
var response models.Response
response.Status = "error"
response.Message = "Email is invalid"
Expand All @@ -62,7 +60,7 @@ func RegisterCustomer(w http.ResponseWriter, r *http.Request, next http.HandlerF
return
}

if (customer.Password == "") {
if (user.Password == "") {
var response models.Response
response.Status = "error"
response.Message = "Password is required"
Expand All @@ -76,35 +74,31 @@ func RegisterCustomer(w http.ResponseWriter, r *http.Request, next http.HandlerF
next(w,r)
}

func LoginCustomer(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
func LoginUser(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {

b := bytes.NewBuffer(make([]byte, 0))
reader := io.TeeReader(r.Body, b)

type Request struct {
Email string `json:"email"`
Password string `json:"password"`
}

decoder := json.NewDecoder(reader)

req := Request{}
err := decoder.Decode(&req)
var user models.User
err := decoder.Decode(&user)

r.Body = ioutil.NopCloser(b)

var response models.Response
if err != nil {
if err != nil {
var response models.Response
response.Status = "error"
response.Message = "Parámetros inválidos!"
response.Message = "Invalid paramss"
respByt,_:= json.Marshal(response)

w.WriteHeader(400)
w.Write(respByt)
return
}

if req.Email == "" {
if (user.Email == "") {
var response models.Response
response.Status = "error"
response.Message = "Email is required"
respByt,_:= json.Marshal(response)
Expand All @@ -114,8 +108,19 @@ func LoginCustomer(w http.ResponseWriter, r *http.Request, next http.HandlerFunc
return
}

if req.Password == "" {

if !govalidator.IsEmail(user.Email) {
var response models.Response
response.Status = "error"
response.Message = "Email is invalid"
respByt,_:= json.Marshal(response)

w.WriteHeader(400)
w.Write(respByt)
return
}

if (user.Password == "") {
var response models.Response
response.Status = "error"
response.Message = "Password is required"
respByt,_:= json.Marshal(response)
Expand All @@ -125,6 +130,5 @@ func LoginCustomer(w http.ResponseWriter, r *http.Request, next http.HandlerFunc
return
}

next(w, r)
next(w,r)
}

6 changes: 4 additions & 2 deletions config/keys.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
{
"development": {
"mongoDBConnection": "mongodb://localhost",
"databaseName" : "dbname"
"databaseName" : "dbname",
"jwtSecret" : "broomBroomTodova"
},
"production": {
"mongoDBConnection": "mongodb://production:[email protected]:59208/productionDB",
"databaseName" : "dbname"
"databaseName" : "dbname",
"jwtSecret" : "broomBroomTodova"
}
}

Loading

0 comments on commit eee8fb0

Please sign in to comment.