Jyrone Parker

Creating A Todo App With SMS Alerts In Laravel 5 – Part 2

[callaction url=”https://www.youtube.com/user/JPlaya01″ background_color=”#333333″ text_color=”#ffffff” button_text=”Go Now” button_background_color=”#e64429″]Subscribe To My Youtube Page[/callaction]

To expand on the todo sms app, I have decided to add social authentication (Facebook) to make things a little more interesting.

Laravel Dependencies

Laravel has a package to deal with social authentication it’s called Socialite. To add to our project, cd to the project root and run this command

composer require laravel/socialite

This will install the dependencies, next we need to register them open config/app.php and add this:

'providers' => [ // Other service providers... LaravelSocialiteSocialiteServiceProvider::class, ],

You also need to register the facade

'Socialite' => LaravelSocialiteFacadesSocialite::class,

Now we need to link the app to Facebook, read here to create your Facebook app go to config/services.php and add this to the array:
'facebook' => [
'client_id' => env('FACEBOOK_ID'),
'client_secret' => env('FACEBOOK_SECRET'),
'redirect' => env('FACEBOOK_CALLBACK_URL'),
]

Don’t forget to add these variables to your .env file. Now we have to set up the routes and logic to handle the social auth. Head over to your routes file and add the following:

Route::get('auth/facebook', 'AuthAuthController@redirectToProvider');


Route::get('auth/facebook/callback', 'AuthAuthController@handleProviderCallback');

Now head over to app/Http/Controllers/Auth/AuthController.php and add the following functions

/**
* Redirect the user to the Facebook authentication page.
*
* @return Response
*/
public function redirectToProvider()
{
return Socialite::driver('facebook')->redirect();
}


/**
* Obtain the user information from Facebook.
*
* @return Response
*/
public function handleProviderCallback(IlluminateHttpRequest $request)
{
$user = Socialite::driver('facebook')->user();
$userSession = [
"name" =>$user->name,
"email"=>$user->email,
"avatar"=>$user->avatar,
"id" => $user->id,
];
$request->session()->push('user', $userSession);
return redirect()->route('home');
}
Notice how we are using $request->session()? This is so users won’t have to keep signing in everytime the come to the website. Let’s set up the logic!

Laravel Sessions

Laravel provides an elegant Session API. You can use a wide array of options such as Memcached, Redis, and Databases. Head over to .env and change SESSION_DRIVER to database. Run the following artisan command


php artisan session:table && php artisan migrate

Now we need to change our ‘/’ route to this:


Route::get('/',['as' => 'home', function (IlluminateHttpRequest $request) {

if ($request->session()->has('user')) {
//

$user = $request->session()->get('user')[0];
//var_dump($user); //exit();
$todos = AppTodo::where('facebook_id','=', $user['id'])->get();
//var_dump($todos); exit();
return view('welcome')->with(['user'=> $user, 'todos' => $todos]);
}
else{
return view('welcome');
}

}]);

Lastly we add a button on the home page to link to Facebook. That’s it! Don’t forget to check out the code here on Github and check out the live app HERE!

Leave a Reply

Instagram

Follow Me!

%d bloggers like this: