This is a simple walkthrough on handling user authentication using Pi SDK and API endpoint. For a detailed explanation, please refer to Pi.authenticate for SDK reference and /me for API reference.
Here is a list of steps you can follow when you authenticate users for your app:
Using the Pi SDK authenticate()
, you can obtain user information along with the access token.
const authRes = await window.Pi.authenticate(scopes, onIncompletePaymentFound);
To verify the data you got in step 1, you need to make a GET request to /me
Pi API endpoint, with the access token included in the header. If the access token is valid, it will return a response with UserDTO. However, if the token is invalid, it will return HTTP 401 Unauthorized code.
const me = await axios.get('https://api.minepi.com/v2/me', {headers: {'Authorization': `Bearer ${accessToken}}});