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:
1
var el = document.createElement("script");
2
el.async = true;
3
el.defer = true;
4
el.src = "https://cdn.jsdelivr.net/npm/[email protected]${version}/dist/oneaccount.min.js";
5
document.body.appendChild(el);
Copied!
Or just include the script in your HTML (at the end of your body tag):
1
<script defer async src="https://cdn.jsdelivr.net/npm/[email protected]<version>/dist/oneaccount.min.js"></script>
Copied!
Initialize the library:
1
if (window.oneaccount) {
2
initOneaccount();
3
} else {
4
document.addEventListener("oneaccount-loaded", initOneaccount);
5
}
6
7
// init the library
8
function initOneaccount() {
9
window.oneaccount.init("your external id", {
10
// NOTE: Please check the Library options page for more customisations
11
iOSRedirectURL: "/user", // required
12
callbackURL: "/oneaccountauth", // required
13
});
14
}
Copied!
Add the button:
1
<!-- NOTE: the classes are important for the library to attach the click bindings -->
2
<button class="oneaccount-button oneaccount-show">Sign in/up using One account</button>
Copied!
1
document.addEventListener("oneaccount-authenticated", function(event) {
2
// data contains any data you have returned from the backend
3
var data = event.detail;
4
// your business logic here
5
});
Copied!

Backend:

Setup the library:
1
<?php declare(strict_types=1);
2
3
namespace App\Http\Controllers;
4
5
use App\Http\Controllers\Controller;
6
use Illuminate\Http\JsonResponse;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Facades\Redis;
9
10
use Oilastudio\Oneaccount\EngineInterface;
11
use Oilastudio\Oneaccount\Oneaccount;
12
13
class OneaccountController extends Controller
14
{
15
public function oneaccountauth(Request $request)
16
{
17
// provide an implementation of EngineInterface for example redis:
18
$oneaccount = new Oneaccount(new RedisEngine());
19
// for development purposes you can use file engine implementation
20
// (creates files in your file system, make sure to delete them):
21
// $oneaccount = new Oneaccount(new FileEngine());
22
$user = $oneaccount->auth();
23
24
// NOTE: never return code 200 if a user is not authenticated
25
if (!$user) {
26
return new JsonResponse(['success' => false], 401);
27
}
28
29
// since One account doesn't differentiate between sign up and sign in,
30
// you can use userId to check if the user signed up c on your website or not.
31
// the same way you can access any other data you requested from the user:
32
33
// $userId = $user['userId'];
34
$firstName = $user['firstName'];
35
// the object contains all fields (camelCased) that are defined in the app (Requested data)
36
// any data returned here would be sent to oneaccount-authenticated event on front-end e.g.:
37
return new JsonResponse(['firstName' => $firstName]);
38
}
39
}
40
41
// For production deployment it is recommended to implement the EngineInterface
42
// Any database or caching system can be used, in this tutorial we will
43
// demonstrate implementatioon of EngineInterface using redis.
44
final class RedisEngine implements EngineInterface
45
{
46
public function set(string $key, array $value): bool
47
{
48
Redis::set($key, json_encode($value), 'EX', 3 * 60);
49
}
50
51
public function get(string $key): array
52
{
53
$data = json_decode(Redis::get($key), true);
54
Redis::del($key);
55
return $data;
56
}
57
}
Copied!
Last modified 1mo ago
Copy link