Move Logout() to user method

This commit is contained in:
nxshock 2019-07-24 19:31:07 +05:00
parent 92f2b43f56
commit f0d2ea7f17
5 changed files with 14 additions and 26 deletions

View File

@ -10,7 +10,6 @@ import (
type Backend interface { type Backend interface {
Register(username, password, device string) (user User, token string, err *models.ApiError) Register(username, password, device string) (user User, token string, err *models.ApiError)
Login(username, password, device string) (token string, err *models.ApiError) Login(username, password, device string) (token string, err *models.ApiError)
Logout(token string) *models.ApiError
GetUserByToken(token string) (user User) GetUserByToken(token string) (user User)
Sync(token string, request sync.SyncRequest) (response *sync.SyncReply, err *models.ApiError) Sync(token string, request sync.SyncRequest) (response *sync.SyncReply, err *models.ApiError)
} }
@ -33,5 +32,6 @@ type User interface {
LeaveRoom(room Room) *models.ApiError LeaveRoom(room Room) *models.ApiError
SetTopic(room Room, topic string) *models.ApiError SetTopic(room Room, topic string) *models.ApiError
SendMessage(room Room, text string) *models.ApiError SendMessage(room Room, text string) *models.ApiError
Logout(token string)
LogoutAll() LogoutAll()
} }

View File

@ -69,22 +69,6 @@ func (backend *Backend) Login(username, password, device string) (token string,
return token, nil return token, nil
} }
func (backend *Backend) Logout(token string) *models.ApiError {
backend.mutex.Lock()
defer backend.mutex.Unlock()
for _, user := range backend.data {
for userToken, _ := range user.(*User).Tokens {
if userToken == token {
delete(user.(*User).Tokens, token)
return nil
}
}
}
return internal.NewError(models.M_UNKNOWN_TOKEN, "unknown token") // TODO: create error struct
}
func (backend *Backend) Sync(token string, request mSync.SyncRequest) (response *mSync.SyncReply, err *models.ApiError) { func (backend *Backend) Sync(token string, request mSync.SyncRequest) (response *mSync.SyncReply, err *models.ApiError) {
backend.mutex.Lock() backend.mutex.Lock()
defer backend.mutex.Unlock() defer backend.mutex.Unlock()

View File

@ -78,15 +78,14 @@ func TestLogout(t *testing.T) {
password = "password1" password = "password1"
) )
_, _, err := backend.Register(userName, password, "") user, _, err := backend.Register(userName, password, "")
assert.Nil(t, err) assert.Nil(t, err)
token, err := backend.Login(userName, password, "") token, err := backend.Login(userName, password, "")
assert.Nil(t, err) assert.Nil(t, err)
assert.NotZero(t, token) assert.NotZero(t, token)
err = backend.Logout(token) user.Logout(token)
assert.Nil(t, err)
} }
func TestLogoutWithWrongToken(t *testing.T) { func TestLogoutWithWrongToken(t *testing.T) {
@ -97,13 +96,12 @@ func TestLogoutWithWrongToken(t *testing.T) {
password = "password1" password = "password1"
) )
_, _, err := backend.Register(userName, password, "") user, _, err := backend.Register(userName, password, "")
assert.Nil(t, err) assert.Nil(t, err)
token, err := backend.Login(userName, password, "") token, err := backend.Login(userName, password, "")
assert.Nil(t, err) assert.Nil(t, err)
assert.NotZero(t, token) assert.NotZero(t, token)
err = backend.Logout("worng token") user.Logout("worng token")
assert.NotNil(t, err)
} }

View File

@ -155,6 +155,10 @@ func (user *User) SendMessage(room internal.Room, text string) *models.ApiError
return nil return nil
} }
func (user *User) Logout(token string) {
delete(user.Tokens, token)
}
func (user *User) LogoutAll() { func (user *User) LogoutAll() {
user.Tokens = make(map[string]Token) user.Tokens = make(map[string]Token)
} }

View File

@ -88,12 +88,14 @@ func LogoutHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
apiErr := currServer.Backend.Logout(token) user := currServer.Backend.GetUserByToken(token)
if apiErr != nil { if user == nil {
errorResponse(w, *apiErr, http.StatusBadRequest, "") // TODO: check code errorResponse(w, models.M_UNKNOWN_TOKEN, http.StatusBadRequest, "")
return return
} }
user.Logout(token)
sendJsonResponse(w, http.StatusOK, struct{}{}) sendJsonResponse(w, http.StatusOK, struct{}{})
} }