Log in with A-chat
A button that lets people sign in to your site with their A-chat account. The person sees who asks and what you get (name, @username, picture, language; anything more only if they allow it), and allows it on the web or on their phone. Never their phone number, e-mail, chats or contacts.
Try it
The answer appears here.
1. Register the site
In A-chat: Settings → For developers → Sites (or POST https://api.a-chat.app/api/login/sites): the name, the domains (e.g.
example.com), the return addresses for OAuth, and, if you want, your bot. You get a client_id (public) and a secret
(keep it on your server).
2. Put the button on the page
<script async src="https://a-chat.app/login/widget.js"
data-client-id="7000000123"
data-onauth="onAChatAuth(user)"
data-request-access="bio"></script>
<script>
function onAChatAuth(user) {
if (!user) return; // cancelled
fetch('/auth/achat', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(user) });
}
</script>
Or data-auth-url="https://example.com/auth/achat": the page goes there with the fields in the query. data-mode="redirect"
opens the consent page in place of a popup. On a computer the popup also shows a QR code to confirm on the phone.
3. Check the answer on your server
The fields are id, first_name, username, photo_url (works for an hour: keep a copy of the
picture), language_code, auth_date, hash. Your server checks them with your site's secret:
// Node.js
const crypto = require('crypto');
function checkAChat(fields, secret) {
const { hash, ...rest } = fields;
const dataCheckString = Object.keys(rest).sort().map((k) => `${k}=${rest[k]}`).join('\n');
const key = crypto.createHash('sha256').update(secret).digest();
const mine = crypto.createHmac('sha256', key).update(dataCheckString).digest('hex');
const fresh = Date.now() / 1000 - Number(rest.auth_date) < 86400;
return fresh && crypto.timingSafeEqual(Buffer.from(mine), Buffer.from(String(hash)));
}
# Python
import hashlib, hmac, time
def check_achat(fields: dict, secret: str) -> bool:
rest = {k: v for k, v in fields.items() if k != 'hash'}
data_check_string = '\n'.join(f'{k}={rest[k]}' for k in sorted(rest))
key = hashlib.sha256(secret.encode()).digest()
mine = hmac.new(key, data_check_string.encode(), hashlib.sha256).hexdigest()
return hmac.compare_digest(mine, fields.get('hash', '')) and time.time() - int(rest['auth_date']) < 86400
id is the person's number: the same number your bot and your mini app get, so you can match them.
Or OAuth 2.0 / OpenID Connect
Discovery: https://api.a-chat.app/api/login/.well-known/openid-configuration. Send the browser to
https://web.a-chat.app/oauth/authorize?response_type=code&client_id=…&redirect_uri=…&scope=openid%20profile&state=…&code_challenge=…&code_challenge_method=S256&nonce=…,
exchange the code at POST /api/login/token (your client_id and secret, the code_verifier), and read the signed
id_token (ES256, keys at /api/login/jwks) or GET /api/login/userinfo.