Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mubassirhayat/4ea97fc1eb31ade3e7b65b5ce7f3a9cf to your computer and use it in GitHub Desktop.
Save mubassirhayat/4ea97fc1eb31ade3e7b65b5ce7f3a9cf to your computer and use it in GitHub Desktop.
Setting Social Login through APIs
<?php
// Migration for Social Logins Table
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSocialLoginsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('social_logins', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('user_id')->unsigned()->index();
$table->string('provider');
$table->text('provider_id');
$table->timestamps();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('social_logins');
}
}
<?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);
}
}
<?php
// SocialLogin Model
namespace Vish\Models;
use Illuminate\Database\Eloquent\Model;
class SocialLogin extends Model
{
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'social_logins';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['user_id', 'provider', 'provider_id'];
/**
* Get the user that owns the social account.
*/
public function user()
{
return $this->belongsTo(\Vish\Models\User::class, 'user_id', 'id');
}
}
<?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