Flask
Flask is a micro web framework written in Python. It is classified as a micro framework because it does not require particular tools or libraries.

Let's Get your Flask App running with SAWO
🙌

Requirements

Python, pip (package-management system written in Python used to install and manage software packages)

Steps

1. The first step to getting started with SAWO API integration into your Django website/application is to install the sawo package as given below:
1
pip install sawo
Copied!
2. Next, you have to import createTemplate, getContext, and verifyToken methods from Sawo. The getContext method is used to create sawo dictionary object from data gain from Django Database. The ways to create the template and verify the token are given in subsequent steps.
1
from sawo import createTemplate, getContext, verifyToken
Copied!
3. To use SAWO Login you would need an API key which can be obtained by creating a project in the sawo dashboard.
4. Once you create your project, you would need to set your project name and hostname. 4.1. For development in a local machine, the hostname should be set to 'localhost'.
If using ''localhost" as hostname is not working for you, try "127.0.0.1"
4.2. For production, the hostname should be set to your domain.
If you are adding your domain do not add 'https://', ''http://', 'www' or even trailing backslash. Example: https://dev.sawolabs.com/ should be kept as dev.sawolabs.com
5. Copy the API key from the project and keep it safe and secure.
The best practice to store your API key is to store values in .env so that they are not exposed.
6. Getting Started with the Setup in Flask:
  • First, you have to create a template for SAWO password-less and OTP-less Authentication for your Flask website. Use the code given below for the same:
    1
    createTemplate("./<filepath>",flask=True)
    2
    #example
    3
    createTemplate("./templates/partials",flask=True)
    Copied!
  • Next, you have to send this data required by _sawo.html. The variable names used in _sawo.html template are sawo.auth_key, sawo.identifier and sawo.to Therefore,to send this data you have to create a JSON.
Note: The "to" route should be a post route that can receive posted data. If you don’t know how data is passed to templates in Django or Flask. We will suggest looking into it first
METHOD USING ADMIN AND DATABASE TO SAVE CONFIG FOR SAWO
  • Creating fields for sawo api_key and identifier to set it from admin dashboard. Copy this code in models of your app
1
class Config(models.Models):
2
api_key = models.CharField(max_length=200)
3
identifier = models.CharField(max_length=200)
4
choices = [("email","Email"),("phone_number_sms","Phone")]
Copied!
  • Setting up view.py of the app. Note: Route should be the receiving end where you can handle post request
1
from models import Config
2
from sawo import getContext
3
4
def <your_function>(request):
5
config = Config.objects.order_by('-api_key')[:1]
6
context = {
7
"sawo" = getContext(config,<route>) #the route where you will recieve
8
the payload sent by sdk
9
}
10
11
12
#example
13
14
def index(request):
15
config = Config.objects.order_by('-api_key')[:1]
16
context = {
17
"sawo" = getContext(config,"login")
18
19
}
Copied!
FLASK
1
"sawo":{
2
"auth_key": "<api_key>",
3
"identifier": "email | phone_number_sms"
4
"to": <route> #the route where you will recieve the payload sent by sdk
5
}
6
7
#example
8
9
"sawo":{
10
"auth_key": "785ha-hdjsdsd-799-ss345",
11
"identifier": "email | phone_number_sms"
12
"to": login
13
}
Copied!
Once the SAWO SDK is successfully set up, a login form will be rendered in the provided container as displayed in the picture below:
Final Render of SAWO Login
Congratulations !! The SAWO API is now ready to be used in your Flask application 🤘.

You can also check out SAWO's
Flask Sample Code and Deployed App.

It's okay, we get it! You got Stuck!
😞
Feel free to contact us on #ask-for-help on our Discord

Last modified 19d ago