Laravel's Magical, Shiny Socialite! ๐ŸŒˆโœจ

Funny Laravel docs · Warning: May cause actual learning AND laughter!

Browse funny laravel docs
On this page

Welcome, Dear Developer! ๐Ÿ‘‹

Dive into the enchanting realm of Laravel Socialite, where unicorns dance and rainbows guide your way to effortless OAuth magic! ๐Ÿฆ„๐ŸŒ‰

Installation: A Snap, Like a Snicker Bar ๐Ÿซ

Add Socialite to your project with ease, like snapping your fingers. Just type composer require laravel/socialite in your terminal and watch the sparkles fly! โœจ๐Ÿงžโ€โ™‚๏ธ

Upgrading Socialite: A Breeze, Like a Soft Summer Zephyr โ˜€๏ธ

Updating Socialite to the latest version is as easy as opening a window. Run composer update laravel/socialite and breathe in that fresh, OAuth-scented air! ๐ŸŒฌ๐Ÿฅค

Configuration: A Piece of Cake, Like Your Grandmaโ€™s Recipe ๐Ÿฐ

Setting up Socialite is as simple as following Grandmaโ€™s famous chocolate cake recipe. Just adjust the config/services.php file to taste! ๐Ÿฅ„๐Ÿซ๐ŸŒถ๏ธ

Authentication: Lean Back, Itโ€™s Showtime! ๐Ÿš€

Routing: A Walk in the Park, Like an afternoon stroll with Garfield ๐Ÿพ๐Ÿšถโ€โ™‚๏ธ

Configure your application routes with a simple step-by-step process. In no time, youโ€™ll have Socialite guiding you through the enchanting forest of authentication! ๐ŸŒฒ๐Ÿงญ

Authentication and Storage: A Breeze, Like a well-oiled machine ๐Ÿ› ๏ธโ˜€๏ธ

Effortlessly handle user authentication and data storage with Socialiteโ€™s built-in functions. Itโ€™s like having your very own virtual butler, handling all the heavy lifting for you! ๐Ÿค–๐Ÿป

Access Scopes: A Snap, Like a well-timed Spider-Man meme ๐Ÿ•ท๏ธ๐ŸŽž๏ธ

Add extra layers of security to your application with access scopes. With Socialite, you can control who gets to swing through which parts of your web! ๐Ÿš€๐Ÿฆ‡

Slack Bot Scopes: A Laugh Riot, Like the funniest cat video on the internet ๐Ÿฑ๐Ÿ˜‚

Customize your Slack botโ€™s capabilities with slack bot scopes. Now you can teach your robot to dance, fetch a coffee, or even play ping-pong! ๐ŸŽพ๐Ÿฅค๐Ÿ’ƒ

Optional Parameters: A Piece of Cake, Like having extra sprinkles on your sundae ๐Ÿจ๐ŸŒˆ

Customize the user authentication process with optional parameters. Now you can make every login unique, just like your favorite ice cream flavor! ๐Ÿฆ๐ŸŒˆ

Retrieving User Details: A Snap, Like a lightning-fast Superman โšก๏ธ๐Ÿƒโ€โ™‚๏ธ

Access user details with ease using Socialiteโ€™s built-in functions. In no time, youโ€™ll have all the information you need to build a superhero database! ๐Ÿฆธโ€โ™‚๏ธ๐Ÿ“

Testing: A Breeze, Like testing whether Wonder Woman can deflect bullets ๐Ÿ’ช๐Ÿ’ฅ

Easily test your Socialite-powered application with the built-in testing functions. Now you can ensure your superhero app is bulletproof! ๐Ÿ›ก๏ธ๐ŸŽฏ

Welcome to the enchanting world of Laravel authentication, where weโ€™re not just logging in users, but pulling rabbits out of firewalls! Instead of the traditional password-protected login forms, letโ€™s dive into the delightful realm of OAuth providers with Laravel Socialite - your magical chameleon of authenticators!

