Node.js
Latest library version: 3.2.2

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!
Listen to an authentication event:
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:

Install the library:
1
npm install -S oneaccount-express
Copied!
Setup the library:
1
const express = require('express');
2
const { OneAccount } = require('oneaccount-express');
3
const Redis = require('ioredis');
4
const app = express();
5
const redis = new Redis();
6
7
app.use(express.json());
8
9
let oneaccount = new OneAccount({
10
// If engine is not provided an in-memory engine will be used.
11
// For production it is recommended to provide an engine:
12
// for this example we will use redis but any other database could be used
13
engine: {
14
// for best results the timeout should match the timeout
15
// set in frontend (updateInterval option, default: 3 minutes)
16
set: (k,v) => redis.set(k, v, "EX", 3 * 60),
17
get: (k) => {
18
let v = redis.get(k); redis.del(k); return v;
19
}
20
},
21
})
22
23
// The route URL is the callback URL you have set when you created One account app.
24
app.post('/oneaccountauth', oneaccount.auth, (req, res, next) => {
25
// NOTE: req.oneaccount is set when a user is authenticated,
26
// so never return code 200 if this object is not present
27
if (!req.oneaccount) {
28
return res.status(401).json({ error: 'unauthorized' });
29
}
30
31
// a user is authenticated and you can implement any logic your application
32
// needs. req.oneaccount holds data sent by the user
33
// after successful authentication
34
35
// since One account doesn't differentiate between sign up and sign in,
36
// you can use userId to check if the user signed up already on your website or not.
37
// the same way you can access any other data you requested from the user:
38
const { userId, firstName } = req.oneaccount;
39
// the object contains all fields (camelCased) that are defined in the app (Requested data)
40
// any data returned here would be sent to oneaccount-authenticated event on front-end e.g.:
41
return res.json({ firstName });
42
})
43
44
// OPTIONAL: in addition you can read error messages if any occured
45
app.use((err, req, res, next) => {
46
console.error(err.devMessage);
47
if (!err.status) err.status = 500;
48
res.status(err.status).send(err.message);
49
})
50
51
app.listen(process.env.PORT || 3000);
Copied!
Please find a simpler version of this tutorial (with minimum comments) and try it out live here: https://glitch.com/edit/#!/oneaccount For more options of authentication please check the library documentation: https://github.com/oilastudio/oneaccount-express
Last modified 1mo ago
Copy link