advanced-solutions / iceland-electronic-id
A Laravel package for islands.is socialite integration
1.0.8
2024-07-31 15:54 UTC
Requires
- php: ^7.4|^8.0
- ext-json: *
- guzzlehttp/guzzle: ^7.0
- illuminate/support: ^8.0|^9.0|^10.0
README
This package provides integration for Iceland's electronic ID system with Laravel Socialite.
Installation
-
Require the package via Composer:
composer require advanced-solutions/iceland-electronic-id
-
Publish the configuration file:
php artisan vendor:publish --provider="AdvancedSolutions\IcelandElectronicId\IslandsServiceProvider"
-
Add the Iceland Electronic ID configuration to your:
ISLANDS_CLIENT_ID=your-client-id ISLANDS_CLIENT_SECRET=your-client-secret ISLANDS_REDIRECT_URI=your-redirect-uri
-
Add the configuration to
config/services.php:
'islands' => [ 'client_id' => env('ISLANDS_CLIENT_ID'), 'client_secret' => env('ISLANDS_CLIENT_SECRET'), 'redirect' => env('ISLANDS_REDIRECT_URI'), ],
Usage
- Add routes to your api.php (for example):
use Illuminate\Support\Facades\Route; use App\Http\Controllers\Auth\LoginController; Route::get('login/islands', [LoginController::class, 'redirectToIslands']); Route::get('login/islands/callback', [LoginController::class, 'handleIslandsCallback']);
- Update your LoginController:
class AuthController extends Controller { protected $islandsService; public function __construct(IslandsService $islandsService) { $this->islandsService = $islandsService; } public function redirectToIslands() { $query = http_build_query([ 'client_id' => config('islands.client_id'), 'redirect_uri' => config('islands.redirect_uri'), 'response_type' => 'code', 'scope' => 'openid profile', 'state' => csrf_token(), ]); return redirect('https://identity-server.staging01.devland.is/connect/authorize?' . $query); } public function handleIslandsCallback(Request $request) { $code = $request->get('code'); try { $tokenData = $this->islandsService->authenticate($code); $userInfo = $this->islandsService->getUserInfo($tokenData['access_token']); // Find or create user logic $user = User::firstOrCreate( ['email' => $userInfo['email']], ['name' => $userInfo['name']] ); Auth::login($user, true); return redirect()->intended('dashboard'); } catch (\Exception $e) { return redirect('/login')->withErrors(['error' => $e->getMessage()]); } } }
- Update the User model if necessary:
... class User extends Authenticatable { use Notifiable; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'email', 'password', ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; }
Testing
To test the Iceland Electronic ID integration, simply navigate to the login route:
http://your-app-url/login/islands