Skip to content
This repository was archived by the owner on Feb 4, 2021. It is now read-only.

Commit 5a0f830

Browse files
authored
Merge pull request #74 from gedorinku/logout
ログアウト
2 parents d38b3bf + d4a374b commit 5a0f830

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

app/interceptor/session_auth.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ var (
3131
)
3232

3333
type currentUserIDKey struct{}
34+
type sessionIDKey struct{}
3435

3536
// GetCurrentUserID returns the current user's id from context
3637
func GetCurrentUserID(ctx context.Context) (id model.UserID, ok bool) {
@@ -39,6 +40,13 @@ func GetCurrentUserID(ctx context.Context) (id model.UserID, ok bool) {
3940
return
4041
}
4142

43+
// GetSessionID returns the session id from context
44+
func GetSessionID(ctx context.Context) (id string, ok bool) {
45+
v := ctx.Value(sessionIDKey{})
46+
id, ok = v.(string)
47+
return
48+
}
49+
4250
// Authorizator provide the authorization interceptor
4351
type Authorizator struct {
4452
di.StoreComponent
@@ -75,6 +83,7 @@ func (a *Authorizator) authorization(ctx context.Context, req interface{}, info
7583
}
7684

7785
newCtx := context.WithValue(ctx, currentUserIDKey{}, s.UserID)
86+
newCtx = context.WithValue(newCtx, sessionIDKey{}, sessionID)
7887
return handler(newCtx, req)
7988
}
8089

app/server/sessions_server.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313

1414
api_pb "github.com/ProgrammingLab/prolab-accounts/api"
1515
"github.com/ProgrammingLab/prolab-accounts/app/di"
16+
"github.com/ProgrammingLab/prolab-accounts/app/interceptor"
17+
"github.com/ProgrammingLab/prolab-accounts/app/util"
1618
)
1719

1820
// NewSessionServiceServer creates a new SessionServiceServer instance.
@@ -52,6 +54,17 @@ func (s *sessionServiceServerImpl) CreateSession(ctx context.Context, req *api_p
5254
}
5355

5456
func (s *sessionServiceServerImpl) DeleteSession(ctx context.Context, req *api_pb.DeleteSessionRequest) (*empty.Empty, error) {
55-
// TODO: Not yet implemented.
56-
return nil, status.Error(codes.Unimplemented, "TODO: You should implement it!")
57+
_, ok := interceptor.GetCurrentUserID(ctx)
58+
if !ok {
59+
return nil, util.ErrUnauthenticated
60+
}
61+
62+
session, _ := interceptor.GetSessionID(ctx)
63+
ss := s.SessionStore(ctx)
64+
err := ss.DeleteSession(session)
65+
if err != nil {
66+
return nil, err
67+
}
68+
69+
return &empty.Empty{}, nil
5770
}

infra/store/session/session_store.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,20 @@ func (s *sessionStoreImpl) GetSession(sessionID string) (*model.Session, error)
9292
}, nil
9393
}
9494

95+
func (s *sessionStoreImpl) DeleteSession(sessionID string) error {
96+
keys, err := s.client.Keys(redisKey(sessionID) + ":*").Result()
97+
if err != nil {
98+
return errors.WithStack(err)
99+
}
100+
101+
if len(keys) == 0 {
102+
return errors.WithStack(errSessionNotFound)
103+
}
104+
105+
_, err = s.client.Del(keys...).Result()
106+
return errors.WithStack(err)
107+
}
108+
95109
func (s *sessionStoreImpl) ResetSession(userID model.UserID) (*model.Session, error) {
96110
keys, err := s.client.Keys(fmt.Sprintf("%s:*:%v", keyPrefix, userID)).Result()
97111
if err != nil {

infra/store/session_store.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ type SessionStore interface {
77
CreateSession(nameOrEmail, password string) (*model.Session, error)
88
GetSession(sessionID string) (*model.Session, error)
99
ResetSession(userID model.UserID) (*model.Session, error)
10+
DeleteSession(sessionID string) error
1011
}

0 commit comments

Comments
 (0)