Currently, Socialite supports a dazzling array of platforms, including Facebook (because who doesnโ€™t love poking around in someone elseโ€™s account?), X (a mysterious entity, shrouded in intrigue), LinkedIn (for when you need to link up with your digital doppelganger), Google (because everyone knows the key to the kingdom is their email password), GitHub (for all those coding cats out there), GitLab (for when you want to push commits before even logging in), Bitbucket (because who doesnโ€™t love a good repository scuffle?), and Slack (to avoid typing โ€œaslโ€ for the rest of your days)!

![NOTE] Hiccup Alert: Donโ€™t fret if Socialite doesnโ€™t support that one platform you love. Fear not, intrepid authenticator! The community-driven Socialite Providers website has got your back with adapters for platforms beyond the stars (or at least, beyond our list)!

Now, letโ€™s move on to the installation part:

The Installation Dance - Grab a pair of tap shoes and get ready for some twinkling toes! First, ensure youโ€™ve updated your composer.json file with the necessary Laravel Socialite package:

composer require laravel/socialite

And then, donโ€™t forget to boot the service provider in config/app.php:

'providers' => [
    // ...
    Laravel\Socialite\SocialiteServiceProvider::class,
],

With that, youโ€™re all set! So go ahead and start authentifying (yes, we just made up a word) with style!

Alrighty then! Letโ€™s get this party started, shall we? Youโ€™re about to embark on an enchanting journey with Laravelโ€™s very own Socialite โ€“ the life of every modern-day Cinderella! ๐Ÿ‘ธ๐Ÿ‘‹

First things first, you need to invite Socialite to your projectโ€™s soiree using the Composer package manager. Hereโ€™s how:

composer require laravel/socialite

Just think of it as sending out a fancy invitation: โ€œDear Socialite, weโ€™d be honored if youโ€™d join our Laravel ball!โ€ ๐Ÿ•บ๏ธโœจ

Now, if you ever find yourself in need of upgrading this fairy-tale friend, simply hop back here and take a peek at our Upgrading Socialite section. Because who wants an outdated pumpkin coach when they can have the latest sparkling carriage, right? ๐Ÿฎ

Remember, in the world of Laravel, itโ€™s always more magical with friends like Socialite! ๐Ÿ’–๐Ÿš€

Upgrading Socialite: The Sequel to the Hilarious Saga You Didnโ€™t Know You Needed! ๐ŸŽฌโœจ

Ahoy, code-warriors and butter-fingered web-wranglers! ๐Ÿš€๐Ÿš€ Ever found yourself in a pickle trying to upgrade your Socialite like a modern-day Icarus? Well, buckle up for this wild ride as we traverse the mystical world of Laravel upgrades together! ๐ŸŒ„

First off, letโ€™s set the mood with some lighthearted music: โ€œUpgrade me, Socialite, like youโ€™ve never upgraded before!โ€ ๐ŸŽถ

Now, before you dive headfirst into this brave new world of code, itโ€™s essential to take a gander at our trusty upgrade guide. Why, you ask? Because itโ€™s got all the secrets to turning an ordinary upgrade into a triumphant dance party! ๐Ÿ’ƒ๐Ÿ•บ

Just remember, dear friend, that this guide is not your run-of-the-mill self-help book promising six-pack abs in a week. Instead, it offers you wisdom on mastering Socialite upgrades with finesse and grace. And who knows? You might even learn a dance move or two! ๐Ÿฅณ

So, grab your favorite beverage (preferably served in a chalice to keep things medieval), put on your thinking cap, and letโ€™s embark on this enchanting journey together! ๐ŸŽ‰โœจ

Alrighty then! Before we dive into the Socialite shenanigans, letโ€™s get our ducks in a row (or should I say OAuth providers?). You see, these fine folks are the ones whoโ€™ll vouch for your application at the digital ball. To snag their digits, youโ€™ll need to create a โ€œdeveloper appโ€ within their respective dashboards, where the partyโ€™s at.

Once youโ€™ve wrangled those credentials, itโ€™s time to introduce them to your application by way of the config/services.php configuration fileโ€”think of it as a modern-day matchmaking service for apps and OAuth providers. But instead of love letters, we use keys like facebook, x, linkedin-openid, google, github, gitlab, bitbucket, slack, or slack-openid. Choose wisely, partner!

Hereโ€™s a little snippet to help you get started:

