PHP

Latest library version: 3.1.8

Even though in this tutorial we use Laravel, the library could be used with any other framework or without any framework

Front end:

Include the library SDK:

var el = document.createElement("script");
el.async = true;
el.defer = true;
el.src = "https://cdn.jsdelivr.net/npm/[email protected]${version}/dist/oneaccount.min.js";
document.body.appendChild(el);

Or just include the script in your HTML (at the end of your body tag):

<script defer async src="https://cdn.jsdelivr.net/npm/[email protected]<version>/dist/oneaccount.min.js"></script>

Initialize the library:

if (window.oneaccount) {
initOneaccount();
} else {
document.addEventListener("oneaccount-loaded", initOneaccount);
}
// init the library
function initOneaccount() {
window.oneaccount.init("your external id", {
// NOTE: Please check the Library options page for more customisations
iOSRedirectURL: "/user", // required
callbackURL: "/oneaccountauth", // required
});
}

Add the button:

<!-- NOTE: the classes are important for the library to attach the click bindings -->
<button class="oneaccount-button oneaccount-show">Sign in/up using One account</button>
document.addEventListener("oneaccount-authenticated", function(event) {
// data contains any data you have returned from the backend
var data = event.detail;
// your business logic here
});

Backend:

Setup the library:

<?php declare(strict_types=1);
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Oilastudio\Oneaccount\FileEngine;
use Oilastudio\Oneaccount\Oneaccount;
use Illuminate\Support\Facades\Redis;
final class RedisEngine implements EngineInterface
{
public function set(string $key, array $value): bool
{
Redis::set($key, json_encode($value), 'EX', 3 * 60);
}
public function get(string $key): array
{
$data = json_decode(Redis::get($key));
Redis::del($key);
return $data;
}
}
class OneaccountController extends Controller
{
// The route URL is the callback URL you have set when you created One account app.
public function oneaccountauth(Request $request)
{
// provide an implementation of EngineInterface for example redis:
$oneaccount = new Oneaccount(new RedisEngine());
// you can implement the interface using any DB or cache.
// for development purposes you can use file engine implementation
// (creates files in your file system, make sure to delete them):
// $oneaccount = new Oneaccount(new FileEngine());
$user = $oneaccount->auth();
// NOTE: never return code 200 if a user is not authenticated
if (false !== $user) {
return new JsonResponse(['success' => false], 401);
}
// since One account doesn't differentiate between sign up and sign in,
// you can use userId to check if the user signed up c on your website or not.
// the same way you can access any other data you requested from the user:
// $userId = $user['userId'];
// $email = $user['email'];
$firstName = $user['firstName'];
return new JsonResponse(['firstName' => $firstName]);
}
}