Completely Login through Facebook:
Hi all, Today i am here for writing an article which is used for login your mobile app through Facebook.
Here, i am doing this using APIs. So, my backend is Laravel Framework(PHP) & my frontend is Ionic Framework(mobile app).
Backend:
Just write down these function after your login function in your model .
public function fbLogin(){
$input = Input::all(); //getting all data(name,email,fb_id) from frontend
$email = $input['email'];
$fbId = $input['fb_id'];
$firstName = $input['first_name'];
$lastName = $input['last_name'];
$user = DB::table('users')
->where('username', $email)
->orWhere('fb_id', $fbId)
->get();
$noOfRow = count($user); //getting data from table w.r.t. email
//in case where fb_id & email are exists in DB & same as getting data row will show 1, in case fb_id $email exists in DB but not both are same then case =2, in case when both does not exists case = 0
if($noOfRow==1){
DB::table('users')->where('username', $email)->update(array('fb_id' => $fbId));
$userID = $user[0]->id;
}elseif($noOfRow==2){
foreach($user as $v){
if($v->fb_id==$fbId){
$userID = $v->id;
}
}
}else{
if($email){
return $this->doLoginforFB($email,$fbId, $firstName,$lastName );
}else{
$timeStamp = md5(microtime());
$dummyEmail = $timeStamp.'@dummy.com'; //creating dummy email
return $this->doLoginforFB($dummyEmail,$fbId, $firstName,$lastName );
}
}
return $this->getUsersDetailsById($userID);
}
public function doLoginforFB($email,$fbId, $firstName,$lastName ){
$password = mt_rand(10000000, 99999999); //creating a dummy password
$hashPass = Hash::make($password);
$newUser = new User();
$newUser->username = $email;
$newUser->password = $hashPass;
$newUser->fb_id = $fbId;
$newUser->save();
$this->RegisterStep($newUser->id);
DB::table('users_details')->insert(
array('firstname' => $firstName, 'lastname' => $lastName, 'user_id'=>$newUser->id)
);
$input['username'] = $email;
$input['password'] = $password;
Input::replace($input);
$request = Request::create('oauth/login', 'POST'); //laravel way for passing parms in doLogin function or you can call here your Login function
$response = Route::dispatch($request);
return $response;
}
Frontend:
Accordingly using this i have created my Frontend.
Just follow its steps and get all the details of any user like email, fb_id , name and send it to your backend.
After that make a build and check it out if its working
Thanks
Ashish Ginotra
Hi all, Today i am here for writing an article which is used for login your mobile app through Facebook.
Here, i am doing this using APIs. So, my backend is Laravel Framework(PHP) & my frontend is Ionic Framework(mobile app).
Backend:
Just write down these function after your login function in your model .
public function fbLogin(){
$input = Input::all(); //getting all data(name,email,fb_id) from frontend
$email = $input['email'];
$fbId = $input['fb_id'];
$firstName = $input['first_name'];
$lastName = $input['last_name'];
$user = DB::table('users')
->where('username', $email)
->orWhere('fb_id', $fbId)
->get();
$noOfRow = count($user); //getting data from table w.r.t. email
//in case where fb_id & email are exists in DB & same as getting data row will show 1, in case fb_id $email exists in DB but not both are same then case =2, in case when both does not exists case = 0
if($noOfRow==1){
DB::table('users')->where('username', $email)->update(array('fb_id' => $fbId));
$userID = $user[0]->id;
}elseif($noOfRow==2){
foreach($user as $v){
if($v->fb_id==$fbId){
$userID = $v->id;
}
}
}else{
if($email){
return $this->doLoginforFB($email,$fbId, $firstName,$lastName );
}else{
$timeStamp = md5(microtime());
$dummyEmail = $timeStamp.'@dummy.com'; //creating dummy email
return $this->doLoginforFB($dummyEmail,$fbId, $firstName,$lastName );
}
}
return $this->getUsersDetailsById($userID);
}
public function doLoginforFB($email,$fbId, $firstName,$lastName ){
$password = mt_rand(10000000, 99999999); //creating a dummy password
$hashPass = Hash::make($password);
$newUser = new User();
$newUser->username = $email;
$newUser->password = $hashPass;
$newUser->fb_id = $fbId;
$newUser->save();
$this->RegisterStep($newUser->id);
DB::table('users_details')->insert(
array('firstname' => $firstName, 'lastname' => $lastName, 'user_id'=>$newUser->id)
);
$input['username'] = $email;
$input['password'] = $password;
Input::replace($input);
$request = Request::create('oauth/login', 'POST'); //laravel way for passing parms in doLogin function or you can call here your Login function
$response = Route::dispatch($request);
return $response;
}
Frontend:
Accordingly using this i have created my Frontend.
Just follow its steps and get all the details of any user like email, fb_id , name and send it to your backend.
After that make a build and check it out if its working
Thanks
Ashish Ginotra
No comments:
Post a Comment