digitive / vipps-socialite-provider
Vipps OAuth2 Provider for Laravel Socialite
Requires
- php: ^8.1
- ext-json: *
- socialiteproviders/manager: ^4.0
This package is auto-updated.
Last update: 2024-04-04 14:17:48 UTC
README
1. Installation
// This assumes that you have composer installed globally composer require digitive/vipps-socialite-provider
2. Service Provider
Add to app.php:
'providers' => [ Laravel\Socialite\SocialiteServiceProvider::class, \SocialiteProviders\Manager\ServiceProvider::class, ];
3. Event Listener
- Add
SocialiteProviders\Manager\SocialiteWasCalled
event to yourlisten[]
array inapp/Providers/EventServiceProvider
.
Example:
/** * The event handler mappings for the application. * * @var array */ protected $listen = [ \SocialiteProviders\Manager\SocialiteWasCalled::class => [ '\SocialiteProviders\Vipps\VippsExtendSocialite@handle', ], ];
4. Configuration setup
You will need to add vipps to the services configuration file so that after config files are cached for usage in production environment (Laravel command artisan config:cache
) all config is still available.
Add to config/services.php
.
'vipps' => [ 'client_id' => env('VIPPS_CLIENT_ID'), 'client_secret' => env('VIPPS_CLIENT_SECRET'), 'redirect' => env('VIPPS_REDIRECT_URI'), ],
Remember to whitelist the redirect_uri in the Vipps portal. Client ID and secret is also available in the Vipps portal.
5. Usage
- Laravel docs on configuration
- Guzzle version 7 or later is required
- You should now be able to use it like you would regularly use Socialite (assuming you have the facade installed):
To initiate the Vipps login, add this to your controller
return Socialite::driver('vipps')->redirect();
You've now gotten a user token from Vipps in your callback function. Now we need to use the user token to get the phone number of the authenticated user.
$user = Socialite::driver('vipps')->stateless()->user();
Example for a VippsAuthController:
<?php namespace App\Http\Controllers\Api; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use Laravel\Socialite\Facades\Socialite; class VippsAuthController extends Controller { // User clicked Login in with Vipps button public function index(Request $request) { return Socialite::driver('vipps')->redirect(); } // Vipps callback function (VIPPS_REDIRECT_URL in .env) public function handleCallback() { $user = Socialite::driver('vipps')->stateless()->user(); if (!$user) { //Handle errors for missing user } //Authenticate user } }
If you need multiple redirect URLs you can define a redirect url in the controller.
Example for specified redirect URL:
Socialite::driver('vipps')->redirectUrl($redirectUrl)->redirect();
The same goes for scopes
Example for specified scopes:
Socialite::diver('vipps')->scopes(['openid', 'api_version_2', 'phoneNumber', 'name'])->redirect();
Vipps guidelines
- When using Vipps login you need to use the login button svgs provided by Vipps. Go to Vipps design guidelines for more info.
License
MIT © Digitive AS