Dear Trailblazers,
In this blog post, I am going to talk about how we can create a Mulesoft application and we can do Login or Signup using a HTML page.
Let’s Start
Use Case :
Design a UI for a Custom Login or Signup Page. Provide the fields for username and password and a Login button for Login Page and the required fields for signup page and the Signup button. On clicking the Login Button or Signup button, The details are posted as a Body and further can be accessed by our Mule Application.
For this use case, I am just accepting the payload coming from UI and Display some dummy data and will show you how to display an Array of elements using Dataweave which has HTML components.
Note: You can also build a logic something like verifying username and password against the DataBAse. But again, it’s your job to implement with help of this article 🙂
Connectors Required:
- Listener
- Parse Template
- Transform Message
- Set Payload
Additional files required
- Login.html
- Signup.html
Note: – All the HTML file must be residing under src/main/resources folder.
Step1 – Add listener for your html page
When we are writing the HTML code, we know that the <form> tag has an attribute called to action so we need to define the action to the correct listener which we will develop to read the data from the page.
For example, your listener is listening at /login then provide login and in my case, my listener is listening at /panther/login-signup so I provided the same. See the image
Make sure that you define method = post or else your credentials will be passed as query params and are visible in the URL!
Here is my listener configuration
The first transformation is transforming the payload to json form
and below is the code for the second transformation
%dw 2.0
var a= [{ name : "Amit Singh" },{ name : "aka" },{name:"SFDCPanther"}]
output application/xml
---
html: {
head: {
title: 'Custom Login Page'
},
body: {
h1: "Welcome to Custom Login Page " ++ payload.usrname default "" ++" !",
p: "Your password is " ++ payload.password default "" ++" !",
p: {
h2 :"The View you are seeing is developed by Transform Message. See below array of data how you can iterate payload using map ..."
},
ul: a map {
h3 @(style : "color : red") : li: $.name
}
},
marquee @(style : "color : blue") : h1: "Mule Rocks!!! Thanks to Sravan Lingam Mule Guru"
}
Step2 – Parse Your HTML Ready
Now as we have our all HTML files ready, we need to parse that html file using the parse Template connector.
Create a flow that is your base URL and place a parse template connector and give the file name of your HTML (in this case Login.html). Make sure you are placing your HTML file in the root of src/main/resources
Check the configuration below
In the above image, you can see that I am using Signup.html page which is located in the src/main/resources folder.
According to MuleSoft docs :
Parse Template is the Mule component to use for processing a template and obtaining a result. A template is defined as text with embedded Mule expressions that are evaluated and replaced with their result.
You can configure template through an external file reference, or you can embed it in the component definition itself.
By default, the data(in this case username and password) that is coming from UI is of media-type application/x-www-form-urlencoded; charset=UTF-8
Now, the final step is to build a UI using DataWeave. Yes, we can build the HTML UI using dataweave.
HTML is in XML . So write the HTML tags as keys in data wave with output application/XML. This will create a HTML based XML data. Next, drop a set payload and set content type as text/HTML. The reason why because data weave does not allow output type as text/HTML!
Now, we are all done
Here is the complete flow
Parse Template
Output
Full code (XML)
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<http:listener-config name="HTTP_Listener" doc:name="HTTP Listener config" doc:id="17d611b4-7a6d-4c06-a50c-8e6e60143657" basePath="/panther">
<http:listener-connection host="0.0.0.0" port="8081" />
</http:listener-config>
<flow name="loginSingupFlow" doc:id="9cf0d913-aed0-444e-97e2-810ba057df27" >
<http:listener doc:name="Listener" doc:id="74a8525d-3e35-4723-a100-ced7d6a57a87" config-ref="HTTP_Listener" path="/loginSignup"/>
<parse-template doc:name="Parse Template" doc:id="7812d5ba-6b50-42e2-8818-0c3968483f29" location="Singup.html"/>
</flow>
<flow name="singupFlow" doc:id="6af75cae-fbb2-4d30-95a4-ca592a58e117" >
<http:listener doc:name="Listener" doc:id="a1982bba-54b1-4e6e-ad00-c9ff683796aa" config-ref="HTTP_Listener" path="/login"/>
<parse-template doc:name="Parse Template" doc:id="3e41ee43-bc0a-448e-9e91-5a2b044cc72f" location="Login.html"/>
</flow>
<flow name="LoginFlow" doc:id="c41738c6-ce6b-4d09-b013-365bfc12f4be">
<http:listener doc:name="Listener" doc:id="d624323e-a00c-45b8-951b-f86c4a8bbd48" config-ref="HTTP_Listener" path="/login-signup" />
<ee:transform doc:name="Transform Message" doc:id="83814787-721b-45d0-9d4f-655599ac22a1">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
payload
]]></ee:set-payload>
</ee:message>
</ee:transform>
<ee:transform doc:name="Transform Message" doc:id="f0024978-6da3-4be7-97fa-fd783fb7328d">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
var a= [{ name : "Amit Singh" },{ name : "aka" },{name:"SFDCPanther"}]
output application/xml
---
html: {
head: {
title: 'Custom Login Page'
},
body: {
h1: "Welcome to Custom Login Page " ++ payload.usrname default "" ++" !",
p: "Your password is " ++ payload.password default "" ++" !",
p: {
h2 :"The View you are seeing is developed by Transform Message. See below array of data how you can iterate payload using map ..."
},
ul: a map {
h3 @(style : "color : red") : li: $.name
}
},
marquee @(style : "color : blue") : h1: "Mule Rocks!!! Thanks to Sravan Lingam Mule Guru"
}]]></ee:set-payload>
</ee:message>
</ee:transform>
<set-payload value="#[payload]" doc:name="Set Payload" doc:id="3d0d2cb3-edb9-4120-ab54-98e8fe2d8f66" mimeType="text/html" />
</flow>
<flow name="singup-Flow" doc:id="a5b4d232-0c3c-435c-8b71-d0140e6d6190" >
<http:listener doc:name="Listener" doc:id="e995206c-71ad-4360-8cf6-78d036bc0507" config-ref="HTTP_Listener" path="/signup"/>
<ee:transform doc:name="Transform Message" doc:id="03ae3a86-2dac-4c4e-95e0-8155ba1efdee" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
payload
]]></ee:set-payload>
</ee:message>
</ee:transform>
<ee:transform doc:name="Transform Message" doc:id="95d54812-00e6-4b41-ab26-bde1f8326542" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
var a= [{ name : "Amit Singh" },{ name : "aka" },{name:"SFDCPanther"}]
output application/xml
---
html: {
head: {
title: 'Custom Login/Signup Page'
},
body: {
h1: "Welcome to Custom Login/Signup Page " ++ payload.usrname default "" ++" !",
p: "Your password is " ++ payload.password default "" ++" !",
ul: a map {
h3 @(style : "color : red") : li: $.name
}
},
marquee @(style : "color : blue") : h1: "Mule Rocks!!! Thanks to Sravan Lingam Mule Guru"
}]]></ee:set-payload>
</ee:message>
</ee:transform>
<set-payload value="#[payload]" doc:name="Set Payload" doc:id="2a9fb648-852d-45c4-98ec-8bd5eb7a5a3e" />
</flow>
</mule>
Demo
Happy Learning
#Mulesoft #Mule4
[…] your experience. We’ll assume you’re ok with this, but you can opt-out if you wish. Accept Read […]
Where can I find login and signup.html pages or code.
Thanks!
Let me check if I can get this project.