One account
Search…
⌃K
Links

PHP

Latest library version: 3.2.2
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 Illuminate\Support\Facades\Redis;
use Oilastudio\Oneaccount\EngineInterface;
use Oilastudio\Oneaccount\Oneaccount;
class OneaccountController extends Controller
{
public function oneaccountauth(Request $request)
{
// provide an implementation of EngineInterface for example redis:
$oneaccount = new Oneaccount(new RedisEngine());
// 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 (!$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'];
$firstName = $user['firstName'];
// the object contains all fields (camelCased) that are defined in the app (Requested data)
// any data returned here would be sent to oneaccount-authenticated event on front-end e.g.:
return new JsonResponse(['firstName' => $firstName]);
}
}
// For production deployment it is recommended to implement the EngineInterface
// Any database or caching system can be used, in this tutorial we will
// demonstrate implementatioon of EngineInterface using 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), true);
Redis::del($key);
return $data;
}
}