added phone verification

This commit is contained in:
jack
2018-04-26 11:44:04 +08:00
parent 456b5a8028
commit a85e58af54
6 changed files with 158 additions and 30 deletions
+70 -30
View File
@@ -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."]);
}
}
+5
View File
@@ -47,4 +47,9 @@ class User extends Authenticatable implements JWTSubject
return [];
}
public function phoneVerification()
{
return $this->hasOne('App\UserVerification', 'user_id');
}
}
+18
View File
@@ -0,0 +1,18 @@
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class UserVerification extends Model
{
protected $fillable = [
'token'
];
public function user()
{
return $this->belongsTo('App\User', 'user_id');
}
}
@@ -0,0 +1,30 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddTimestampToVerification extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('user_verifications', function (Blueprint $table) {
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
@@ -0,0 +1,32 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class ChangeNullableInUserTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('name')->nullable()->change();
$table->string('password')->nullable()->change();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
+3
View File
@@ -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(){