happyonlinegr / socialite-provider-vipps
There is no license information available for the latest version (dev-main) of this package.
Vipps Provider for Laravel Socialite
dev-main
2022-07-25 15:20 UTC
Requires
This package is not auto-updated.
Last update: 2024-04-19 09:09:11 UTC
README
Installation
composer require happyonlinegr/socialite-provider-vipps
Service Provider
Add to app.php
:
'providers' => [ Laravel\Socialite\SocialiteServiceProvider::class, \SocialiteProviders\Manager\ServiceProvider::class, ];
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', ], ];
Configuration setup
You will need to add an entry 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.
5. Usage
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 { public function index(Request $request) { return Socialite::driver('vipps')->redirect(); } public function handleCallback() { $user = Socialite::driver('vipps')->stateless()->user(); if (!$user) { //Return error message } //Do stuff } }