
You’ll need the Client ID, Client Secret, and Authorization URL.
The Authorization URL supplied by nOS.app ends with ?state=example. Replace example with a randomly generated that you in the user’s browser (e.g. with a cookie or session key).
When the user authorizes the application, nOS.app adds the state variable in the Redirect URI. You can use this to check if the same user has authorized the application in the same browser session.
This is a functional PHP example. Make sure to replace the common variables with your own app variables (Client ID, Secret).
Redirect the user to the $url with a button or redirect. The user will be redirected to nOS.app and see the following dialog:
4. Handle the Authorization
Upon clicking Approve, the user is redirected to your Redirect URI. The Redirect URI will have 2 variables: codeand state.
Flow
- Compare the
statevalue with thestateyou stored in the user’s browser session in Step 3 to verify the user session. - If the
statematches, make a POST request to nos.app to retrieve the user’s Access Token. - Store the Access Token to your database.
PHP Example
if (!empty($_GET['code']) && !empty($_SESSION['state']) && $_SESSION['state'] === $_GET['state']) elseif (!empty($_SESSION['state']) && !empty($_GET['state']) && $_SESSION['state'] !== $_GET['state'])
5. Using the Access Token to Read User Data
You can use the Access to register the user to your app, log them in, connect user data to an existing account on your app, and build features around your users’ accounts. For example you can check if a user holds a specific amount of your on one of their verified addresses (retrieved by nOS.app API), and unlock a feature in an app, or grant bonus points/give starting XP in a game, etc.
- If you want to register the user, you should retrieve the user’s Account Details using the nOS.app API and it in your database together with the Access Token.
- If you want to log in the user, you can check if the Access Token belongs to an existing user in your database.
In this tutorial we’re saving the Access to the user’s browser session. This is not recommended in production. You should the Access in your database, and use a separate session to keep the user logged in.
PHP Example
/**
* The code below is used to display the nOS.app user account data for an authorized user.
*/
if (!empty($_SESSION['access_token']))
Full PHP Example
For a full PHP example, view the nOS ID documentation .
Good luck!
Published at Wed, 22 May 2019 18:28:07 +0000