'github' => [
    'client_id' => env('GITHUB_CLIENT_ID'), // Whisper it into the ear of your app, shh!
    'client_secret' => env('GITHUB_CLIENT_SECRET'), // And now, the password...
    'redirect' => 'http://example.com/callback-url', // The secret rendezvous spot where they'll send your users after authentication
],

Attention

If your redirect option is a little shy and only offers a relative path, itโ€™ll automatically grow a pair and turn into a fully qualified URL. No more coyness, we like โ€˜em bold and confident!

Ahoy there, Captain! Buckle up for a whimsical journey through the realm of Laravel authentication - a veritable pirateโ€™s treasure trove of secure user management! ๐Ÿดโ€โ˜ ๏ธ๐Ÿ”’

Routing and Authentication

Setting sail on our first stop, we encounter the mighty routes. These are the navigational charts guiding your applicationโ€™s users through their swashbuckling adventures. With Laravel, you can define these routes in web.php or api.php, ensuring a well-charted course for all ye landlubbers and scallywags! ๐ŸŒ๐Ÿ“ˆ

But hereโ€™s the catch โ€“ we want our app to be as secure as Fort Knox, right? Well, weโ€™ve got a cunning solution: middleware. Just like the friendly neighborhood Spider-Man, it swoops in to save the day by inspecting incoming requests and enforcing rules. In the case of routing, middleware can ensure that only authorized users (i.e., those with proper pirate credentials) gain access to protected routes! ๐Ÿ•ธ๏ธ๐ŸŽ‰

Authenticating Users

Now itโ€™s time to discuss the lifeblood of every seafaring expedition: user authentication. This is where we ensure that every swashbuckler aboard our ship is, in fact, who they claim to be. Laravel takes care of the heavy lifting for us with its built-in authentication scaffolding. All you need to do is provide a database table containing user records, and Laravel will handle the rest! ๐Ÿ†๐Ÿ’ช

With the mighty Auth facade at your fingertips, you can easily authenticate users, manage their sessions, check whether theyโ€™re logged in or out, and even log them in and out. Itโ€™s like having a crew of loyal sailors ready to do your bidding! ๐Ÿคบ๐Ÿดโ€โ˜ ๏ธ

Authenticating Users via API

But what if you want to add more flexibility to your treasure trove? Fear not, matey! Laravel provides a handy API authentication feature that allows users to log in and out using API requests. Itโ€™s like creating an intergalactic portcullis around your app, ensuring that only authorized users can enter via their spaceships! ๐Ÿš€๐ŸŒŸ

The Guard System

As our pirate adventure continues, we stumble upon Laravelโ€™s Guard system. This powerful tool is like Captain Barbossaโ€™s cursed gold โ€“ valuable and dangerous if not handled with care. It allows you to manage multiple user providers (e.g., for website users, mobile app users, etc.) from a central location, giving you the flexibility to customize authentication as needed! ๐Ÿ†๐Ÿ”“

With the Guard system in your arsenal, you can rest assured that no scurvy dog will be able to infiltrate your Laravel treasure chest without proper credentials! ๐Ÿดโ€โ˜ ๏ธ๐Ÿ”’

Ahoy there, matey! Youโ€™ve now reached the end of this whimsical guide to Laravel authentication. With your newfound knowledge, you can set sail on your own swashbuckling adventure, creating secure and user-friendly applications that would make even the mightiest pirates envious! Arrrr ye ready for a sea of success? ๐Ÿดโ€โ˜ ๏ธ๐Ÿ’ช๐ŸŽ‰

Alrighty, grab your coding cowboy hats, because weโ€™re about to gallop through the wild west of Laravel routing! To wrangle those unruly users with an OAuth lasso, youโ€™ll need two routes โ€“ one for herding them towards the OAuth providerโ€™s corral, and another for rounding up the callback from the provider after theyโ€™ve been authenticated. Hereโ€™s a lariat-tight example:

Use Laravel's shiny Socialite Sheriff (Socialite), y'all!

Route::get('/auth/ride', function () {
    return Socialite::driver('github')->rideOff(); // Redirects the user to the OAuth provider
});

