diff --git a/handler/auth.go b/handler/auth.go index 78c40468e..0f018edd6 100644 --- a/handler/auth.go +++ b/handler/auth.go @@ -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 { @@ -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, } } @@ -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, } } diff --git a/handler/auth_test.go b/handler/auth_test.go index 275f8b661..4a872efea 100644 --- a/handler/auth_test.go +++ b/handler/auth_test.go @@ -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{ @@ -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) diff --git a/handler/me.go b/handler/me.go index 3fbd34889..caffc1959 100644 --- a/handler/me.go +++ b/handler/me.go @@ -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} } diff --git a/handler/me_test.go b/handler/me_test.go index 21f938e7f..39c3343fb 100644 --- a/handler/me_test.go +++ b/handler/me_test.go @@ -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"] } }`) })