Python
Latest library version: 3.2.2
Even though in this tutorial we use FastApi, 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:

Install the library:
1
pip install oneaccount
Copied!
Setup the library:
1
from oneaccount import OneAccount
2
3
# Any engine could be used instead of RedisEngine that
4
# implements the set and get methods
5
# oneaccount from with an InMemoryEngine
6
# for local development or for small services
7
# for large services it is highly recommended to use
8
# redis or any other persisting database engine
9
oa = OneAccount(RedisEngine())
10
11
@router.post("/oneaccountauth", status_code=status.HTTP_200_OK)
12
async def oneaccount_auth(request: Request):
13
14
unauthorized_errror = JSONResponse(
15
status_code=status.HTTP_401_UNAUTHORIZED,
16
content={"success": False}
17
)
18
19
try:
20
data = await oa.auth(request)
21
if not data:
22
return unauthorized_errror
23
except:
24
return unauthorized_errror
25
26
return data
27
28
29
class RedisEngine:
30
31
def __init__(self):
32
self.client = redis.Redis(host='localhost', port=6379, db=0)
33
34
def set(self, key: str, value: str) -> None:
35
self.client.set(key, value)
36
37
def get(self, key: str) -> str:
38
data = self.client.get(key)
39
self.client.delete(key)
40
return data
Copied!
Last modified 2mo ago
Copy link