OAuth Goes Mainstream

on August 9th, 2019

Setting up OAuth v2 was always a little funky and hard to remember. You had to install a skeleton bridge addon just to turn the feature on, and then customizing user data was a bit unintuitive.

In v3 it's much more in tune with the fundamental Laravel approach.

Getting out and socializing

Install Socialite and drop in your provider's credentials, the same way you would if you were building your own Laravel app:

composer require laravel/socialite
// config/services.php
'github' => [
    'client_id' => env('GITHUB_CLIENT_ID'),
    'client_secret' => env('GITHUB_CLIENT_SECRET'),
    'redirect' => 'http://your-site.com/oauth/github/callback',

Tell Statamic which providers you want to support. For each service, corresponding buttons will appear on your login page.

// config/statamic/oauth.php
'providers' => ['github', 'twitter'],

Then all you need to do is flick the switch with an .env variable.

Login screen with OAuth buttons

More providers

Socialite comes with some common providers like Facebook and Twitter, but if you want something extra, I'll bet it already exists in the third party SocialiteProviders library. There's a giant pile of them in there.

Follow along with the instructions to add one of their providers.

composer require socialiteproviders/etsy
// EventServiceProvider.php
protected $listen = [
    \SocialiteProviders\Manager\SocialiteWasCalled::class => [
        'SocialiteProviders\\Etsy\\[email protected]',

You're ready to log into your blog with the same account you shop for beaded necklaces and homemade wooden coasters. Welcome to the the future!

Customizing users

You can more easily control what gets stored with the user with a closure inside your AppServiceProvider.

OAuth::provider('github')->withUserData(function ($user) {
    return [
        'name' => $user->getName(),
        'created_at' => now()->format('Y-m-d'),
Jason Varga
Jason Varga, Lead Deveoper @ Statamic
Lead Developer

Copyright 2020 Statamic