Skip to content

Commit

Permalink
User auth0 and user marshaling improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
markdicksonjr committed Mar 25, 2019
1 parent febeaa4 commit 97625b9
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 66 deletions.
5 changes: 2 additions & 3 deletions marshalling.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion marshalling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 3 additions & 1 deletion user/auth/auth0/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:<port>/test
- should redirect to login when hitting localhost:<port>/login
- once logged in, it redirects back to localhost:<port>/callback, it will show 404, which is fine
- once logged in, it redirects back to localhost:<port>/callback, it will respond with a 200 status code
- go to localhost:<port>/test - you should see "authorized"
13 changes: 13 additions & 0 deletions user/auth/auth0/sample/sample.application.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
13 changes: 7 additions & 6 deletions user/auth/auth0/sample/sample.extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}`)
}

2 changes: 1 addition & 1 deletion user/auth/local/sample/sample.application.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
92 changes: 46 additions & 46 deletions user/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
11 changes: 3 additions & 8 deletions user/sample/sample.application.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

Expand All @@ -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())
}
}

0 comments on commit 97625b9

Please sign in to comment.