Merge branch 'master' of gitlab.com:CIEFWorldwideSdnBhd/izyim-api into zain/shipping-order

# Conflicts:
#	Dockerfile
#	app/Http/Controllers/AuthController.php
#	app/User.php
#	database/factories/UserFactory.php
#	database/migrations/2018_04_25_084609_change_email_to_mobile_from_user.php
#	database/seeds/DatabaseSeeder.php
#	public/home.html
#	public/index.html
#	public/profile-first-setup.html
#	public/verify-code-number.html
#	public/verify-phone-number.html
#	routes/api.php
#	routes/web.php
#	tests/Feature/LoginTest.php
#	tests/Feature/RegisterTest.php
This commit is contained in:
Jack Goh
2018-06-28 16:58:01 +08:00
49 changed files with 15458 additions and 263 deletions
+28 -14
View File
@@ -7,6 +7,7 @@ use App\PasswordResets;
use JWTAuth;
use Auth;
use App\Role;
use App\Company;
use Aloha\Twilio\Twilio;
use Tymon\JWTAuth\Exceptions\JWTException;
@@ -30,10 +31,14 @@ class AuthController extends Controller
public function sendVerification(Request $request){
$credentials = $request->only('phone');
$rules = [
'phone' => 'required|digits:10|unique:users'
'phone' => 'required|digits_between:10,11|unique:users'
];
$validator = Validator::make($credentials, $rules);
if($validator->fails()){
return response()->json(['success'=> false, 'message'=> 'The minimum length should be 10.' ], 400);
}
// create user if not exist
$user = User::firstOrNew([
'phone' => $request->phone
@@ -41,24 +46,24 @@ class AuthController extends Controller
$user->save();
// create token if not exist, update if exist
$token = mt_rand(0000,9999);
$token = mt_rand(0000,9999);
// for testing
// for testing
if($request->phone == '0123456789'){
$token = "1234";
}
$user_verification = $user->phoneVerification()->firstOrNew([
'user_id' => $user->id,
]);
$user_verification->token = $token;
$user_verification->save();
// TODO: send token to phone
// TODO: send token to phone
//$message = "RM0.00 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.' ]);
return response()->json(['success'=> true, 'message'=> 'A verification code has been send to your mobile number.' ], 200);
}
@@ -79,14 +84,14 @@ class AuthController extends Controller
return response()->json([
'success'=> true,
'message'=> 'Account already verified.'
]);
], 200);
}
}
else{
return response()->json([
'success'=> false,
'message'=> 'Please contact support.'
]);
], 400);
}
// check and verify valid token
@@ -99,14 +104,17 @@ class AuthController extends Controller
if (!$userToken=JWTAuth::fromUser($check_user)) {
return response()->json(['error' => 'invalid_credentials'], 401);
}
// TODO : Create company for user
$expiration = JWTAuth::setToken($userToken)->getPayload()->get('exp');
// all good so return the token
return response()->json(['success' => true, 'token' => $userToken,
'token_type' => 'bearer',
'expires_in' => $expiration - time()]);
}
return response()->json(['success'=> false, 'error'=> "Verification code is invalid."]);
return response()->json(['success'=> false, 'error'=> "Verification code is invalid."], 400);
}
/**
@@ -127,9 +135,8 @@ class AuthController extends Controller
$validator = Validator::make($credentials, $rules);
if($validator->fails()) {
return response()->json(['success'=> false, 'error'=> $validator->messages()]);
return response()->json(['success'=> false, 'error'=> $validator->messages()], 400);
}
$name = $request->name;
$password = $request->password;
$role = $request->role;
@@ -142,7 +149,14 @@ class AuthController extends Controller
// attach role to user
$user->attachRole($role);
$user->update(['name' => $name, 'password' => Hash::make($password)]);
return response()->json(['success'=> true, 'message'=> 'Profile updated.']);
// TODO : create company for user
$company = new Company;
$company->user_id = $user->id;
$company->save();
return response()->json(['success'=> true, 'message'=> 'Profile updated.'], 200);
}
@@ -170,7 +184,7 @@ class AuthController extends Controller
try {
// attempt to verify the credentials and create a token for the user
if (! $token = JWTAuth::attempt($credentials)) {
return response()->json(['success' => false, 'error' => 'We cant find an account with this credentials. Please make sure you entered the right information and you have verified your email address.'], 401);
return response()->json(['success' => false, 'error' => 'Phone or password incorrect'], 401);
}
} catch (JWTException $e) {
// something went wrong whilst attempting to encode the token