PHP code example of ziming / laravel-myinfo-sg

1. Go to this page and download the library: Download ziming/laravel-myinfo-sg 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/ */

    

ziming / laravel-myinfo-sg example snippets




use Ziming\LaravelMyinfoSg\Http\Integrations\MyinfoV5\MyinfoConnector;

$myinfoConnector = new MyinfoConnector;

return redirect()->to(
    $myinfoConnector->generateAuthorizationUrl()
);



use Ziming\LaravelMyinfoSg\Http\Integrations\MyinfoV5\MyinfoConnector;

$myinfoConnector = new MyinfoConnector;

return redirect()->to(
    $myinfoConnector->generateAuthorizationUrl(
        'https://your-app.test/callback/myinfo-v5'
    )
);



use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Ziming\LaravelMyinfoSg\Http\Integrations\MyinfoV5\MyinfoConnector;

Route::get('/callback/myinfo-v5', function (Request $request) {
    $myinfoConnector = new MyinfoConnector;
    $tokenSet = $myinfoConnector->completeAuthorization($request);
    $userInfo = $myinfoConnector->getVerifiedUserInfo($tokenSet);

    return response()->json($userInfo->personInfo());
});



use Illuminate\Support\Facades\Route;
use Ziming\LaravelMyinfoSg\Http\Controllers\MyinfoV5\CallAuthorizationApiController;
use Ziming\LaravelMyinfoSg\Http\Controllers\MyinfoV5\PublicJwksController;

Route::post('/redirect-to-singpass-v5', CallAuthorizationApiController::class)
    ->name('myinfo-v5.singpass')
    ->middleware('web');

Route::get('/myinfo/v5/jwks', PublicJwksController::class)
    ->name('myinfo-v5.public-jwks');

use Ziming\LaravelMyinfoSg\Exceptions\MyinfoV5\MyinfoV5TransportException;
use Ziming\LaravelMyinfoSg\Http\Integrations\MyinfoV5\MyinfoConnector;

$myinfoConnector = new MyinfoConnector;

try {
    $tokenSet = $myinfoConnector->completeAuthorization($request);
    $userInfo = $myinfoConnector->getVerifiedUserInfo($tokenSet);
} catch (MyinfoV5TransportException $exception) {
    if ($exception->restartAuthorization()) {
        // Offer a new authorization attempt using the POST form in your UI.
        return response()->json(['message' => 'Please start Singpass authorization again.'], 503);
    }

    return response()->json(['message' => 'Singpass is temporarily unavailable.'], 503);
}
bash
php artisan vendor:publish --provider="Ziming\LaravelMyinfoSg\LaravelMyinfoSgServiceProvider" --tag="myinfo-sg-config"
blade
<form method="POST" action="{{ route('myinfo-v5.singpass') }}">
    @csrf
    <button type="submit">Continue with Singpass</button>
</form>
bash
php artisan myinfo:validate-jwks \
    --private=storage/app/private/myinfo/private.jwks.json \
    --public=storage/app/myinfo/public.jwks.json \
    --signing-kid="$MYINFO_V5_CHOSEN_JWKS_SIG_KID"