diff --git a/app/Http/Controllers/AuthController.php b/app/Http/Controllers/AuthController.php index dc327f6..5b5a08d 100644 --- a/app/Http/Controllers/AuthController.php +++ b/app/Http/Controllers/AuthController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; use App\User; +use App\UserVerification; use JWTAuth; use Aloha\Twilio\Twilio; use Tymon\JWTAuth\Exceptions\JWTException; @@ -15,8 +16,75 @@ class AuthController extends Controller * @param Request $request * @return \Illuminate\Http\JsonResponse */ - public function verification_code(Request $request){ - + public function sendVerification(Request $request){ + $credentials = $request->only('phone'); + $rules = [ + 'phone' => 'required|digits:10|unique:users' + ]; + $validator = Validator::make($credentials, $rules); + + // create user if not exist + $user = User::firstOrNew([ + 'phone' => $request->phone + ]); + $user->save(); + + // create token if not exist, update if exist + $token = mt_rand(0000,9999); + $user_verification = $user->phoneVerification()->firstOrNew([ + 'user_id' => $user->id, + ]); + $user_verification->token = $token; + $user_verification->save(); + + // send token to phone + // ** switching to nexmo ** + //$message = "IZYIM verification code : ". $token; + //$twilio = new Twilio(env('TWILIO_ACC'), env('TWILIO_TOKEN'), env('TWILIO_NUMBER')); + //$twilio->message($request->phone, $message); + return response()->json(['success'=> true, 'message'=> 'A verification code has been send to your mobile number.' ]); + + } + + /** + * API Verify User + * + * @param Request $request + * @return \Illuminate\Http\JsonResponse + */ + public function verifyUser(Request $request) + { + $credentials = $request->only('phone', 'token'); + + // check if user already verified + $check_user = User::where('phone', $request->phone)->first(); + if(!is_null($check_user)){ + if($check_user->is_verified == 1){ + return response()->json([ + 'success'=> true, + 'message'=> 'Account already verified.' + ]); + } + } + else{ + return response()->json([ + 'success'=> false, + 'message'=> 'Please contact support.' + ]); + } + + // check and verify valid token + $check_token = $check_user->phoneVerification()->where('token', $request->token)->first(); + if(!is_null($check_token)){ + $check_user->is_verified = 1; + $check_user->save(); + $check_token->delete(); + return response()->json([ + 'success'=> true, + 'message'=> 'Completed' + ]); + } + return response()->json(['success'=> false, 'error'=> "Verification code is invalid."]); } /** @@ -133,32 +201,4 @@ class AuthController extends Controller 'success' => true, 'data'=> ['message'=> 'A reset email has been sent! Please check your email.'] ]); } - - /** - * API Verify User - * - * @param Request $request - * @return \Illuminate\Http\JsonResponse - */ - public function verifyUser($verification_code) - { - $check = DB::table('user_verifications')->where('token',$verification_code)->first(); - if(!is_null($check)){ - $user = User::find($check->user_id); - if($user->is_verified == 1){ - return response()->json([ - 'success'=> true, - 'message'=> 'Account already verified..' - ]); - } - $user->update(['is_verified' => 1]); - DB::table('user_verifications')->where('token',$verification_code)->delete(); - return response()->json([ - 'success'=> true, - 'message'=> 'You have successfully verified your email address.' - ]); - } - return response()->json(['success'=> false, 'error'=> "Verification code is invalid."]); - } - } \ No newline at end of file diff --git a/app/User.php b/app/User.php index 4ac3ae8..947baac 100644 --- a/app/User.php +++ b/app/User.php @@ -47,4 +47,9 @@ class User extends Authenticatable implements JWTSubject return []; } + public function phoneVerification() + { + return $this->hasOne('App\UserVerification', 'user_id'); + } + } diff --git a/app/UserVerification.php b/app/UserVerification.php new file mode 100644 index 0000000..8d4b9a9 --- /dev/null +++ b/app/UserVerification.php @@ -0,0 +1,18 @@ +belongsTo('App\User', 'user_id'); + } + +} diff --git a/database/migrations/2018_04_25_145608_add_timestamp_to_verification.php b/database/migrations/2018_04_25_145608_add_timestamp_to_verification.php new file mode 100644 index 0000000..7730bd9 --- /dev/null +++ b/database/migrations/2018_04_25_145608_add_timestamp_to_verification.php @@ -0,0 +1,30 @@ +timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + } +} diff --git a/database/migrations/2018_04_26_020634_change_nullable_in_user_table.php b/database/migrations/2018_04_26_020634_change_nullable_in_user_table.php new file mode 100644 index 0000000..b3c6069 --- /dev/null +++ b/database/migrations/2018_04_26_020634_change_nullable_in_user_table.php @@ -0,0 +1,32 @@ +string('name')->nullable()->change(); + $table->string('password')->nullable()->change(); + }); + + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + } +} diff --git a/routes/api.php b/routes/api.php index aff41ed..7006184 100644 --- a/routes/api.php +++ b/routes/api.php @@ -21,6 +21,9 @@ Route::post('register', 'AuthController@register'); Route::post('login', 'AuthController@login'); Route::post('recover', 'AuthController@recover'); +Route::post('send-verification', 'AuthController@sendVerification'); +Route::post('verify', 'AuthController@verifyUser'); + Route::group(['middleware' => ['jwt.auth']], function() { Route::get('logout', 'AuthController@logout'); Route::get('test', function(){