The tutorial lets you implement LoginRadius user registration, login, profile, and log out in your python based application.
You must have python installed, and the minimum supported version is 2.7.
New to Python? Check out this reference.
When you signed up for the LoginRadius account, it created an app for you. This app is linked to a ready to use web page - Auth Page (IDX).
Auth Page (IDX) reflects the configuration changes that you make in LoginRadius Dashboard. You can utilize this webpage for authentication requirements on your python application.
Create an account to get started if you don’t have one yet!
In your LoginRadius Dashboard, select your app, then navigate to the Auth Page (IDX) section located in the left navigation bar and click the Theme Customization section. There you can select a design theme for your login page, or further customize the content displayed:
To preview your login page’s theme, click Go to your Login Page link highlighted on the above screen.
Features like Email and Password login, User registration, Forgot password, and Remember me are already configured for your Auth Page(IDX).
Before using any of the APIs or Methods that LoginRadius provides, you need to get your App Name, API Key, and API Secret.
In your LoginRadius Dashboard, navigate to Configuration > API Credentials and click the API Key And Secret subsection to retrieve your API Credentials.
Check if pip package manager is installed by running the following command:
Install virutalenv by running the following command:
pip install virtualenv
Create the virtual environment by running the following command:
Activate the virtualenv, the following are the commands for MacOS/Linux and Windows:
- For MacOS/Linux:
- For Windows:
- For MacOS/Linux:
This implementation uses the flask framework to create Python API.
Install flask by running the following command:
pip install Flask
- Create a requirement file
pip freeze > requirements.txt.
Create a new file name
app.pyand add the following code snippet:
from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "Hello World!" if __name__ == "__main__": app.run()
Now, try running new Flask application with the command
python app.py. Open http://localhost:5000 in your browser, and you should see the “Hello World!” response.
Add project dependency and LoginRadius SDK using pip by running the following command in the command line:
pip install requests loginradius-v2==11.0.0
Create a file name
config.py and add the following lines:
from LoginRadius import LoginRadius as LR API_KEY = "" API_SECRET = "" LR.API_KEY = API_KEY LR.API_SECRET = API_SECRET LR.CUSTOM_DOMAIN = "https://example.com/" loginradius = LR()
Replace the placeholders in the above config object with the following values:
- apiKey: API Key obtained in the Get Credentials step.
- apiSecret: API Secret obtained in the Get Credentials step.
This tutorial uses Auth Page(IDX) for authentication, where Registration and Login functionality is already implemented.
Navigate your Register or Login links or buttons to the following URLs:
Registration Page URL:
https://<LoginRadius APP Name>.hub.loginradius.com/auth.aspx?action=register&return_url=<Return URL>
Login Page URL:
https://<LoginRadius APP Name>.hub.loginradius.com/auth.aspx?action=login&return_url=<Return URL>
- LoginRadius App Name is the name of your app as mentioned in the Get Credentials step.
- return_url is where you want to redirect users upon successful registration or login. Whitelist your domain if you are not using Local Domain for this tutorial.
return_url can be your website, frontend app, or backend server url where you are handling the access token.
Once the authentication is done using Auth Page (IDX), the default script of LoginRadius sends an access token in the query string as a token parameter with the returnurl. The returnurl will access the Python backend API with query parameter (access token).
The following is an example of the access token in the query string with the Return URL:
If returnurl is of frontend, pass the token to backend python API (eg: http://localhost:5000) from the application. Or you can use the path of the back end API as the returnurl.
Add the following API snippet to
app.pyto get the user profile using the access token:
@user.route("/user") def get_user(): res = loginradius.authentication.get_profile_by_access_token(request.args['token']) if 'ErrorCode' in res: return abort(Response(res['Description'], 400)) else: return jsonify(res)
- Run the API Server by executing the following command in the command line:
Open your Auth Page(IDX) registration URL
https://<LoginRadius APP Name>.hub.loginradius.com/auth.aspx?action=register&return_url=<Return URL>. It will display the following screen:
- Register a user here and then log in. Upon successful login, it will redirect you to the return url with the access token. In response, you will get a user profile in JSON format.
Similarly, you can implement more features using Python SDK.
In addition to Registration and Login actions, the Auth Page (IDX) supports more actions. Refer to this document for more information.
To whitelist your domain, in your LoginRadius Dashboard, navigate to Configuration > Whitelist Your Domain and add your domain name:
- Refer to GitHub Demo to know how to implement various LoginRadius features using SDK Functions.