@@ -14,6 +14,8 @@ class Verify extends Snippet
1414 private string $ token ;
1515 private string $ service ;
1616
17+ private bool $ phoneFromSession = false ;
18+
1719 public function process ()
1820 {
1921 $ this ->sid = $ this ->modx ->getOption ('twilio.account_sid ' );
@@ -40,6 +42,12 @@ private function verifyPhone(): bool
4042 $ hook =& $ this ->sp ['hook ' ];
4143 $ code = $ hook ->getValue ('code ' );
4244 $ phone = $ this ->modx ->getPlaceholder ('twilio.phone ' );
45+ $ this ->phoneFromSession = intval ($ this ->getOption ('twilioPhoneFromSession ' , '0 ' )) === 1 ;
46+ $ twilioPersistPhone = $ this ->getOption ('twilioPersistPhone ' , '' );
47+
48+ if ($ this ->phoneFromSession ) {
49+ $ phone = $ _SESSION ['twilio_phone ' ];
50+ }
4351
4452 try {
4553 $ twilio = new Client ($ this ->sid , $ this ->token );
@@ -52,6 +60,7 @@ private function verifyPhone(): bool
5260 /** @var modUser $user */
5361 $ user = $ this ->getUser ();
5462
63+ if (empty ($ twilioPersistPhone )) {
5564 $ user ->set ('active ' , true );
5665 $ user ->_fields ['cachepwd ' ] = '' ;
5766 $ user ->setDirty ('cachepwd ' );
@@ -60,8 +69,22 @@ private function verifyPhone(): bool
6069 $ this ->modx ->invokeEvent ('OnUserActivate ' , [
6170 'user ' => &$ user ,
6271 ]);
72+ } else {
73+ if ($ twilioPersistPhone !== 'phone ' ) {
74+ $ twilioPersistPhone = 'mobilephone ' ;
75+ }
76+
77+ $ profile = $ user ->getOne ('Profile ' );
78+ $ profile ->set ($ twilioPersistPhone , $ phone );
79+ $ profile ->save ();
80+
81+ unset($ _SESSION ['twilio_phone ' ]);
82+ }
83+
84+ if (!$ this ->phoneFromSession ) {
85+ $ this ->autoLogIn ($ user );
86+ }
6387
64- $ this ->autoLogIn ($ user );
6588 $ this ->redirect ();
6689
6790 return true ;
@@ -129,6 +152,10 @@ private function verifyTotp(): bool
129152
130153 private function getUser ()
131154 {
155+ if ($ this ->phoneFromSession ) {
156+ return $ this ->modx ->user ;
157+ }
158+
132159 $ username = $ this ->base64urlDecode ($ _REQUEST ['lu ' ]);
133160 /** @var modUser $user */
134161 $ user = $ this ->modx ->getObject (modUser::class, ['username ' => $ username ]);
0 commit comments