Route::get('/auth/rodeo', function () {
    $user = Socialite::driver('github')->corralUser(); // Retrieves the user's information after they've approved authentication

    // $user->token is their brandin' new cowpoke badge!
});

The rideOff method handed to you by the Socialite sheriff will kindly escort your users towards the OAuth provider, while the corralUser method will rope in the incoming request and haul their user info back from the provider after theyโ€™ve given the authentication a hearty howdy!

Now yโ€™all are on the right trail to taming those wild users with this here OAuth rodeo! Happy wrangling!

Alrighty then, letโ€™s dive into the thrilling world of Laravel Authentication and Storage! After youโ€™ve successfully charmed the OAuth provider into giving up your user data (props to you, smooth talker), itโ€™s time to see if this cat already has a profile on your app. If not, itโ€™s time for some digital paperwork - letโ€™s create their record!

use App\Models\User; // Import the User model, because who doesn't love users?
use Illuminate\Support\Facades\Auth; // Authentication, because passwords and secrets are our business now.
use Laravel\Socialite\Socialite; // Socialite, because we're about to make some new friends from Github.

Route::get('/auth/callback', function () {
    $githubCat = Socialite::driver('github')->user(); // Fetch the user from Github, purr-fectly named here for added feline flair.

    $newFeline = User::updateOrCreate([
        'github_id' => $githubCat->id, // Use the id to check if the user already exists in our database, if not, create a new record.
    ], [
        'name' => $githubCat->name, // Fill in the necessary details like name and email.
        'email' => $githubCat->email,
        'github_token' => $githubCat->token, // Don't forget to store the token for later use!
        'github_refresh_token' => $githubCat->refreshToken, // And a refresh token, just in case they need another life.
    ]);

    Auth::login($newFeline); // Now that we have their details, let's log them in and give them access to the dashboard.

    return redirect('/dashboard'); // Redirect them to their new digital home.
});

Ahem, for additional details on what tantalizing tidbits of user data you can get from different OAuth providers, kindly refer to our enlightening guide on Retrieving User Details.

Alrighty then! Letโ€™s delve into the enchanting world of Laravel Access Scopes, shall we? Itโ€™s like giving your user a magical keychain with all the right charms for the socialite dance. ๐Ÿ˜‰

First off, letโ€™s talk about the scopes method. Imagine youโ€™re at a costume party and everyoneโ€™s wearing masks; these scopes are your costume tags! You can deck your user out in multiple masks (er, scopes) by simply chaining them together:

use Laravel\Socialite\Socialite;

return Socialite::driver('github')
    ->scopes(['mask1', 'mask2']) // Don't forget to coordinate!
    ->redirect();

Now, if you want to switch your entire costume collection (or overwrite all the existing scopes), setScopes is the method for you:

return Socialite::driver('github')
    ->setScopes(['mask1', 'mask2']) // Time for a fashion emergency!
    ->redirect();

Just remember, in the whimsical realm of GitHub parties, youโ€™ll find yourself at the entrance with these scopes. ๐ŸŽ‰ And thatโ€™s all folks, until next time when we dance together again in Laravel Land! ๐Ÿ’ƒ๐Ÿผ๐Ÿ•บ๐Ÿป

Ahoy there, Laravel adventurers! Letโ€™s talk โ€˜bots and tokens, shall we? In the vast digital sea of Slackโ€™s API, there are several flavors of access tokens, each with its own set of permission scopes - kinda like pick-n-mix candy at a carnival. Socialite, our friendly guide, is compatible with two of these token types:

  1. Bot (marked with xoxb-) - Great for applications that want to send notifications to Slack workspaces owned by your users. You can generate a bot token using the following pirate code:
return Socialite::drive('slack')
    ->setSailAsPirate() // Just kidding, it's `asBotUser()`
    ->grabScopes(['chat:write', 'chat:write.public', 'chat:write.customize'])
    ->shoveOff();

But wait, thereโ€™s more! Once you have that bot token, donโ€™t forget to setSailAsPirate() again before using the user() method after Slack has rowed the user back to your ship:

$user = Socialite::drive('slack')->setSailAsPirate()->user();

