Skip to navigation Skip to content
Jyrone Parker
  • Home
  • About Jyrone Parker
  • Blog
  • Code Life Podcast
  • Shop
    • Accessories
    • Clothing
    • Coaching
    • Hardware
    • Study Guides
    • Video Courses
  • Conferences
  • Sponsors
  • Home
  • About Jyrone Parker
  • Blog
  • Code Life Podcast
  • Shop
    • Accessories
    • Clothing
    • Coaching
    • Hardware
    • Study Guides
    • Video Courses
  • Conferences
  • Sponsors
  • $0.00 0 items
Home / Blog / Creating A Todo App With SMS Alerts In Laravel 5 – Part 2
Posted on October 21, 2015 by mastashake08 — Leave a comment

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!

Share this:

  • Twitter
  • Facebook

Like this:

Like Loading...
Categories: Blog, Uncategorized
Tags: Laravel and Facebook, Laravel social authentication

Post navigation

Previous post: Create A Real-Time Chat App With Ionic
Next post: Creating A Todo App With SMS Alerts In Laravel 5 – Part 3
Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

© Jyrone Parker 2021
Privacy PolicyBuilt with Storefront & WooCommerce.
  • My Account
  • Search
  • Cart 0
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Cookie settingsACCEPT
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled

Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.

Non-necessary

Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.

%d bloggers like this: