Created
October 15, 2018 15:48
-
-
Save mubassirhayat/4ea97fc1eb31ade3e7b65b5ce7f3a9cf to your computer and use it in GitHub Desktop.
Setting Social Login through APIs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Vish\Http\Controllers\APIs; | |
use Illuminate\Http\Request; | |
use Vish\Http\Controllers\APIBaseController; | |
use Vish\Models\User; | |
use Vish\Models\SocialLogin; | |
use Validator; | |
use Auth; | |
class LoginAPIController extends APIBaseController | |
{ | |
/** | |
* Authenticates the user | |
* | |
* @param Request $request contains all the data passed through the URI or the Form | |
* @return User | |
*/ | |
public function login(Request $request) | |
{ | |
\Log::info('Login API'); | |
$validator = Validator::make($request->all(), [ | |
'email' => 'required|string|email|max:191', | |
'password' => 'required|string|min:6', | |
'device_type' => 'required|string', | |
'device_token' => 'required|string', | |
'latitude' => 'required', | |
'longitude' => 'required', | |
]); | |
if ($validator->fails()) { | |
return $this->respondBadRequest('Validation failed for parameters please review.', $validator->errors()->toArray()); | |
} | |
if (Auth::attempt(['email' => $request->email, 'password' => $request->password])) | |
{ | |
// Authentication passed... | |
$user = Auth::user(); | |
if ( ! is_null( $user->verification_code ) || $user->is_verified == 0 ) { | |
return $this->respondUnauthorized('Validation failed for parameters please review.', [ | |
'verification' => [ | |
'You are not verified yet. Please verify your identity to use our services.' | |
] | |
]); | |
} | |
$user->device_token = $request->device_token; | |
$user->device_type = $request->device_type; | |
$user->latitude = $request->latitude; | |
$user->longitude = $request->longitude; | |
$user->save(); | |
return $this->respondSuccess('User successfully logged In', $user); | |
} | |
else | |
{ | |
return $this->respondUnauthorized('Validation failed for parameters please review.', [ | |
'email' => [ | |
'Email might be incorrect.' | |
], | |
'password' => [ | |
'Password might be incorrect' | |
], | |
]); | |
} | |
} | |
public function socialLogin(Request $request) | |
{ | |
\Log::info('Social Login API'); | |
$validator = Validator::make($request->all(), [ | |
'email' => 'required|string|email|max:191', | |
'name' => 'required|string', | |
'provider_id' => 'required|string', | |
'provider' => 'required|string', | |
'device_type' => 'required|string', | |
'device_token' => 'required|string', | |
'latitude' => 'required', | |
'longitude' => 'required', | |
]); | |
if ($validator->fails()) { | |
return $this->respondBadRequest('Validation failed for parameters please review.', $validator->errors()->toArray()); | |
} | |
$socialUser = null; | |
//Check is this email present | |
$userCheck = User::where('email', '=', $request->email)->first(); | |
if(!empty($userCheck)) | |
{ | |
$socialUser = $userCheck; | |
} | |
else | |
{ | |
$sameSocialId = SocialLogin::where('provider_id', '=', $request->provider_id)->where('provider', '=', $request->provider )->first(); | |
if(empty($sameSocialId)) | |
{ | |
//There is no combination of this social id and provider, so create new one | |
$newSocialUser = User::create([ | |
'name' => $request->name, | |
'email' => $request->email, | |
'api_token' => str_random(60), | |
'is_verified' => true, | |
'is_accepted' => false, | |
'device_type' => $request->device_type, | |
'device_token' => $request->device_token, | |
'type' => "User", | |
'latitude' => $request->latitude, | |
'longitude' => $request->longitude, | |
]); | |
// Add it to social logins for recording that it was a social login | |
$socialData = new SocialLogin; | |
$socialData->provider_id = $request->provider_id; | |
$socialData->provider = $request->provider; | |
$socialData->user_id = $newSocialUser->id; | |
$socialData->save(); | |
$socialUser = $newSocialUser; | |
} | |
else | |
{ | |
//Load this existing social user | |
$socialUser = $sameSocialId->user; | |
} | |
} | |
$socialUser->device_token = $request->input('device_token'); | |
$socialUser->save(); | |
return $this->respondSuccess('User successfully logged In', $socialUser); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// User Model | |
namespace Vish\Models; | |
use Illuminate\Notifications\Notifiable; | |
use Illuminate\Foundation\Auth\User as Authenticatable; | |
use Illuminate\Database\Eloquent\SoftDeletes; | |
class User extends Authenticatable | |
{ | |
use Notifiable, SoftDeletes; | |
/** | |
* The attributes that are mass assignable. | |
* | |
* @var array | |
*/ | |
protected $fillable = [ | |
'name', | |
'email', | |
'phone', | |
'password', | |
'api_token', | |
'dob', | |
'verification_code', | |
'is_verified', | |
'is_accepted', | |
'device_type', | |
'device_token', | |
'type', | |
'avatar_url', | |
'latitude', | |
'longitude', | |
'default_tip', | |
'allow_notifications', | |
'allow_payment_notifications', | |
'text_reservation_updates', | |
'text_order_updates', | |
'allow_payment_text', | |
'email_special_offers', | |
'email_reservation_updates', | |
'email_order_updates', | |
'allow_payment_email', | |
]; | |
/** | |
* The attributes that should be hidden for arrays. | |
* | |
* @var array | |
*/ | |
protected $hidden = [ | |
'password', 'remember_token', | |
]; | |
public function orders() | |
{ | |
return $this->hasMany(\Vish\Models\Order::class, 'user_id', 'id'); | |
} | |
public function favourite() | |
{ | |
return $this->hasMany(\Vish\Models\Favorite::class, 'user_id', 'id'); | |
} | |
/** | |
* Get the ratings for the resturant. | |
*/ | |
public function managerOf() | |
{ | |
return $this->belongsTo(\Vish\Models\Restaurant::class, 'manages', 'id'); | |
} | |
/** | |
* Get the user that owns the social account. | |
*/ | |
public function socialLinks() | |
{ | |
return $this->hasMany(\Vish\Models\SocialLogin::class, 'user_id', 'id'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment