Skip to content

Commit 2d1e257

Browse files
committed
...
1 parent d1e6c39 commit 2d1e257

File tree

4 files changed

+115
-126
lines changed

4 files changed

+115
-126
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
2+
3+
## Refresh access token
4+
5+
To keep your access uninterrupted and secure across all apps, follow these steps to refresh OAuth tokens and extend your authentication:
6+
7+
1. Create a query module to call the token refresh endpoint provided by your authentication service. For OAuth services, ensure that you add the refresh token in the request payload to receive a new access token.
8+
9+
<dd>
10+
11+
*Example:*
12+
13+
```api
14+
https://api.example.io/api/user/token-auth/
15+
```
16+
17+
</dd>
18+
19+
2. Create a new JS module to refresh the token:
20+
21+
<dd>
22+
23+
*Example:*
24+
25+
```jsx
26+
export default {
27+
refreshAccessToken: () => {
28+
// Retrieving the refresh token from the appsmith store
29+
const refreshToken = appsmith.store.refreshtoken;
30+
31+
// Checking if a refresh token exists
32+
if (refreshToken) {
33+
// Executing the refresh API with the retrieved refresh token
34+
return refreshAPI.run({ refreshtoken: refreshToken })
35+
.then(newTokens => {
36+
// Updating the access token in the appsmith store
37+
storeValue('accesstoken', newTokens.accesstoken);
38+
39+
// Checking if a new refresh token is provided and updating it
40+
if (newTokens.refreshtoken) {
41+
storeValue('refreshtoken', newTokens.refresh_token);
42+
}
43+
})
44+
.catch(error => {
45+
console.log('Error refreshing token:', error);
46+
});
47+
}
48+
}
49+
};
50+
```
51+
52+
</dd>
53+
54+
3. Create a new query module to handle the generation of tokens when an authentication token expires.
55+
56+
<dd>
57+
58+
*Example:*
59+
60+
```js
61+
https://api.example.io/api/user/token-refresh/
62+
```
63+
64+
In the API configuration, provide the refresh token from the store in the request body:
65+
66+
```js
67+
{
68+
"refresh_token":{{appsmith.store.authRefreshToken}}
69+
}
70+
```
71+
72+
</dd>
73+
74+
4. Publish the package.
75+
76+
77+
Now you can use the JS module within your app to fetch details using query or refresh tokens on page load, ensuring continuous authentication access.
78+
79+
80+
81+
82+
83+
84+
85+
86+

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

