@@ -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 ())
0 commit comments