Skip to content

Commit f745f6d

Browse files
committed
...
1 parent 0d493ce commit f745f6d

File tree

1 file changed

+67
-56
lines changed

1 file changed

+67
-56
lines changed

website/docs/packages/how-to-guides/use-query-inside-js-module.md

Lines changed: 67 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ This page shows how to create a custom authentication module using packages, whi
2424

2525
## Prerequisites
2626

27-
- A package that has already been created. For more information, see [Package and query modules tutorials](/packages/tutorial/query-module).
27+
- A UI Package that has already been created. For more information, see [Package and query modules tutorials](/packages/tutorial/query-module).
2828
- An authenticated datasource with user sign-in endpoints.
2929

3030

@@ -49,77 +49,79 @@ To secure your Appsmith application, you will need to set up a sign-in flow that
4949
/>
5050
</div>
5151

52-
53-
54-
55-
1. Create a new query module to authenticate users and retrieve an authentication token:
56-
52+
1. In the UI Module, create the login interface using the following widgets:
5753

5854
<dd>
5955

60-
*Example*:
61-
62-
```js
63-
//POST METHOD
64-
https://example.api.co/auth/v1/token?grant_type=password
65-
```
66-
67-
</dd>
56+
- **Input Widget** (`emailInput`) – Used to capture the user's email or username.
6857

58+
- **Input Widget** (`passwordInput`) – Used to capture the user's password. Set the input type to password to mask the input.
6959

70-
2. Create **Inputs** from the right-side property pane to dynamically pass user data to the query. For example, create two inputs: one for **email** and one for **password**.
60+
- **Button Widget** (`loginButton`) – Triggers the login process when clicked.
7161

62+
- **Text Widget** – Displays the application name.
7263

64+
- **Image Widget** – Displays the brand logo.
7365

74-
<ZoomImage src="/img/login-inputs-modules.png" alt="" caption="" />
66+
</dd>
7567

7668

69+
2. Create a new query to authenticate users and retrieve an authentication token:
7770

78-
3. Pass the user data into your API in the expected format. Use the `inputs` property to bind the user data.
71+
<dd>
7972

73+
*Example*:
8074

81-
<dd>
75+
```js
76+
//POST METHOD
77+
https://example.api.co/auth/v1/token?grant_type=password
78+
```
8279

83-
*Example*: if the API expects data in JSON format, select Body in the API, and then select JSON. Use the inputs property to bind the data, like:
80+
Body:
8481

8582
```js
8683
{
87-
"email": {{inputs.email}},
88-
"password": {{inputs.password}}
84+
"email": {{emailInput.text}},
85+
"password": {{passwordInput.text}}
8986
}
9087
```
9188

89+
The values for email and password are dynamically taken from the input widgets (`emailInput` and `passwordInput`) inside the module.
9290

93-
</dd>
9491

92+
</dd>
9593

96-
3. Create a new JS module to handle the sign-in process by calling the `sign_in` API query with the provided `email` and `password`, storing the response data in local storage, and navigating to the `Home` page upon successful authentication.
94+
3. Create a new JS Object inside the UI Module to manage the login logic. This function will call the login API query with the provided email and password, store the authentication token in local storage using storeValue, and navigate to the Home page upon successful authentication.
9795

9896
<dd>
9997

100-
* To pass data from the **JS module to the query module**, you can pass parameters at runtime using `run()`, like `sign_in.run({ email: email, password: password })`.
101-
102-
* To read the **JS module data in the query module**, create **Inputs** with the same parameter name, like `email` and `password`.
103-
98+
*Example:*
10499