When you generate a bot token, the user() method will still yield a Laravel\Socialite\Two\User instance; however, only the token property will be filled to the brim. This token can be stored for future use to send notifications to the authenticated userโ€™s Slack workspaces. Yarrr! ๐Ÿค 

Now, go forth and conquer the API seas, matey! ๐ŸŒŠ๐Ÿš€

Extra Flavors for Your OAuth Dance

Just like your eccentric uncle at a family gathering, some OAuth providers canโ€™t help but show off with their optional parameters during the redirection shuffle. To include these party tricks in your dance, kindly ask them over with the with method, dressed in an associative array:

use Laravel\Socialite\Socialite;

return Socialite::driver('google')
    ->with(['party_trick' => 'example.com']) // Just remember to avoid reserved moves like "state" or "response_type".
    ->redirect();

โ˜๏ธ PSA: When using the with method, donโ€™t get caught stepping on anyoneโ€™s toes with reserved keywords. Save the โ€˜stateโ€™ for when you really mean to say โ€œIโ€™ve got a dateโ€ and keep โ€˜response_typeโ€™ out of your dance moves, lest you want to start a cha-cha-challenge no one wanted.

๐Ÿ•บ๏ธ Back to the dance floor!

Now, letโ€™s get those socialite parties started! ๐ŸŽ‰

Alright, grab your virtual popcorn and letโ€™s dive into the world of user details!

User Details on Demand! ๐Ÿฟ

After our intrepid user has been zapped back to your appโ€™s authentication callback route, itโ€™s time for a little red carpet treatment. Who doesnโ€™t love a bit of VIP access, right? With Socialiteโ€™s user method, you can whisk them into the limelight and retrieve their deets like a pro!

The dapper user method returns an object loaded with properties and methods that will help you store user information in your very own swanky database. Keep in mind, the menu of offerings might vary depending on whether the OAuth valet youโ€™re wooing supports either OAuth 1.0 or 2.0:

Use Laravel\Socialite\Socialite as Socialite;

Route::get('/auth/callback', function () {
    $user = Socialite::driver('github')->user();

    // For OAuth 2.0 providers, the red carpet rolled out...
    $token = $user->token;
    $refreshToken = $user->refreshToken;
    $expiresIn = $user->expiresIn;

    // OAuth 1.0 providers? No sweat, we've got 'em covered too!
    $token = $user->token;
    $tokenSecret = $user->tokenSecret;

    // All providers, no matter the era, get these sweet deals...
    $userId = $user->getId();
    $nickname = $user->getNickname();
    $realName = $user->getName();
    $emailAddress = $user->getEmail();
    $profilePictureUrl = $user->getAvatar();
});

Remember, sometimes you gotta mix business with a dash of humor to keep things entertaining! ๐Ÿš€

Alrighty, letโ€™s dive into the world of digital dance cards! If youโ€™ve already scored a date with someone (by obtaining a valid access token), you can learn more about them using Socialiteโ€™s charming charmer, userFromToken method. Itโ€™s like asking for their number, but in code:

use Laravel\Socialite\Socialite;

$date = Socialite::driver('github')->userFromToken($token);

But hold on to your party hats! If youโ€™re using Facebook Limited Login via an iOS app, things get a bit more complicated. See, Facebook isnโ€™t one for playing by the rules, so instead of sending a regular access token, theyโ€™ll send an OpenID Connect (OIDC) token. Think of it like receiving a secret handshake instead of a traditional greeting. Donโ€™t worry though, this OIDC token can still be provided to our dance partner, userFromToken, to get more details:

$secretHandshake = Socialite::driver('facebook')->userFromToken($oidc_token);

Now, arenโ€™t you just the catโ€™s pajamas for knowing how to charm your digital dates like a pro? ๐Ÿ•บ๏ธ๐ŸŽ‰๐Ÿ’ƒ๏ธ

Ah, the stateless method! Itโ€™s like the secret sauce for your API, making it as agile and carefree as a digital nomad at a co-working cafรฉ.

Imagine youโ€™re running an API thatโ€™s as popular as a cat video on a Monday morning. Now, you want to add social authentication - think logins via Google, Facebook, or Twitter. But thereโ€™s a twist: your API is stateless and doesnโ€™t use cookie-based sessions.

