Laravel Socialite facebook get user phone number - facebook

I use laravel socialite for authentication users on my website. I get basic user data, but I need user_phone, because some users signet up for Facebook with a phone number.
Unfortunately in the documentation I have not found a field phone_number
This is screenschot based FB doc, ebout email field:
The question: if the field email is empty, where to look for a number???

Phone number is not available using Facebook API check available fields here https://developers.facebook.com/docs/graph-api/reference/v2.2/user

You can try using this. Credits goes to #amirhazz, the full thread can be found here : https://laracasts.com/discuss/channels/laravel/socialite-facebook-extra-fields?page=1
class FacebookLogin {
/**
* Since facebook will give you name, email, gender by default,
* You'll only need to initialize Facebook scopes after getting permission
*/
const facebookScope = [
'user_birthday',
'user_location',
];
/**
* Initialize Facebook fields to override
*/
const facebookFields = [
'name', // Default
'email', // Default
'gender', // Default
'birthday', // I've given permission
'location', // I've given permission
];
public function facebookRedirect()
{
return Socialite::driver('facebook')->fields(self::facebookFields)->scopes(self::facebookScope)->redirect();
}
public function facebookCallback()
{
$facebook = Socialite::driver('facebook')->fields(self::facebookFields)->user();
}
}

Related

How to validate use entered phone number using slots in botpress?

I am experimenting on botpress slots . There is the new option called as slot which will validate the user input . However I am not able to find resources which will validate the user input.
The bot must validate the use input as a phone number using slot feature or any other without use of external api ?
Is this possible ?
for example:
If the user inputs a valide phone number the flow will proceed.
else if use enters invalid phone number the flow will ask to re enter a valid phone number.
I have tried multiple things but had no luck finding the proper documentation/ tutorial regarding it.
I think you can use a custom action to achieve this. A custom action for validating mobile number may look like:
const baseMessage = {
type: 'text',
markdown: false
}
/**
* check if phone number is valid
* #title validate phone number
* #category Validation
* #author Your name
* #param {string} phone - phone number
*/
const validateNumber = async phone => {
var phoneRegex = /^\d{10}$/
if (phone.match(phoneRegex)) {
temp.phone_validation = 'success'
} else {
temp.phone_validation = 'error'
}
}
return validateNumber(args.phone)
You can call the custom action in your validation flow and redirect user accordingly

laravel 5.2 redirect after registration by id

I've found here some examples, but they are not answering how to redirect registered user to his own profile by id.
protected $redirectPath = '/profile/view/'.$user->id; Did not help.
I have a project where users are being authorized without email confirmation and they are sent to /home after registration.
Route to user profile is: /profile/view/id (id is unique of course).
I managed to send them there after login (in AuthController :
public function authenticated($request,$user)
{
return redirect('/profile/view/'.$user->id);
}
but redirect to profile after registration I can't handle.
Approach 1.
Let users view their own profile without ID.
route (make ID optional):
Route::get('profile/view/{id?}', ...);
controller:
public function view($id = null) {
if (is_null($id) { //set id of currently signed in user if id == null
$id = Auth::user()->id;
}
//continue as before
}
Approach 2.
Modify routes so redirect happens to correct url.
Note: order of routes is important
Route::get('profile/view', function() {
return redirect()->route('profile.view', ['id' => Auth::user()->id]);
});
Route::get('profile/view/{id}', ...)->name('profile.view');
Note: in both approaches auth middleware is a must, else you going to get error if user is not logged in (PHP error: Trying to get property of non-object on line X)
With both approaches you just redirect user to profile/view:
is shown profile (without ID in URL)
is redirected to proper url profile/view/ID.

Laravel Socialite with Facebook not logging in

I'm following the documentation exactly.
https://github.com/laravel/socialite and https://laravel.com/docs/5.1/authentication#social-authentication
I've created my app on Facebook and got everything working. When I click my log in with Facebook button, it authorizes the app and takes me back to my site.
However, it doesn't show me as logged in. If I dd() instead of the redirect below, I get all of the data from my Facebook account. But the pages that are only visible to logged in users, aren't visible.
Here is my controller:
public function redirectToProvider()
{
return Socialite::driver('facebook')->redirect();
}
public function handleProviderCallback()
{
$user = Socialite::driver('facebook')->user();
return redirect('my-profile')
->with('message', 'You have signed in with Facebook.');
}
Here are my routes:
Route::get('login/facebook', 'Auth\AuthController#redirectToProvider');
Route::get('login/facebook/callback', 'Auth\AuthController#handleProviderCallback');
Socialite is installed properly in composer.json. The classes are in config/app.php and the IDs for my FB app are in config/services.php.
Any ideas as to why it's not working?
In the handleProviderCallback method you need to create and authenticate the user returned by the driver.
Create the user if doesn't exist:
$userModel = User::firstOrNew(['email' => $user->getEmail()]);
if (!$userModel->id) {
$userModel->fill([.....]);
$userModel->save();
}
Then authenticate the user:
Auth::login($userModel);
Your method will look like this:
public function handleProviderCallback() {
$user = Socialite::driver('facebook')->user();
$userModel = User::firstOrNew(['email' => $user->getEmail()]);
if (!$userModel->id) {
$userModel->fill([.....]);//Fill the user model with your data
$userModel->save();
}
Auth::login($userModel);
return redirect('my-profile')
->with('message', 'You have signed in with Facebook.');
}

How to get logged off users email address in meteor?

In my routing file I have the following down.
Router.route('/user/:createdBy', {
name: 'user',
/*onBeforeAction: function () {
AccountsEntry.signInRequired(this);
},*/
fastRender: true,
data: function () {
paramId = this.params.createdBy;
// Still have to find a way how to get data
// Function below is only for signed in users
return Meteor.users.findOne(paramId);
}
});
In my user template I want to display the email. I have it like this {{emails.[0].address}} and as {{users.emails.[0].address}} but the email doesn't show up. It only shows up if the user is logged in. I however have the users Id as my param. (This is for testing purposes guys!).
If you want to use the logged off user information, you could try this:
// in your router.js
// callback would be called when login is successful
Meteor.onLogin(function(){
Session.set('login user', Meteor.user());
})
// in your template, or other functions
// the Session would be update only if a user login successfully
var userId = Session.get('login user')._id;
Click here for more details.
I wish it could help :-)

Using Zend Auth with external authentication mechanism

I have a Drupal site and a Zend application. The main thing is the Drupal site, where the users are stored & everything.
I want my users to be automatically logged in to the Zend app when they log in on Drupal. The problem is that Drupal changes the session cookie to SESS* where * is some random (EDIT: not random, but based on protocol and domain) string.
Is there any way I can tell Zend to use this cookie as a session identifier and to log the user automatically?
You have to write your own authentication adapter:
class YourApp_Auth_Adapter_DrupalBridge implements Zend_Auth_Adapter_Interface
{
/**
* #return Zend_Auth_Result
*/
public function authenticate()
{
// Check if the Drupal session is set by reading the cookie.
// ...
// Read the current user's login into $username.
// ...
// Create the authentication result object.
// Failure
if (null === $username) {
return new Zend_Auth_Result(Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND, null);
}
// Success
return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $username);
}
}
Then process your authentication:
$adapter = new YourApp_Auth_Adapter_DrupalBridge();
$result = Zend_Auth::getInstance()->authenticate($adapter);
if ($result->isValid()) {
// User is logged in
}