@@ -28,14 +28,18 @@ public function register(Request $request) {
28
28
$ rules = [
29
29
'name ' => 'required|max:255 ' ,
30
30
'email ' => 'required|email|max:255|unique:users ' ,
31
+ 'phone_number ' => 'required|phone_number|min:9|unique:users ' ,
31
32
'password ' => 'required|confirmed|min:6 ' ,
33
+ 'v_type ' => 'required ' ,
32
34
];
33
35
34
36
$ input = $ request ->only (
35
37
'name ' ,
36
38
'email ' ,
39
+ 'phone_number ' ,
37
40
'password ' ,
38
- 'password_confirmation '
41
+ 'password_confirmation ' ,
42
+ 'v_type '
39
43
);
40
44
41
45
$ validator = Validator::make ($ input , $ rules );
@@ -47,57 +51,81 @@ public function register(Request $request) {
47
51
return response ()->json (['success ' => false , 'error ' => $ error ]);
48
52
}
49
53
50
- $ confirmation_code = str_random (30 ); //Generate confirmation code
54
+ $ verification_code = "" ;
55
+ $ confirmation_code = "" ;
56
+
57
+ if ($ input ['v_type ' ] === "sms " ){
58
+ //SMS Verification
59
+ $ verification_code = rand (100000 , 999999 );
60
+ }elseif ($ input ['v_type ' ] === "email " ){
61
+ //Email verification
62
+ $ confirmation_code = str_random (30 ); //Generate confirmation code
63
+ }
64
+
51
65
52
66
$ name = $ input ['name ' ];
53
67
$ email = $ input ['email ' ];
68
+ $ phone_number = $ input ['phone_number ' ];
54
69
User::create ([
55
- 'name ' => $ input ['name ' ],
56
- 'email ' => $ input ['email ' ],
70
+ 'name ' => $ name ,
71
+ 'email ' => $ email ,
72
+ 'phone_number ' => $ phone_number ,
57
73
'password ' => Hash::make ( $ input ['password ' ]),
58
- 'confirmation_code ' => $ confirmation_code
74
+ 'confirmation_code ' => $ confirmation_code ,
75
+ 'verification_code ' => $ verification_code
59
76
]);
60
77
61
- Mail::send ('email.verify ' , ['confirmation_code ' => $ confirmation_code ],
62
- function ($ m ) use ($ email , $ name ){
63
- $ m ->from ('[from_email_addd] ' , 'Test API ' );
64
- $ m ->to ($ email , $ name )
65
- ->subject ('Verify your email address ' );
66
- });
67
-
68
- return response ()->json (['success ' => true , 'message ' => 'Thanks for signing up! Please check your email. ' ]);
78
+ if ($ input ['v_type ' ] === "sms " ){
79
+ //SMS Verification
80
+ $ this ->sendSMSVerification ($ verification_code ,$ phone_number );
81
+ }elseif ($ input ['v_type ' ] === "email " ){
82
+ //Email verification
83
+ Mail::send ('email.verify ' , ['confirmation_code ' => $ confirmation_code ],
84
+ function ($ m ) use ($ email , $ name ){
85
+ $ m ->from ($ _ENV ['MAIL_USERNAME ' ], 'Test API ' );
86
+ $ m ->to ($ email , $ name )
87
+ ->subject ('Verify your email address ' );
88
+ });
89
+ return response ()->json (['success ' => true , 'message ' => 'Thanks for signing up! Please check your email. ' ]);
90
+ }
69
91
}
70
92
71
93
/**
72
- * API Confirm User
94
+ * API Verify User
73
95
*
74
96
* @param Request $request
75
97
*/
76
- public function confirm ( $ confirmation_code )
98
+ public function verifyUser ( $ type , $ code )
77
99
{
78
- if (!$ confirmation_code ) return "Invalid link " ;
100
+ if (!$ code ) return "Invalid link/code " ;
79
101
80
- $ user = User::where ('confirmation_code ' , $ confirmation_code )->first ();
102
+ if ($ type === "email " ){
103
+ $ user = User::where ('confirmation_code ' , $ code )->first ();
104
+ }else {
105
+ $ user = User::where ('verification_code ' , $ code )->first ();
106
+ }
81
107
82
108
if (!$ user ) return response ()->json (['success ' => false , 'error ' => "User Not Found " ]);
83
109
84
110
$ user ->confirmed = 1 ;
85
- $ user ->confirmation_code = null ;
86
- $ user-> save () ;
111
+ if ( $ type === " email " ) $ user ->confirmation_code = null ;
112
+ else $ user = $ user -> verification_code = null ;
87
113
88
- $ email =$ user ->email ;
89
- $ name =$ user ->name ;
114
+ $ user ->save ();
90
115
91
- Mail::send ('email.welcome ' , ['email ' => $ email , 'name ' => $ name ],
92
- function ($ m ) use ($ email , $ name ) {
93
- $ m ->from ('[from_email_add] ' , 'Test API ' );
94
- $ m ->to ($ email , $ name )->subject ('Welcome To Test API ' );
95
- });
116
+ if ($ type === "email " ){
117
+ $ email =$ user ->email ;
118
+ $ name =$ user ->name ;
119
+ Mail::send ('email.welcome ' , ['email ' => $ email , 'name ' => $ name ],
120
+ function ($ m ) use ($ email , $ name ) {
121
+ $ m ->from ($ _ENV ['MAIL_USERNAME ' ], 'Test API ' );
122
+ $ m ->to ($ email , $ name )->subject ('Welcome To Test API ' );
123
+ });
124
+ }
96
125
97
126
return response ()->json (['success ' => true , 'message ' => 'You have successfully verified your account. ' ]);
98
127
}
99
128
100
-
101
129
/**
102
130
* API Resend Verification
103
131
*
@@ -192,4 +220,40 @@ public function logout(Request $request) {
192
220
JWTAuth::invalidate ($ request ->input ('token ' ));
193
221
}
194
222
223
+ public function sendSMSVerification ($ verification_code , $ phone_number ){
224
+ // public function sendSMSVerification(){
225
+
226
+ // $verification_code = rand(100000, 999999);
227
+ // $phone_number = '16318364980';
228
+
229
+ $ otp_prefix = ': ' ;
230
+
231
+ //Your message to send, Add URL encoding here.
232
+ $ message = "Hello! Welcome to TestApp. Your Verification code is $ otp_prefix $ verification_code " ;
233
+
234
+ $ url = 'https://rest.nexmo.com/sms/json? ' .http_build_query (
235
+ [
236
+ 'api_key ' => $ _ENV ['NEXMO_API_KEY ' ],
237
+ 'api_secret ' => $ _ENV ['NEXMO_API_SECRET ' ],
238
+ 'to ' => $ phone_number ,
239
+ 'from ' => $ _ENV ['NEXMO_FROM_NUMBER ' ],
240
+ 'text ' => $ message
241
+ ]
242
+ );
243
+
244
+ $ ch = curl_init ($ url );
245
+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
246
+ $ response = curl_exec ($ ch );
247
+
248
+ //Decode the json object you retrieved when you ran the request.
249
+ $ decoded_response = json_decode ($ response , true );
250
+
251
+ foreach ( $ decoded_response ['messages ' ] as $ message ) {
252
+ if ($ message ['status ' ] == 0 ) {
253
+ return response ()->json (['success ' => true , 'message ' => "Verification code sent to " .$ phone_number +'. ' ]);
254
+ } else {
255
+ return response ()->json (['success ' => false , 'error ' => "Error {$ message ['status ' ]} {$ message ['error-text ' ]}" ]);
256
+ }
257
+ }
258
+ }
195
259
}
0 commit comments