How to create a serverless function to login using REST API

We have started looking at RESTful APIs provided by Hypi’s low code platform.

In the previous post, we have seen how to login to a Hypi App instance using REST API.

In this post, let’s try something different!

We will see how to create a serverless function to login using REST API.
This example provides an insight into how to combine REST and serverless functions.

Moving on to REST APIs, here we will use the email of an account to login instead of username. Again, email login can be triggered using either GET or POST method.

Notice an important change for login using email:

  • The URL has loginByEmail instead of login

/rest/v1/fn/query/loginByEmail?email=x&password=y&type=query

GET method:

First let’s write a python script that would use the GET method to login.

import json
import requests
from string import Template
# Function to create Hypi User Account
def main(args):
    password = args.get("pwd")
    email = args.get("email")
    my_headers = {'hypi-domain' : 'inclemency.apps.hypi.app' , 'content-type' : 'application/json'}
    query_value = Template('https://api.staging.hypi.dev/rest/v1/fn/query/loginByEmail?email=${email}&password=${password}&type=query').substitute(email=email,password=password)
    response = requests.get(query_value, headers=my_headers)
    text = response.text
    return json.loads(text)

You may check this post to understand how to use the Python Requests module to interact with REST APIs of Hypi.
We have written a Template to form the URL using input email and password.

Let’s create and invoke an action using the above script. Save the script in a file login.py.

>wsk action create login login.py
ok: created action login

>wsk action invoke login --param pwd customer@abc.io --param email customer@abc.io --result
{
    "data": {
        "loginByEmail": {
            "__typename": "AccessToken",
            "errorCode": null,
            "errorMsg": null,
            "hypi": null,
            "sessionExpires": 1645693675,
            "sessionToken": "Auth Token"
        }
    }
}

Check this guide on how to create and use a serverless function.

Now, we will define and execute the serverless function on Hypi’s low code platform.

#define
hypiInstanceLogin(email: String,pwd: String): Json @tan(type:OpenWhisk, name:"login")

#query
{
  hypiInstanceLogin(email: "customer@abc.io", pwd: "customer@abc.io")
}

#result
{
  "data": {
    "hypiInstanceLogin": {
      "data": {
        "loginByEmail": {
          "__typename": "AccessToken",
          "errorCode": null,
          "errorMsg": null,
          "hypi": null,
          "sessionExpires": 1645704846,
          "sessionToken": "Auth Token"
        }
      }
    }
  }
}

POST method:

If you want to use the POST method to login, there will be slight changes. You need to send the email and password as data and not as query parameters. Here is the python script for your reference.

import json
import requests
from string import Template
# Function to create Hypi User Account
def main(args):
    password = args.get("pwd")
    email = args.get("email")
    my_headers = {'hypi-domain' : 'inclemency.apps.hypi.app' , 'content-type' : 'application/json'}
    query_value = Template('{"email": "${email}","password": "${password}"}').substitute(email=email,password=password)
    response = requests.post('https://api.staging.hypi.dev/rest/v1/fn/query/loginByEmail',data=query_value, headers=my_headers)
    text = response.text
    return json.loads(text)

Check the Hypi REST APIs POSTMAN collection for the Login with Email requests in different programming languages! Click </> and choose the programming language of your choice.

Run in Postman