Skip to content

Commit

Permalink
Add roles to auth (login / signup) response
Browse files Browse the repository at this point in the history
Ref. SkygearIO#111

This commit also aligns the returning data
format of `me` handler with `auth:*` handlers
  • Loading branch information
Ben Lei committed Aug 22, 2016
1 parent 8a56235 commit 804011f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
11 changes: 7 additions & 4 deletions handler/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ import (
var errUserDuplicated = skyerr.NewError(skyerr.Duplicated, "user duplicated")

type authResponse struct {
UserID string `json:"user_id,omitempty"`
Username string `json:"username,omitempty"`
Email string `json:"email,omitempty"`
AccessToken string `json:"access_token,omitempty"`
UserID string `json:"user_id,omitempty"`
Username string `json:"username,omitempty"`
Email string `json:"email,omitempty"`
Roles []string `json:"roles,omitempty"`
AccessToken string `json:"access_token,omitempty"`
}

type signupPayload struct {
Expand Down Expand Up @@ -186,6 +187,7 @@ func (h *SignupHandler) Handle(payload *router.Payload, response *router.Respons
UserID: info.ID,
Username: info.Username,
Email: info.Email,
Roles: info.Roles,
AccessToken: token.AccessToken,
}
}
Expand Down Expand Up @@ -332,6 +334,7 @@ func (h *LoginHandler) Handle(payload *router.Payload, response *router.Response
UserID: info.ID,
Username: info.Username,
Email: info.Email,
Roles: info.Roles,
AccessToken: token.AccessToken,
}
}
Expand Down
8 changes: 8 additions & 0 deletions handler/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ func TestLoginHandler(t *testing.T) {

Convey("login user", func() {
userinfo := skydb.NewUserInfo("john.doe", "john.doe@example.com", "secret")
userinfo.Roles = []string{
"Programmer",
"Tester",
}
conn.CreateUser(&userinfo)

req := router.Payload{
Expand All @@ -189,10 +193,14 @@ func TestLoginHandler(t *testing.T) {
handler.Handle(&req, &resp)

So(resp.Result, ShouldHaveSameTypeAs, authResponse{})

authResp := resp.Result.(authResponse)
So(authResp.Username, ShouldEqual, "john.doe")
So(authResp.Email, ShouldEqual, "john.doe@example.com")
So(authResp.AccessToken, ShouldNotBeEmpty)
So(authResp.Roles, ShouldContain, "Programmer")
So(authResp.Roles, ShouldContain, "Tester")

token := tokenStore.Token
So(token.UserInfoID, ShouldEqual, authResp.UserID)
So(token.AccessToken, ShouldNotBeEmpty)
Expand Down
16 changes: 6 additions & 10 deletions handler/me.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,10 @@ func (h *MeHandler) Handle(payload *router.Payload, response *router.Response) {
return
}

response.Result = map[string]interface{}{
"id": userinfo.ID,
"type": "user",
"data": struct {
ID string `json:"_id"`
Email string `json:"email"`
Username string `json:"username"`
Roles []string `json:"roles,omitempty"`
}{userinfo.ID, userinfo.Email, userinfo.Username, userinfo.Roles},
}
response.Result = struct {
UserID string `json:"user_id,omitempty"`
Username string `json:"username,omitempty"`
Email string `json:"email,omitempty"`
Roles []string `json:"roles,omitempty"`
}{userinfo.ID, userinfo.Username, userinfo.Email, userinfo.Roles}
}
12 changes: 4 additions & 8 deletions handler/me_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,10 @@ func TestMeHandler(t *testing.T) {
So(resp.Code, ShouldEqual, http.StatusOK)
So(resp.Body.Bytes(), ShouldEqualJSON, `{
"result": {
"data": {
"_id": "tester-1",
"email": "tester1@example.com",
"username": "tester1",
"roles": ["Test", "Programmer"]
},
"id": "tester-1",
"type": "user"
"user_id": "tester-1",
"email": "tester1@example.com",
"username": "tester1",
"roles": ["Test", "Programmer"]
}
}`)
})
Expand Down

0 comments on commit 804011f

Please sign in to comment.