Cue the stateless method! Itโ€™s like the bartender who serves the drinks without asking for ID because they know their regulars. Hereโ€™s how you can use it:

use Laravel\Socialite\Socialite;

// It's party time! Time to dance with Google, Facebook, or Twitter.
return Socialite::driver('google')->stateless()->user();

Just remember, this method disables session state verification. So itโ€™s like saying โ€œHey, I donโ€™t need no stinkinโ€™ tickets, let me in!โ€ to the bouncer at a VIP club. Use it wisely, my friend!

Now, if youโ€™re feeling adventurous and want to test this out, you canโ€ฆ but remember: like playing pinball, practice makes perfect!

Testing, testing! One, twoโ€ฆ Laravel Socialite here, saving your sanity from the OAuth dance!

Ever found yourself in a tangled web of authentication flows, making requests to OAuth providers that leave you feeling like a peon at a royal ball? Well fear no more, because weโ€™ve got just the cure for those awkward encounters - introducing our swanky fake method!

Now, you can pretend to be the OAuth provider and control the conversation by defining how they should behave during the dance. The real question is: will it be a romantic encounter or a clumsy tango? Itโ€™s up to you, dear developer!

But wait, thereโ€™s more! Whatโ€™s an OAuth dance without a dashing partner? Meet your new suitor: the user data! With our fake method, you can choose the user data that should be returned in response to the OAuth provider, so no need to worry about the awkward silence after asking for their number (erโ€ฆuser details).

So grab your dancing shoes and take a twirl with Laravel Socialite. Who knows? You might even catch the eye of that special OAuth provider youโ€™ve had your eye on!

Alright, letโ€™s dive into the hilarious world of Laravel and social media! Hereโ€™s a fun take on how to trick your application into thinking itโ€™s redirecting users to an OAuth provider. ๐Ÿคช

Pulling off the Great Redirect Hoax

Want to catch your app in a lie about redirecting its users to an OAuth provider? No problem! You can set up a phony sting operation with ease. Simply unleash the fake method before firing a request at your redirect route. This will make Socialite believe itโ€™s sending the user to a fake authorization URL, instead of the real OAuth provider:

use Laravel\Socialite\Socialite as FakeSoc; // Give it a catchy alias, because why not?

test('user gets tricked by github', function () {
    FakeSoc::fake('github'); // It's time for some mischief!

    $response = $this->get('/auth/github/redirect'); // Grab a response from the unsuspecting user.

    $response->assertRedirect(); // Confirm the redirect... or was it just another prank?
});

Now, arenโ€™t we having a whale of a time while still learning something new? Happy coding! ๐Ÿณ๐ŸŽ‰

Alright, buckle up, coding cowboy! Time to test your appโ€™s callback route like a true gunslinger. Hereโ€™s how you can make it believe that Callback Kid has ridden in with the goods without even leaving the corral.

use Laravel\Socialite\Socialite as Sheriff;
use Laravel\Socialite\Two\User as Cowboy;

test('user can log in with GitHub', function () {
    Sheriff::fake('github', (new Cowboy)->map([
        'id' => 'github-123',
        'name' => 'Jason Beggs',
        'email' => 'jason@example.com',
    ]));

    $posse = $this->get('/auth/github/callback');

    $posse->should_ensure_safe_passage()->assertRedirect('/dashboard');

    $this->database_outlaw()->should_have_stolen([
        'name' => 'Jason Beggs',
        'email' => 'jason@example.com',
        'github_id' => 'github-123',
    ]);
});

By golly, by default this Cowboy instance comes loaded with a token. But if you find yourself in need of some extra ammo, you can customize the Cowboy instance to your heartโ€™s content:

$fakeCowboy = (new Cowboy)->map([
    'id' => 'github-123',
    'name' => 'Jason Beggs',
    'email' => 'jason@example.com',
])->set_badge('fake-token')
  ->set_wanted_poster('fake-refresh-token')
  ->set_warrant_of_arrest(3600)
  ->add_outlawed_territories(['read', 'write']);

Now, partner, go forth and test with a grin! The west is waiting.

Our network