One account
Search…
⌃K
Links

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:
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:

Install the library:
pip install oneaccount
Setup the library:
from oneaccount import OneAccount
# Any engine could be used instead of RedisEngine that
# implements the set and get methods
# oneaccount from with an InMemoryEngine
# for local development or for small services
# for large services it is highly recommended to use
# redis or any other persisting database engine
oa = OneAccount(RedisEngine())
@router.post("/oneaccountauth", status_code=status.HTTP_200_OK)
async def oneaccount_auth(request: Request):
unauthorized_errror = JSONResponse(
status_code=status.HTTP_401_UNAUTHORIZED,
content={"success": False}
)
try:
data = await oa.auth(request)
if not data:
return unauthorized_errror
except:
return unauthorized_errror
return data
class RedisEngine:
def __init__(self):
self.client = redis.Redis(host='localhost', port=6379, db=0)
def set(self, key: str, value: str) -> None:
self.client.set(key, value)
def get(self, key: str) -> str:
data = self.client.get(key)
self.client.delete(key)
return data