Lines changed: 27 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ tags={[
1818

1919
<!-- vale on -->
2020

21-
This page shows how to create a custom authentication module using packages, which allows you to reuse the same module across different applications to authenticate users efficiently.
22-
21+
This page shows you how to create a reusable custom authentication module using UI Packages. It guides you through building a secure login flow that can be shared across multiple applications, while allowing each app to customize its branding and behavior as needed.
2322

2423

2524
## Prerequisites
@@ -31,38 +30,26 @@ This page shows how to create a custom authentication module using packages, whi
3130

3231
## Configure package
3332

34-
To secure your Appsmith application, you will need to set up a sign-in flow that requires users to authenticate before accessing the app. Follow these steps to create the sign-in flow:
35-
33+
To create a reusable and secure sign-in flow, you’ll configure a Login UI Module that handles user authentication, supports dynamic branding, and exposes login state back to the parent app. Follow these steps to build the login flow inside the package:
3634

3735

38-
<div style={{ position: "relative", paddingBottom: "calc(50.52% + 41px)", height: 0, width: "100%" }}>
39-
<iframe
40-
src="https://demo.arcade.software/EbrwbFYIQoyrqb738kX8?embed"
41-
frameBorder="0"
42-
loading="lazy"
43-
webkitAllowFullScreen
44-
mozAllowFullScreen
45-
allowFullScreen
46-
allow="fullscreen"
47-
style={{ position: "absolute", top: 0, left: 0, width: "100%", height: "100%" }}
48-
title="Appsmith | Connect Data"
49-
/>
50-
</div>
51-
52-
1. In the UI Module, create the login interface using the following widgets:
36+
1. In the **UI Module**, create the login interface using the following widgets:
5337

5438
<dd>
5539

5640
- **Input Widget** (`emailInput`) – Used to capture the user's email or username.
57-
5841
- **Input Widget** (`passwordInput`) – Used to capture the user's password. Set the input type to password to mask the input.
59-
6042
- **Button Widget** (`loginButton`) – Triggers the login process when clicked.
61-
6243
- **Text Widget** – Displays the application name.
63-
6444
- **Image Widget** – Displays the brand logo.
6545

46+
47+
<ZoomImage
48+
src="/img/login-module.png"
49+
alt=""
50+
caption=""
51+
/>
52+
6653
</dd>
6754

6855

@@ -91,7 +78,7 @@ The values for email and password are dynamically taken from the input widgets (
9178

9279
</dd>
9380

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.
81+
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.
9582

9683
<dd>
9784

@@ -102,7 +89,7 @@ export default {
10289
async loginUser() {
10390
if (!emailInput.text || !passwordInput.text) {
10491
showAlert("Enter both email and password", "error");
105-
return;
92+
return { token: "", isLoggedIn: false };
10693
}
10794

10895
try {
@@ -113,13 +100,15 @@ export default {
113100

114101
if (response.token) {
115102
await storeValue("access_token", response.token);
116-
// Navigate to the 'Home' page upon successful authentication
117103
navigateTo("Home");
104+
return { token: response.token, isLoggedIn: true };
118105
} else {
119106
showAlert("Login failed", "error");
107+
return { token: "", isLoggedIn: false };
120108
}
121109
} catch (e) {
122110
showAlert("Invalid credentials", "error");
111+
return { token: "", isLoggedIn: false };
123112
}
124113
}
125114
};
@@ -130,23 +119,24 @@ For any future user actions, use the stored access token to access the relevant
130119

131120
</dd>
132121

133-
4. Set the **onClick** event of the Login button to run the loginUser function defined in the JS Object.
122+
4. On the **UI** tab, set the **onClick** event of the Login button to run the `loginUser` function defined in the JSObject.
134123

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.
124+
125+
5. To customize the login UI based on the app using the module, use [Inputs](/packages/reference/ui-module#inputs) to pass values such as the application name and logo URL from the parent app. This allows each application to display its own branding, such as “HR Portal” or “CRM Dashboard”, without modifying the module.
136126

137127
<dd>
138128

139129
*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.
140130

141131

142-
`appName`: Set the Text widget’s text property to: `{{ this.params.appName }}`
132+
- `appName`: Set the Text widget’s text property to: `{{ inputs.appName }}`
133+
- `logoUrl`: Set the Image widget’s source property to: `{{ inputs.logoUrl }}`
143134

144-
`logoUrl`: Set the Image widget’s source property to: `{{ this.params.logoUrl }}`
145135

146136
</dd>
147137

148138

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.
139+
6. To pass login state or token ID from the module back to the parent app, use [Outputs](/packages/reference/ui-module#outputs). This allows you to expose specific values such as authentication tokens or login status that the parent application can access and respond to.
150140

151141
<dd>
152142

@@ -159,14 +149,13 @@ For any future user actions, use the stored access token to access the relevant
159149

160150
The token can then be accessed in the parent app using `LoginModule1.outputs.token`.
161151

162-
163152
</dd>
164153

165154

166-
7. Publish the package.
155+
7. **Publish** the package.
167156

168157

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.
158+
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.
170159

171160
<dd>
172161

@@ -176,100 +165,14 @@ Based on your application’s requirements, you can use the token output to stor
176165
</dd>
177166

178167

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.
180-
181-
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.
183-
184-
185-
186-
187-
188-
189-
190-
191-
192-
193-
## Refresh access token
194-
195-
To keep your access uninterrupted and secure across all apps, follow these steps to refresh OAuth tokens and extend your authentication:
196-
197-
1. Create a query module to call the token refresh endpoint provided by your authentication service. For OAuth services, ensure that you add the refresh token in the request payload to receive a new access token.
198-
199-
<dd>
200-
201-
*Example:*
202-
203-
```api
204-
https://api.example.io/api/user/token-auth/
205-
```
206-
207-
</dd>
208-
209-
2. Create a new JS module to refresh the token:
210-
211-
<dd>
212-
213-
*Example:*
214-
215-
```jsx
216-
export default {
217-
refreshAccessToken: () => {
218-
// Retrieving the refresh token from the appsmith store
219-
const refreshToken = appsmith.store.refreshtoken;
220-
221-
// Checking if a refresh token exists
222-
if (refreshToken) {
223-
// Executing the refresh API with the retrieved refresh token
224-
return refreshAPI.run({ refreshtoken: refreshToken })
225-
.then(newTokens => {
226-
// Updating the access token in the appsmith store
227-
storeValue('accesstoken', newTokens.accesstoken);
228-
229-
// Checking if a new refresh token is provided and updating it
230-
if (newTokens.refreshtoken) {
231-
storeValue('refreshtoken', newTokens.refresh_token);
232-
}
233-
})
234-
.catch(error => {
235-
console.log('Error refreshing token:', error);
236-
});
237-
}
238-
}
239-
};
240-
```
241-
242-
</dd>
243-
244-
3. Create a new query module to handle the generation of tokens when an authentication token expires.
245-
246-
<dd>
247-
248-
*Example:*
249-
250-
```js
251-
https://api.example.io/api/user/token-refresh/
252-
```
253-
254-
In the API configuration, provide the refresh token from the store in the request body:
255-
256-
```js
257-
{
258-
"refresh_token":{{appsmith.store.authRefreshToken}}
259-
}
260-
```
261-
262-
</dd>
263-
264-
4. Publish the package.
265-
266-
267-
Now you can use the JS module within your app to fetch details using query or refresh tokens on page load, ensuring continuous authentication access.
268-
269168

169+
### See also
270170

171+
- [Refresh access token](/docs/build-apps/how-to-guides/refresh-access-token.md) – Use this guide to implement a refresh token strategy and maintain session continuity for apps using short-lived access tokens. View guide
271172

173+
- [Module inputs](/packages/reference/ui-module#inputs) – Learn how to define and use inputs to customize module behavior such as dynamic app names, logos, or redirect paths. View reference
272174

175+
- [Module outputs](/packages/reference/ui-module#outputs) – Understand how to expose values like access tokens or login status from a module to the parent app. View reference
273176

274177

275178

website/docs/packages/overview.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ Inside a Code Package, you can create two types of modules—Query Modules and J
5252
### UI Packages
5353

5454

55-
UI Packages allow you to create and distribute reusable UI components across multiple applications in the same workspace.
55+
UI Packages are reusable bundles that group user interface components and related logic, which can be shared across applications within the same workspace. Inside a UI Package, you can create one or more UI Modules.
5656

57+
A **UI Module** combines widgets, queries, JavaScript, inputs, and outputs into a self-contained, configurable component. This allows you to build dynamic, branded UI blocks—like login forms or dashboards—that can be reused and customized across different applications without duplicating logic.
5758

58-
* **UI Modules:** Reusable widgets and UI components designed to enhance modularity and reusability in the user interface, streamlining the development and maintenance of consistent UI elements across multiple applications.
5959

6060

6161

343 KB
Loading

0 commit comments

Comments
 (0)