PHP code example of advanced-solutions / iceland-electronic-id
1. Go to this page and download the library: Download advanced-solutions/iceland-electronic-id library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
advanced-solutions / iceland-electronic-id example snippets
'islands' => [
'client_id' => env('ISLANDS_CLIENT_ID'),
'client_secret' => env('ISLANDS_CLIENT_SECRET'),
'redirect' => env('ISLANDS_REDIRECT_URI'),
],
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']);
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()]);
}
}
}
...
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',
];
}
bash
php artisan vendor:publish --provider="AdvancedSolutions\IcelandElectronicId\IslandsServiceProvider"