From 97625b9cadb07ee0baadc83f3b47964a89866110 Mon Sep 17 00:00:00 2001 From: Mark Dickson Jr Date: Mon, 25 Mar 2019 00:07:01 -0400 Subject: [PATCH] User auth0 and user marshaling improvements --- marshalling.go | 5 +- marshalling_test.go | 2 +- user/auth/auth0/README.md | 4 +- user/auth/auth0/sample/sample.application.go | 13 +++ user/auth/auth0/sample/sample.extension.go | 13 +-- user/auth/local/sample/sample.application.go | 2 +- user/model.go | 92 ++++++++++---------- user/sample/sample.application.go | 11 +-- 8 files changed, 76 insertions(+), 66 deletions(-) diff --git a/marshalling.go b/marshalling.go index 3a5231e..e670e53 100644 --- a/marshalling.go +++ b/marshalling.go @@ -6,11 +6,10 @@ import ( ) // typeOfPtr should be the type of a pointer to the type you're unmarshalling to -func FromJson(jsonString string, typeOfPtr reflect.Type) (interface{}, error) { +func FromJson(jsonString string, typeItem reflect.Type) (interface{}, error) { byteArray := []byte(jsonString) - objType := typeOfPtr.Elem() - obj := reflect.New(objType).Interface() + obj := reflect.New(typeItem).Interface() err := json.Unmarshal(byteArray, obj) return obj, err diff --git a/marshalling_test.go b/marshalling_test.go index 3f20ad5..a996fb6 100644 --- a/marshalling_test.go +++ b/marshalling_test.go @@ -33,7 +33,7 @@ func TestToJson(t *testing.T) { } func TestFromJson(t *testing.T) { - str, err := FromJson("{\"id\":\"kj\",\"createdAt\":\"2018-01-01T00:00:00Z\",\"updatedAt\":\"2018-02-01T00:00:00Z\",\"current\":7}", reflect.TypeOf(&Score{})) + str, err := FromJson("{\"id\":\"kj\",\"createdAt\":\"2018-01-01T00:00:00Z\",\"updatedAt\":\"2018-02-01T00:00:00Z\",\"current\":7}", reflect.TypeOf(Score{})) if err != nil { t.Fail() diff --git a/user/auth/auth0/README.md b/user/auth/auth0/README.md index 7b54023..ac53b63 100644 --- a/user/auth/auth0/README.md +++ b/user/auth/auth0/README.md @@ -4,7 +4,9 @@ Connects Auth0 and our user model ## Sample app +- first, change sample.application.go and set the user email to match the account in Auth0 (this module +requires the user to be in the local DB) - should return 404 / "not found" when hitting localhost:/test - should redirect to login when hitting localhost:/login -- once logged in, it redirects back to localhost:/callback, it will show 404, which is fine +- once logged in, it redirects back to localhost:/callback, it will respond with a 200 status code - go to localhost:/test - you should see "authorized" diff --git a/user/auth/auth0/sample/sample.application.go b/user/auth/auth0/sample/sample.application.go index 6fb4a0f..8f1bb4a 100644 --- a/user/auth/auth0/sample/sample.application.go +++ b/user/auth/auth0/sample/sample.application.go @@ -71,6 +71,19 @@ func main() { log.Fatal(err.Error()) } + // create a test user, if it does not exist + emailVal := "someone@example.com" + password := "" + _, errCreate := userExtension.Create(&user.User{ + Email: &emailVal, + Password: &password, + }) + + // assert the test user got created + if errCreate != nil { + log.Fatal(errCreate.Error()) + } + // start the app if err = appContext.Run(); err != nil { log.Fatal(err.Error()) diff --git a/user/auth/auth0/sample/sample.extension.go b/user/auth/auth0/sample/sample.extension.go index 5eb7bad..e3325e7 100644 --- a/user/auth/auth0/sample/sample.extension.go +++ b/user/auth/auth0/sample/sample.extension.go @@ -21,16 +21,17 @@ func (s *SampleExtension) ProtectedRoute(w http.ResponseWriter, r *http.Request) u, err := s.Auth0Extension.SessionExtension.GetCaller(r) if err != nil { - w.WriteHeader(http.StatusInternalServerError) - w.Header().Set("Content-Type", "application/json") - w.Write([]byte(`{"result": "` + err.Error() + `"}`)) + nibbler.Write500Json(w, err.Error()) + return + } + + if u == nil { + nibbler.Write404Json(w) return } log.Println(u) - w.WriteHeader(http.StatusOK) - w.Header().Set("Content-Type", "application/json") - w.Write([]byte(`{"result": "authorized"}`)) + nibbler.Write200Json(w, `{"result": "authorized"}`) } diff --git a/user/auth/local/sample/sample.application.go b/user/auth/local/sample/sample.application.go index f099023..6089c3c 100644 --- a/user/auth/local/sample/sample.application.go +++ b/user/auth/local/sample/sample.application.go @@ -106,7 +106,7 @@ func main() { } // check to see if our test user exists - emailVal := "markdicksonjr1@gmail.com" + emailVal := "markdicksonjr@gmail.com" user, errGet := userExtension.GetUserByEmail(emailVal) if errGet != nil { diff --git a/user/model.go b/user/model.go index 0af2169..c797caf 100644 --- a/user/model.go +++ b/user/model.go @@ -8,55 +8,55 @@ import ( ) type User struct { - ID string `json:"id" bson:"_id" gorm:"primary_key"` // ID - CreatedAt time.Time `json:"createdAt"` - UpdatedAt time.Time `json:"updatedAt"` - DeletedAt *time.Time `json:"deletedAt,omitempty" sql:"index"` - Email *string `json:"email,omitempty" gorm:"unique"` - Username *string `json:"username,omitempty" gorm:"unique"` - FirstName *string `json:"firstName,omitempty"` - LastName *string `json:"lastName,omitempty"` - Title *string `json:"title,omitempty"` - Password *string `json:"password,omitempty"` - PortraitUri *string `json:"portraitUri,omitempty"` - AvatarUri *string `json:"avatarUri,omitempty"` - StatusText *string `json:"statusText,omitempty"` - IsActive *bool `json:"isActive,omitempty"` - IsEmailValidated *bool `json:"isEmailValidated,omitempty"` - DeactivatedAt *time.Time `json:"deactivatedAt,omitempty"` - LastLogin *time.Time `json:"lastLogin,omitempty"` - FailedLoginCount *int8 `json:"failedLoginCount,omitempty"` - Gender *string `json:"gender,omitempty" gorm:"size:1"` - PhoneHome *string `json:"phoneHome,omitempty" gorm:"size:24"` - PhoneWork *string `json:"phoneWork,omitempty" gorm:"size:24"` - PhoneMobile *string `json:"phoneMobile,omitempty" gorm:"size:24"` - PhoneOther *string `json:"phoneOther,omitempty" gorm:"size:24"` - Fax *string `json:"fax,omitempty" gorm:"size:24"` - Uri *string `json:"uri,omitempty"` - Birthday *string `json:"birthday,omitempty"` - Bio *string `json:"bio,omitempty"` - AddressLine1 *string `json:"addressLine1,omitempty"` - AddressLine2 *string `json:"addressLine2,omitempty"` - AddressLine3 *string `json:"addressLine3,omitempty"` - AddressCity *string `json:"addressCity,omitempty"` - AddressStateOrProvince *string `json:"addressStateOrProvince,omitempty"` - AddressPostalCode *string `gorm:"size:16" json:"postalCode,omitempty"` - CountryCode *string `gorm:"size:3" json:"countryCode,omitempty"` - EmployeeId *string `json:"employeeId,omitempty"` - ReferenceId *string `json:"referenceId,omitempty"` - PasswordResetToken *string `json:"passwordResetToken,omitempty"` - PasswordResetExpiration *time.Time `json:"passwordResetExpiration,omitempty"` - EmailValidationToken *string `json:"emailValidationToken,omitempty"` - EmailValidationExpiration *time.Time `json:"emailValidationExpiration,omitempty"` - EmploymentStartDate *time.Time `json:"employmentStartDate,omitempty"` - EmploymentEndDate *time.Time `json:"employmentEndDate,omitempty"` - ContractStartDate *time.Time `json:"contractStartDate,omitempty"` - ContractEndDate *time.Time `json:"contractEndDate,omitempty"` - Context *string `json:"contractEndDate,omitempty"` + ID string `json:"id" bson:"_id" gorm:"primary_key"` // ID + CreatedAt time.Time `json:"createdAt"` + UpdatedAt time.Time `json:"updatedAt"` + DeletedAt *time.Time `json:"deletedAt,omitempty" sql:"index"` + Email *string `json:"email,omitempty" gorm:"unique"` + Username *string `json:"username,omitempty" gorm:"unique"` + FirstName *string `json:"firstName,omitempty"` + LastName *string `json:"lastName,omitempty"` + Title *string `json:"title,omitempty"` + Password *string `json:"password,omitempty"` + PortraitUri *string `json:"portraitUri,omitempty"` + AvatarUri *string `json:"avatarUri,omitempty"` + StatusText *string `json:"statusText,omitempty"` + IsActive *bool `json:"isActive,omitempty"` + IsEmailValidated *bool `json:"isEmailValidated,omitempty"` + DeactivatedAt *time.Time `json:"deactivatedAt,omitempty"` + LastLogin *time.Time `json:"lastLogin,omitempty"` + FailedLoginCount *int8 `json:"failedLoginCount,omitempty"` + Gender *string `json:"gender,omitempty" gorm:"size:1"` + PhoneHome *string `json:"phoneHome,omitempty" gorm:"size:24"` + PhoneWork *string `json:"phoneWork,omitempty" gorm:"size:24"` + PhoneMobile *string `json:"phoneMobile,omitempty" gorm:"size:24"` + PhoneOther *string `json:"phoneOther,omitempty" gorm:"size:24"` + Fax *string `json:"fax,omitempty" gorm:"size:24"` + Uri *string `json:"uri,omitempty"` + Birthday *string `json:"birthday,omitempty"` + Bio *string `json:"bio,omitempty"` + AddressLine1 *string `json:"addressLine1,omitempty"` + AddressLine2 *string `json:"addressLine2,omitempty"` + AddressLine3 *string `json:"addressLine3,omitempty"` + AddressCity *string `json:"addressCity,omitempty"` + AddressStateOrProvince *string `json:"addressStateOrProvince,omitempty"` + AddressPostalCode *string `gorm:"size:16" json:"postalCode,omitempty"` + CountryCode *string `gorm:"size:3" json:"countryCode,omitempty"` + EmployeeId *string `json:"employeeId,omitempty"` + ReferenceId *string `json:"referenceId,omitempty"` + PasswordResetToken *string `json:"passwordResetToken,omitempty"` + PasswordResetExpiration *time.Time `json:"passwordResetExpiration,omitempty"` + EmailValidationToken *string `json:"emailValidationToken,omitempty"` + EmailValidationExpiration *time.Time `json:"emailValidationExpiration,omitempty"` + EmploymentStartDate *time.Time `json:"employmentStartDate,omitempty"` + EmploymentEndDate *time.Time `json:"employmentEndDate,omitempty"` + ContractStartDate *time.Time `json:"contractStartDate,omitempty"` + ContractEndDate *time.Time `json:"contractEndDate,omitempty"` + Context *string `json:"contractEndDate,omitempty"` } func FromJson(jsonString string) (*User, error) { - userInt, err := nibbler.FromJson(jsonString, reflect.TypeOf(&User{})) + userInt, err := nibbler.FromJson(jsonString, reflect.TypeOf(User{})) return userInt.(*User), err } diff --git a/user/sample/sample.application.go b/user/sample/sample.application.go index 986a165..76bef6a 100644 --- a/user/sample/sample.application.go +++ b/user/sample/sample.application.go @@ -71,9 +71,7 @@ func main() { // initialize the application context appContext := nibbler.Application{} - err = appContext.Init(config, &logger, &extensions) - - if err != nil { + if err = appContext.Init(config, &logger, &extensions); err != nil { log.Fatal(err.Error()) } @@ -99,16 +97,13 @@ func main() { lastName := "testlast" uV.FirstName = &firstName uV.LastName = &lastName - err = sqlExtensions.UserExtension.Update(uV) - if err != nil { + if err = sqlExtensions.UserExtension.Update(uV); err != nil { log.Fatal(err.Error()) } // start the app - err = appContext.Run() - - if err != nil { + if err = appContext.Run(); err != nil { log.Fatal(err.Error()) } }