105100
```js
106101
export default {
107-
signin: ({ email, password }) => {
108-
// Call the sign_in API query with the provided email and password
109-
return sign_in.run({ email: email, password: password })
110-
.then(data => {
111-
// Remove the user information from the response data for security reasons
112-
delete data.user;
113-
// Store each key-value pair from the response data in the local storage
114-
Object.keys(data).forEach(i => {
115-
storeValue(i, data[i]);
116-
});
117-
})
118-
.then(() =>
119-
// Navigate to the 'Home' page upon successful authentication
120-
navigateTo('Home')
121-
);
102+
async loginUser() {
103+
if (!emailInput.text || !passwordInput.text) {
104+
showAlert("Enter both email and password", "error");
105+
return;
106+
}
107+
108+
try {
109+
const response = await login.run({
110+
email: emailInput.text,
111+
password: passwordInput.text
112+
});
113+
114+
if (response.token) {
115+
await storeValue("access_token", response.token);
116+
// Navigate to the 'Home' page upon successful authentication
117+
navigateTo("Home");
118+
} else {
119+
showAlert("Login failed", "error");
120+
}
121+
} catch (e) {
122+
showAlert("Invalid credentials", "error");
122123
}
124+
}
123125
};
124126
```
125127

@@ -128,50 +130,59 @@ For any future user actions, use the stored access token to access the relevant
128130

129131
</dd>
130132

133+
4. Set the **onClick** event of the Login button to run the loginUser function defined in the JS Object.
131134

132-
4. Publish the package.
133-
134-
135+
5. To dynamically change the login UI based on the app using the module, use inputs to pass data from the parent application into the module. This allows each app to control elements like the displayed app name and logo without modifying the module itself.
135136

136-
## Integrate custom authentication
137+
<dd>
137138

138-
Follow these steps to use the login authentication module within your application:
139+
*Example:* Create two inputs, `appName` and `logoUrl`, to reuse the same login module across different applications such as an HR portal or a CRM dashboard.
139140

140141

142+
`appName`: Set the Text widget’s text property to: `{{ this.params.appName }}`
141143

142-
1. Open your **App** from the homepage and ensure that both the app and modules share the same workspace.
144+
`logoUrl`: Set the Image widget’s source property to: `{{ this.params.logoUrl }}`
143145

144-
2. Create a simple login form using widgets such as Text and Input fields, tailored to meet your specific requirements.
146+
</dd>
145147

146-
3. Select the JS tab on the Entity Explorer, add the **Login JS module**, and pass login form data into function parameters for authentication.
147148

149+
6. To pass login state or token ID from the module back to the parent app, use outputs. This allows you to expose specific values such as authentication tokens or login status that the parent application can access and respond to.
148150

149151
<dd>
150152

153+
*Example*: Create an output named `token` and pass the token ID received from the login API:
151154

152-
To pass data from the **App to a JS module**, you can set **Inputs** parameters or pass it by calling the respective function with parameters, like `login([email protected], password@123)`.
155+
```js
156+
//token
157+
{{ authUtils.loginUser.data.token }}
158+
```
153159

160+
The token can then be accessed in the parent app using `LoginModule1.outputs.token`.
154161

155-
<ZoomImage src="/img/inputs-js1.png" alt="" caption="" />
156162

157163
</dd>
158164

159-
4. Set the **onClick** event of the Submit/Login button to run the JS function.
165+
166+
7. Publish the package.
167+
168+
169+
8. To integrate custom authentication into your app, open the application and navigate to the UI tab. Drag the login module onto the desired page and configure the appName and logoUrl inputs.
160170

161171
<dd>
162172

173+
Based on your application’s requirements, you can use the token output to store the login state, authorize API requests, or redirect users after successful authentication. To log out a user, call the revoke API to invalidate the access token, use the [clear store()](/reference/appsmith-framework/widget-actions/clear-store) action to remove stored user data, and redirect the user to the `LoginPage`.
163174

164175

165-
When the button is clicked, the JS module verifies whether the user's email exists in the database. If the email and password match, it redirects the user to a new page.
176+
</dd>
166177

167178

179+
With this setup, the login module can now securely authenticate users, dynamically adapt to different applications, and expose login state back to the parent app. Once a user logs in successfully, they are automatically navigated to the home page or any other page you specify through input configuration.
168180

169181

182+
If your application uses short-lived access tokens, consider implementing a refresh token strategy to maintain session continuity. For details on when and how to use refresh tokens, see the Refresh Token Handling Guide.
170183

171-
</dd>
172184

173185

174-
To log out a user, call the revoke API to invalidate the access token, use the [clear store()](/reference/appsmith-framework/widget-actions/clear-store) action to remove stored user data, and redirect the user to the `LoginPage`.
175186

176187

177188

0 commit comments

Comments
 (0)