Skip to content

Commit 0cfd168

Browse files
Moses EsanMosesEsan
authored andcommitted
Added SMS Verification
1 parent 67a2d2a commit 0cfd168

File tree

6 files changed

+124
-55
lines changed

6 files changed

+124
-55
lines changed

app/Http/Controllers/Api/AuthController.php

Lines changed: 91 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,18 @@ public function register(Request $request) {
2828
$rules = [
2929
'name' => 'required|max:255',
3030
'email' => 'required|email|max:255|unique:users',
31+
'phone_number' => 'required|phone_number|min:9|unique:users',
3132
'password' => 'required|confirmed|min:6',
33+
'v_type' => 'required',
3234
];
3335

3436
$input = $request->only(
3537
'name',
3638
'email',
39+
'phone_number',
3740
'password',
38-
'password_confirmation'
41+
'password_confirmation',
42+
'v_type'
3943
);
4044

4145
$validator = Validator::make($input, $rules);
@@ -47,57 +51,81 @@ public function register(Request $request) {
4751
return response()->json(['success'=> false, 'error'=> $error]);
4852
}
4953

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+
5165

5266
$name = $input['name'];
5367
$email = $input['email'];
68+
$phone_number = $input['phone_number'];
5469
User::create([
55-
'name' => $input['name'],
56-
'email' => $input['email'],
70+
'name' => $name,
71+
'email' => $email,
72+
'phone_number' => $phone_number,
5773
'password' => Hash::make( $input['password']),
58-
'confirmation_code' => $confirmation_code
74+
'confirmation_code' => $confirmation_code,
75+
'verification_code' => $verification_code
5976
]);
6077

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+
}
6991
}
7092

7193
/**
72-
* API Confirm User
94+
* API Verify User
7395
*
7496
* @param Request $request
7597
*/
76-
public function confirm($confirmation_code)
98+
public function verifyUser($type, $code)
7799
{
78-
if(!$confirmation_code) return "Invalid link";
100+
if(!$code) return "Invalid link/code";
79101

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+
}
81107

82108
if (!$user) return response()->json(['success'=> false, 'error'=> "User Not Found"]);
83109

84110
$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;
87113

88-
$email =$user->email;
89-
$name =$user->name;
114+
$user->save();
90115

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+
}
96125

97126
return response()->json(['success'=> true, 'message'=> 'You have successfully verified your account.']);
98127
}
99128

100-
101129
/**
102130
* API Resend Verification
103131
*
@@ -192,4 +220,40 @@ public function logout(Request $request) {
192220
JWTAuth::invalidate($request->input('token'));
193221
}
194222

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+
}
195259
}

app/Http/routes.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121
//anything goes here will be under api/
2222
Route::post('login', 'Api\AuthController@login');
2323
Route::post('register', 'Api\AuthController@register');
24-
Route::get('verify/{confirmationCode}', 'Api\AuthController@confirm');
24+
Route::get('verify/{type}/{confirmationCode}', 'Api\AuthController@verifyUser');
2525
Route::post('resend', 'Api\AuthController@resendVerification');
2626
Route::post('recover', 'Api\AuthController@recoverPassword');
2727

28+
// Route::get('send', 'Api\AuthController@sendSMSVerification');
29+
2830

2931
Route::group(['middleware' => ['jwt.auth']], function() {
3032
Route::post('logout', 'Api\AuthController@logout');

app/User.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class User extends Authenticatable
1212
* @var array
1313
*/
1414
protected $fillable = [
15-
'name', 'email', 'password', 'confirmation_code'
15+
'name', 'email', 'password', 'confirmation_code', 'verification_code'
1616
];
1717

1818
/**

composer.lock

Lines changed: 26 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

database/migrations/2014_10_12_000000_create_users_table.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ public function up()
1616
$table->increments('id');
1717
$table->string('name');
1818
$table->string('email')->unique();
19+
$table->number('phone_number')->unique();
1920
$table->string('password');
2021
$table->boolean('confirmed')->default(0);
2122
$table->string('confirmation_code')->nullable();
23+
$table->string('verification_code')->nullable();
2224
$table->rememberToken();
2325
$table->timestamps();
2426
});

resources/views/email/verify.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<div>
1818
Thanks for creating an account with Test API.
1919
Please follow the link below to verify your email address
20-
<a href="{{ URL::to('api/verify/' . $confirmation_code) }}">Verify your account</a>.
20+
<a href="{{ URL::to('api/verify/email/' . $confirmation_code) }}">Verify your account</a>.
2121

2222
</div>
2323

0 commit comments

Comments
 (0)