Skip to content

Commit 0618133

Browse files
committed
fix(session): renamed Login/Logout/Register
1 parent fd32bde commit 0618133

23 files changed

Lines changed: 175 additions & 166 deletions

auth.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ var (
2323
defaultTOPTAccountName = "Auth"
2424
defaultDHTEmail = "auth:email"
2525
defaultDHTMobile = "auth:mobile"
26-
defaultSignInCodeLen = 6
27-
defaultSignInCodeTTL = 60 * time.Second
26+
defaultLoginCodeLen = 6
27+
defaultLoginCodeTTL = 60 * time.Second
2828
)
2929

3030
var (
@@ -48,8 +48,8 @@ type Auth struct {
4848
totpIssuer string
4949
totpAccountName string
5050

51-
signInCodeLen int
52-
signInCodeTTL time.Duration
51+
loginCodeSize int
52+
loginCodeTTL time.Duration
5353

5454
dhtEmail string
5555
dhtMobile string
@@ -122,12 +122,12 @@ func New(db *sqle.DB, options ...Option) *Auth {
122122
a.dhtMobile = defaultDHTMobile
123123
}
124124

125-
if a.signInCodeLen < 1 {
126-
a.signInCodeLen = defaultSignInCodeLen
125+
if a.loginCodeSize < 1 {
126+
a.loginCodeSize = defaultLoginCodeLen
127127
}
128128

129-
if a.signInCodeTTL < 1 {
130-
a.signInCodeTTL = defaultSignInCodeTTL
129+
if a.loginCodeTTL < 1 {
130+
a.loginCodeTTL = defaultLoginCodeTTL
131131
}
132132

133133
return a

auth_db.go

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -709,61 +709,57 @@ func (a *Auth) getUserProfileData(ctx context.Context, userID shardid.ID) (Profi
709709
return pd, nil
710710
}
711711

712-
func (a *Auth) createSignInCode(ctx context.Context, userID shardid.ID, ip string) (string, error) {
713-
code := randStr(a.signInCodeLen, dicNumber)
712+
func (a *Auth) createLoginCode(ctx context.Context, userID shardid.ID, userIP string) (string, error) {
713+
code := randStr(a.loginCodeSize, dicNumber)
714714

715715
now := time.Now()
716716

717717
_, err := a.db.On(userID).
718718
ExecBuilder(ctx, a.createBuilder().
719-
Insert("<prefix>signin_code").
719+
Insert("<prefix>login_code").
720720
Set("user_id", userID.Int64).
721721
Set("hash", generateHash(a.hash(), code, "")).
722-
Set("ip", ip).
723-
Set("expires_on", now.Add(a.signInCodeTTL)).
722+
Set("user_ip", userIP).
723+
Set("expires_on", now.Add(a.loginCodeTTL)).
724724
Set("created_at", now).
725725
End())
726726

727727
if err != nil {
728-
a.logger.Error("auth: createSignInCode",
728+
a.logger.Error("auth: createloginCode",
729729
slog.Int64("user_id", userID.Int64),
730730
slog.Any("err", err))
731731
return "", ErrBadDatabase
732732
}
733733
return code, nil
734734
}
735735

736-
func (a *Auth) checkSignInCode(ctx context.Context, userID shardid.ID, code string) error {
736+
func (a *Auth) getLoginCodeUserIP(ctx context.Context, userID shardid.ID, code string) (string, error) {
737737
h := generateHash(a.hash(), code, "")
738738

739-
var count int
739+
var userIP string
740740
err := a.db.On(userID).
741741
QueryRowBuilder(ctx, a.createBuilder().
742-
Select("<prefix>signin_code", "count(user_id)").
742+
Select("<prefix>login_code", "user_ip").
743743
Where("user_id = {user_id} AND hash = {hash}").
744744
Param("user_id", userID.Int64).
745745
Param("hash", h)).
746-
Scan(&count)
746+
Scan(&userIP)
747747

748748
if err != nil {
749749
if errors.Is(err, sql.ErrNoRows) {
750-
return ErrCodeNotMatched
750+
return "", ErrCodeNotMatched
751751
}
752-
a.logger.Error("auth: checkSignInCode",
752+
a.logger.Error("auth: checkloginCode",
753753
slog.Int64("user_id", userID.Int64),
754754
slog.String("code", code),
755755
slog.Any("err", err))
756-
return ErrBadDatabase
757-
}
758-
759-
if count == 0 {
760-
return ErrCodeNotMatched
756+
return "", ErrBadDatabase
761757
}
762758

763-
return nil
759+
return userIP, nil
764760
}
765761

766-
func (a *Auth) createSession(ctx context.Context, userID shardid.ID, firstName, lastName string) (Session, error) {
762+
func (a *Auth) createSession(ctx context.Context, userID shardid.ID, firstName, lastName, userIP, userAgent string) (Session, error) {
767763
s := Session{
768764
UserID: userID.Int64,
769765
FirstName: firstName,
@@ -810,6 +806,8 @@ func (a *Auth) createSession(ctx context.Context, userID shardid.ID, firstName,
810806
Insert("<prefix>user_token").
811807
Set("user_id", userID.Int64).
812808
Set("hash", hashToken(s.RefreshToken)).
809+
Set("user_ip", userID).
810+
Set("user_agent", userAgent).
813811
Set("expires_on", exp).
814812
Set("created_at", now).
815813
End())

auth_signin.go renamed to auth_login.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import (
55
"errors"
66
)
77

8-
// SignIn sign in with email and password.
9-
func (a *Auth) SignIn(ctx context.Context, email, passwd string, option LoginOption) (Session, error) {
8+
// Login sign in with email and password.
9+
func (a *Auth) Login(ctx context.Context, email, passwd string, option LoginOption) (Session, error) {
1010
u, err := a.getUserByEmail(ctx, email)
1111

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

1717
return noSession, ErrPasswdNotMatched
@@ -23,20 +23,20 @@ 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, u.FirstName, u.LastName)
26+
return a.createSession(ctx, u.ID, u.FirstName, u.LastName, option.UserIP, option.UserAgent)
2727
}
2828

2929
return noSession, err
3030

3131
}
3232

33-
// SignInMobile sign in with mobile and password.
34-
func (a *Auth) SignInMobile(ctx context.Context, mobile, passwd string, option LoginOption) (Session, error) {
33+
// LoginMobile sign in with mobile and password.
34+
func (a *Auth) LoginMobile(ctx context.Context, mobile, passwd string, option LoginOption) (Session, error) {
3535
u, err := a.getUserByMobile(ctx, mobile)
3636

3737
if err == nil {
3838
if verifyHash(a.hash(), u.Passwd, passwd, u.Salt) {
39-
return a.createSession(ctx, u.ID, u.FirstName, u.LastName)
39+
return a.createSession(ctx, u.ID, u.FirstName, u.LastName, option.UserIP, option.UserAgent)
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, u.FirstName, u.LastName)
51+
return a.createSession(ctx, u.ID, u.FirstName, u.LastName, option.UserIP, option.UserAgent)
5252
}
5353

5454
return noSession, err
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import (
88
"github.com/yaitoo/sqle/shardid"
99
)
1010

11-
func TestSignIn(t *testing.T) {
11+
func TestLogin(t *testing.T) {
1212

13-
authTest := createAuthTest("./tests_sign_in.db")
13+
authTest := createAuthTest("./tests_login.db")
1414

1515
tests := []struct {
1616
name string
@@ -73,7 +73,7 @@ func TestSignIn(t *testing.T) {
7373
}
7474
}
7575

76-
s, err := authTest.SignIn(context.TODO(), test.email, test.passwd, test.option)
76+
s, err := authTest.Login(context.TODO(), test.email, test.passwd, test.option)
7777
if test.wantedErr == nil {
7878
require.NoError(t, err)
7979
} else {
@@ -97,9 +97,9 @@ func TestSignIn(t *testing.T) {
9797
}
9898
}
9999

100-
func TestSignInMobile(t *testing.T) {
100+
func TestLoginMobile(t *testing.T) {
101101

102-
authTest := createAuthTest("./tests_sign_in_mobile.db")
102+
authTest := createAuthTest("./tests_login_mobile.db")
103103

104104
tests := []struct {
105105
name string
@@ -173,7 +173,7 @@ func TestSignInMobile(t *testing.T) {
173173
}
174174
}
175175

176-
s, err := authTest.SignInMobile(context.TODO(), test.mobile, test.passwd, test.option)
176+
s, err := authTest.LoginMobile(context.TODO(), test.mobile, test.passwd, test.option)
177177
if test.wantedErr == nil {
178178
require.NoError(t, err)
179179
} else {

auth_login_with_code.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package auth
2+
3+
import (
4+
"context"
5+
"errors"
6+
)
7+
8+
// CreateLoginCode create a code for loging in by email
9+
func (a *Auth) CreateLoginCode(ctx context.Context, email string, option LoginOption) (string, error) {
10+
id, err := a.getUserIDByEmail(ctx, email)
11+
12+
if option.CreateIfNotExists && errors.Is(err, ErrEmailNotFound) {
13+
u, err := a.createLoginWithEmail(ctx, email, randStr(12, dicAlphaNumber), option.FirstName, option.LastName)
14+
if err != nil {
15+
return "", err
16+
}
17+
18+
id = u.ID
19+
}
20+
21+
return a.createLoginCode(ctx, id, option.UserIP)
22+
}
23+
24+
// LoginWithCode sign in with email and code.
25+
func (a *Auth) LoginWithCode(ctx context.Context, email, code string) (Session, error) {
26+
u, err := a.getUserByEmail(ctx, email)
27+
if err != nil {
28+
return noSession, err
29+
}
30+
31+
userIP, err := a.getLoginCodeUserIP(ctx, u.ID, code)
32+
if err != nil {
33+
return noSession, err
34+
}
35+
36+
return a.createSession(ctx, u.ID, u.FirstName, u.LastName, userIP, "CODE")
37+
}
38+
39+
// CreateLoginMobileCode create a code for loging in by mobile
40+
func (a *Auth) CreateLoginMobileCode(ctx context.Context, mobile string, option LoginOption) (string, error) {
41+
id, err := a.getUserIDByMobile(ctx, mobile)
42+
43+
if option.CreateIfNotExists && errors.Is(err, ErrMobileNotFound) {
44+
u, err := a.createLoginWithMobile(ctx, mobile, randStr(12, dicAlphaNumber), option.FirstName, option.LastName)
45+
if err != nil {
46+
return "", err
47+
}
48+
49+
id = u.ID
50+
}
51+
52+
return a.createLoginCode(ctx, id, option.UserIP)
53+
}
54+
55+
// LoginMobileWithCode sign in with mobile and code.
56+
func (a *Auth) LoginMobileWithCode(ctx context.Context, mobile, code string) (Session, error) {
57+
u, err := a.getUserByMobile(ctx, mobile)
58+
if err != nil {
59+
return noSession, err
60+
}
61+
62+
userIP, err := a.getLoginCodeUserIP(ctx, u.ID, code)
63+
if err != nil {
64+
return noSession, err
65+
}
66+
67+
return a.createSession(ctx, u.ID, u.FirstName, u.LastName, userIP, "CODE")
68+
}
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import (
88
"github.com/yaitoo/sqle/shardid"
99
)
1010

11-
func TestSignInWithCode(t *testing.T) {
11+
func TestLoginWithCode(t *testing.T) {
1212

13-
authTest := createAuthTest("./tests_sign_in_with_code.db")
13+
authTest := createAuthTest("./tests_login_with_code.db")
1414

1515
tests := []struct {
1616
name string
@@ -32,7 +32,7 @@ func TestSignInWithCode(t *testing.T) {
3232
email: "code_not_matched@sign_in_with_code.com",
3333
wantedErr: ErrCodeNotMatched,
3434
setup: func(r *require.Assertions) string {
35-
_, err := authTest.CreateSignInCode(context.Background(), "code_not_matched@sign_in_with_code.com", LoginOption{CreateIfNotExists: true})
35+
_, err := authTest.CreateLoginCode(context.Background(), "code_not_matched@sign_in_with_code.com", LoginOption{CreateIfNotExists: true})
3636
r.NoError(err)
3737

3838
return ""
@@ -42,7 +42,7 @@ func TestSignInWithCode(t *testing.T) {
4242
name: "code_should_work",
4343
email: "code@sign_in_with_code.com",
4444
setup: func(r *require.Assertions) string {
45-
code, err := authTest.CreateSignInCode(context.Background(), "code@sign_in_with_code.com", LoginOption{CreateIfNotExists: true})
45+
code, err := authTest.CreateLoginCode(context.Background(), "code@sign_in_with_code.com", LoginOption{CreateIfNotExists: true})
4646
r.NoError(err)
4747

4848
return code
@@ -58,7 +58,7 @@ func TestSignInWithCode(t *testing.T) {
5858

5959
code := test.setup(r)
6060

61-
s, err := authTest.SignInWithCode(context.TODO(), test.email, code)
61+
s, err := authTest.LoginWithCode(context.TODO(), test.email, code)
6262
if test.wantedErr == nil {
6363
require.NoError(t, err)
6464
} else {
@@ -82,9 +82,9 @@ func TestSignInWithCode(t *testing.T) {
8282
}
8383
}
8484

85-
func TestSignInMobileWithCode(t *testing.T) {
85+
func TestLoginMobileWithCode(t *testing.T) {
8686

87-
authTest := createAuthTest("./tests_sign_in_mobile_with_code.db")
87+
authTest := createAuthTest("./tests_login_mobile_with_code.db")
8888

8989
tests := []struct {
9090
name string
@@ -106,7 +106,7 @@ func TestSignInMobileWithCode(t *testing.T) {
106106
mobile: "1+333444555",
107107
wantedErr: ErrCodeNotMatched,
108108
setup: func(r *require.Assertions) string {
109-
_, err := authTest.CreateSignInMobileCode(context.Background(), "1+333444555", LoginOption{CreateIfNotExists: true})
109+
_, err := authTest.CreateLoginMobileCode(context.Background(), "1+333444555", LoginOption{CreateIfNotExists: true})
110110
r.NoError(err)
111111

112112
return ""
@@ -116,7 +116,7 @@ func TestSignInMobileWithCode(t *testing.T) {
116116
name: "code_should_work",
117117
mobile: "1+444555666",
118118
setup: func(r *require.Assertions) string {
119-
code, err := authTest.CreateSignInMobileCode(context.Background(), "1+444555666", LoginOption{CreateIfNotExists: true})
119+
code, err := authTest.CreateLoginMobileCode(context.Background(), "1+444555666", LoginOption{CreateIfNotExists: true})
120120
r.NoError(err)
121121

122122
return code
@@ -131,7 +131,7 @@ func TestSignInMobileWithCode(t *testing.T) {
131131

132132
code := test.setup(r)
133133

134-
s, err := authTest.SignInMobileWithCode(context.TODO(), test.mobile, code)
134+
s, err := authTest.LoginMobileWithCode(context.TODO(), test.mobile, code)
135135
if test.wantedErr == nil {
136136
require.NoError(t, err)
137137
} else {
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
"github.com/pquerna/otp/totp"
77
)
88

9-
// SignInWithOTP sign in with email and otp.
10-
func (a *Auth) SignInWithOTP(ctx context.Context, email, otp string) (Session, error) {
9+
// LoginWithOTP sign in with email and otp.
10+
func (a *Auth) LoginWithOTP(ctx context.Context, email, otp string) (Session, error) {
1111

1212
u, err := a.getUserByEmail(ctx, email)
1313

@@ -24,12 +24,12 @@ func (a *Auth) SignInWithOTP(ctx context.Context, email, otp string) (Session, e
2424
return noSession, ErrOTPNotMatched
2525
}
2626

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

2929
}
3030

31-
// SignInMobileWithOTP sign in with mobile and otp.
32-
func (a *Auth) SignInMobileWithOTP(ctx context.Context, mobile, otp string) (Session, error) {
31+
// LoginMobileWithOTP sign in with mobile and otp.
32+
func (a *Auth) LoginMobileWithOTP(ctx context.Context, mobile, otp string) (Session, error) {
3333
u, err := a.getUserByMobile(ctx, mobile)
3434

3535
if err != nil {
@@ -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, u.ID, u.FirstName, u.LastName)
48+
return a.createSession(ctx, u.ID, u.FirstName, u.LastName, "", "OTP")
4949
}

0 commit comments

Comments
 (0)