Skip to content

Commit fd32bde

Browse files
committed
fix(session): aded firstName and lastName
1 parent b0595b9 commit fd32bde

6 files changed

Lines changed: 52 additions & 19 deletions

File tree

auth_db.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,30 @@ func (a *Auth) createBuilder() *sqle.Builder {
1919
return sqle.New().Input("prefix", a.prefix)
2020
}
2121

22+
func (a *Auth) getUserByID(ctx context.Context, uid shardid.ID) (User, error) {
23+
var u User
24+
25+
err := a.db.On(uid).
26+
QueryRowBuilder(ctx, a.createBuilder().
27+
Select("<prefix>user").
28+
Where("id = {id}").Param("id", uid.Int64)).
29+
Bind(&u)
30+
31+
if err != nil {
32+
if errors.Is(err, sql.ErrNoRows) {
33+
return u, ErrUserNotFound
34+
}
35+
a.logger.Error("auth: getUserByID",
36+
slog.String("pos", "user"),
37+
slog.String("tag", "db"),
38+
slog.Int64("user_id", uid.Int64),
39+
slog.Any("err", err))
40+
return u, ErrBadDatabase
41+
}
42+
43+
return u, nil
44+
}
45+
2246
func (a *Auth) getUserByEmail(ctx context.Context, email string) (User, error) {
2347
var u User
2448

@@ -739,9 +763,11 @@ func (a *Auth) checkSignInCode(ctx context.Context, userID shardid.ID, code stri
739763
return nil
740764
}
741765

742-
func (a *Auth) createSession(ctx context.Context, userID shardid.ID) (Session, error) {
766+
func (a *Auth) createSession(ctx context.Context, userID shardid.ID, firstName, lastName string) (Session, error) {
743767
s := Session{
744-
UserID: userID.Int64,
768+
UserID: userID.Int64,
769+
FirstName: firstName,
770+
LastName: lastName,
745771
}
746772

747773
now := time.Now()

auth_session.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,10 @@ func (a *Auth) RefreshSession(ctx context.Context, refreshToken string) (Session
5959

6060
go a.deleteUserToken(ctx, uid, refreshToken) // nolint: errcheck
6161

62-
return a.createSession(ctx, uid)
62+
u, err := a.getUserByID(ctx, uid)
63+
if err != nil {
64+
return noSession, err
65+
}
66+
67+
return a.createSession(ctx, uid, u.FirstName, u.FirstName)
6368
}

auth_signin.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func (a *Auth) SignIn(ctx context.Context, email, passwd string, option LoginOpt
1111

1212
if err == nil {
1313
if verifyHash(a.hash(), u.Passwd, passwd, u.Salt) {
14-
return a.createSession(ctx, u.ID)
14+
return a.createSession(ctx, u.ID, u.FirstName, u.LastName)
1515
}
1616

1717
return noSession, ErrPasswdNotMatched
@@ -23,7 +23,7 @@ func (a *Auth) SignIn(ctx context.Context, email, passwd string, option LoginOpt
2323
return noSession, err
2424
}
2525

26-
return a.createSession(ctx, u.ID)
26+
return a.createSession(ctx, u.ID, u.FirstName, u.LastName)
2727
}
2828

2929
return noSession, err
@@ -36,7 +36,7 @@ func (a *Auth) SignInMobile(ctx context.Context, mobile, passwd string, option L
3636

3737
if err == nil {
3838
if verifyHash(a.hash(), u.Passwd, passwd, u.Salt) {
39-
return a.createSession(ctx, u.ID)
39+
return a.createSession(ctx, u.ID, u.FirstName, u.LastName)
4040
}
4141

4242
return noSession, ErrPasswdNotMatched
@@ -48,7 +48,7 @@ func (a *Auth) SignInMobile(ctx context.Context, mobile, passwd string, option L
4848
return noSession, err
4949
}
5050

51-
return a.createSession(ctx, u.ID)
51+
return a.createSession(ctx, u.ID, u.FirstName, u.LastName)
5252
}
5353

5454
return noSession, err

auth_signin_with_code.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ func (a *Auth) CreateSignInCode(ctx context.Context, email string, option LoginO
2323

2424
// SignInWithCode sign in with email and code.
2525
func (a *Auth) SignInWithCode(ctx context.Context, email, code string) (Session, error) {
26-
id, err := a.getUserIDByEmail(ctx, email)
26+
u, err := a.getUserByEmail(ctx, email)
2727
if err != nil {
2828
return noSession, err
2929
}
3030

31-
err = a.checkSignInCode(ctx, id, code)
31+
err = a.checkSignInCode(ctx, u.ID, code)
3232
if err != nil {
3333
return noSession, err
3434
}
3535

36-
return a.createSession(ctx, id)
36+
return a.createSession(ctx, u.ID, u.FirstName, u.LastName)
3737
}
3838

3939
// CreateSignInMobileCode create a code for signing in by mobile
@@ -54,15 +54,15 @@ func (a *Auth) CreateSignInMobileCode(ctx context.Context, mobile string, option
5454

5555
// SignInMobileWithCode sign in with mobile and code.
5656
func (a *Auth) SignInMobileWithCode(ctx context.Context, mobile, code string) (Session, error) {
57-
id, err := a.getUserIDByMobile(ctx, mobile)
57+
u, err := a.getUserByMobile(ctx, mobile)
5858
if err != nil {
5959
return noSession, err
6060
}
6161

62-
err = a.checkSignInCode(ctx, id, code)
62+
err = a.checkSignInCode(ctx, u.ID, code)
6363
if err != nil {
6464
return noSession, err
6565
}
6666

67-
return a.createSession(ctx, id)
67+
return a.createSession(ctx, u.ID, u.FirstName, u.LastName)
6868
}

auth_signin_with_otp.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import (
99
// SignInWithOTP sign in with email and otp.
1010
func (a *Auth) SignInWithOTP(ctx context.Context, email, otp string) (Session, error) {
1111

12-
id, err := a.getUserIDByEmail(ctx, email)
12+
u, err := a.getUserByEmail(ctx, email)
1313

1414
if err != nil {
1515
return noSession, ErrEmailNotFound
1616
}
1717

18-
pd, err := a.getUserProfileData(ctx, id)
18+
pd, err := a.getUserProfileData(ctx, u.ID)
1919
if err != nil {
2020
return noSession, err
2121
}
@@ -24,19 +24,19 @@ func (a *Auth) SignInWithOTP(ctx context.Context, email, otp string) (Session, e
2424
return noSession, ErrOTPNotMatched
2525
}
2626

27-
return a.createSession(ctx, id)
27+
return a.createSession(ctx, u.ID, u.FirstName, u.LastName)
2828

2929
}
3030

3131
// SignInMobileWithOTP sign in with mobile and otp.
3232
func (a *Auth) SignInMobileWithOTP(ctx context.Context, mobile, otp string) (Session, error) {
33-
id, err := a.getUserIDByMobile(ctx, mobile)
33+
u, err := a.getUserByMobile(ctx, mobile)
3434

3535
if err != nil {
3636
return noSession, ErrMobileNotFound
3737
}
3838

39-
pd, err := a.getUserProfileData(ctx, id)
39+
pd, err := a.getUserProfileData(ctx, u.ID)
4040
if err != nil {
4141
return noSession, err
4242
}
@@ -45,5 +45,5 @@ func (a *Auth) SignInMobileWithOTP(ctx context.Context, mobile, otp string) (Ses
4545
return noSession, ErrOTPNotMatched
4646
}
4747

48-
return a.createSession(ctx, id)
48+
return a.createSession(ctx, u.ID, u.FirstName, u.LastName)
4949
}

session.go

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

99
type Session struct {
1010
UserID int64 `json:"userID,omitempty"`
11+
FirstName string `json:"firstName,omitempty"`
12+
LastName string `json:"lastName,omitempty"`
1113
AccessToken string `json:"accessToken,omitempty"`
1214
RefreshToken string `json:"refreshToken,omitempty"`
1315
}

0 commit comments

Comments
 (0)