Depending on your usage, you can implement this with pop-up window or redirection
Popup window method
Add a message event listener like the following
window.addEventListener('message',asyncfunction(event) {console.log(event)// Check for the user data in the messageif (event.data?.type ==='well-oauth-token'&&event.data?.token) {// verify and store the token in a secure wayconstjwtToken=event.data?.token;// Close the popup windowif (popup) {popup.close(); } }});
{"id":"nX9d7H3cPQWmxrK2t3jh4UzIcGo5","linkedAddress":"0x3F8B9a45678dCd872e93Be582f54AFe912b874Dc","token": {"description":"Hello test.well, Well ID is the digital identity representing you in the ever growing network of wellness. Powered by WELL3.","image":"https://s3.well3.com/WellID/7752/1.jpg","tokenId":"77522218045247854526172210698749283758228288462773800836215567039694054862682","name":"test.well","attributes": [ {"trait_type":"Type","value":"Well ID" } ],"createdAt":"1713175672123" }}
{"error":"Invalid request"}
Telegram mini app oauth
Use the update account API to update your "tgResultCallbackURL", "tgBotID", "tgBotMiniAppURL"
Add the following code to your link/login well3 button callback
Receive the result callback in your backend. The request will be a GET request, with a jwt attached to the API query. Nodejs example code:
import jwt from'jsonwebtoken';router.get('/tg-well-oauth-callback',async (req, res, next) => {constdecoded=jwt.verify(req.query.jwt,WELL_OAUTH_PROJECT_SECRET);const {tgUserID,token} = decoded;// Code to bind the user in your server with the access tokenawaitredis.set(`tg-test:${tgUserID}`, token);res.send();});
Our telegram bot will have a button to redirect the user back to your telegram mini app with the tgBotMiniAppURL you provided, so it is important to fill in the URL.
When the user go back to your bot, fetch the access token stored in your server to get the Well user info.