Skip to content

Modular logistics and supply chain operating system (LSOS)

implemented countdown component in two-fa controller template and succesfully sent sms to user

TemuulenBM5fb485154454
diff --git a/console/app/controllers/auth/two-fa.js b/console/app/controllers/auth/two-fa.js
index 3a05d6a2..ffe63caf 100644
--- a/console/app/controllers/auth/two-fa.js
+++ b/console/app/controllers/auth/two-fa.js
@@ -1,18 +1,135 @@
import Controller from '@ember/controller';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
+/**
+ * Controller responsible for handling two-factor authentication.
+ * @class AuthTwoFaController
+ * @extends Controller
+ */
export default class AuthTwoFaController extends Controller {
+ /**
+ * Tracked property for storing the verification token.
+ *
+ * @property {string} token
+ * @tracked
+ */
@tracked token;
- @tracked timerValue = 60;
+
+ /**
+ * Tracked property representing the remaining time in seconds for the countdown.
+ *
+ * @property {number} timeRemaining
+ * @tracked
+ * @default 60
+ */
+ @tracked timeRemaining = 60;
+
+ /**
+ * Tracked property representing the client token from the validated 2fa session.
+ *
+ * @property {number} clientToken
+ * @tracked
+ * @default null
+ */
+ @tracked clientToken;
+
+ /**
+ * Tracked property representing the date the 2fa session will expire
+ * @property {Date|null} twoFactorSessionExpiresAfter
+ * @tracked
+ * @default null
+ */
+ @tracked twoFactorSessionExpiresAfter;
+
+ /**
+ * Tracked property representing when the countdown is ready to start.
+ *
+ * @property {Boolean} countdownReady
+ * @tracked
+ * @default false
+ */
+ @tracked countdownReady = false;
+
+ /**
+ * Query parameters for the controller.
+ *
+ * @property {Array} queryParams
+ */
queryParams = ['token'];
- @action verifyCode() {
- // console.log('Verification code submitted!');
+
+ /**
+ * Action method for verifying the entered verification code.
+ *
+ * @method verifyCode
+ * @action
+ */
+ @action async verifyCode() {
+ try {
+ const { token, verificationCode } = this;
+
+ // Call the backend API to verify the entered verification code
+ const { authToken } = await this.fetch.post('two-fa/verify-code', {
+ token,
+ verificationCode,
+ });
+
+ // If verification is successful, transition to the desired route
+ this.notifications.success('Verification successful!');
+
+ // authenticate user
+ this.session.manuallyAuthenticate(authToken);
+ this.router.transitionTo('console');
+ } catch (error) {
+ // Handle verification failure
+ this.notifications.error('Verification failed. Please try again.');
+ }
}
- @action resendCode() {
- // console.log('Resending verification code...');
+ /**
+ * Action method for resending the verification code.
+ *
+ * @method resendCode
+ * @action
+ */
+ @action resendCode() {}
+
+ /**
+ * Converts a base64 encoded client token to a Date representing the expiration date.
+ *
+ * @method getExpirationDateFromClientToken
+ * @param {string} clientToken - Base64 encoded client token.
+ * @returns {Date|null} - Date representing the expiration date, or null if invalid.
+ */
+ getExpirationDateFromClientToken(clientToken) {
+ const decoder = new TextDecoder();
+ const binString = atob(clientToken);
+ const bytes = Uint8Array.from(binString, (m) => m.codePointAt(0));
+ const decodedString = decoder.decode(bytes);
+
+ if (typeof decodedString === 'string' && decodedString.includes('|')) {
+ const parts = decodedString.split('|');
+ const expiresAt = this.convertUtcToClientTime(parts[0]);
+
+ if (expiresAt instanceof Date) {
+ return expiresAt;
+ }
+ }
+
+ return null;
}
- @action resetTimer() {
+ /**
+ * Converts a UTC date-time string to client time zone.
+ *
+ * @method convertUtcToClientTime
+ * @param {string} utcDateTimeString - UTC date-time string.
+ * @returns {Date} - Date in client time zone.
+ */
+ convertUtcToClientTime(utcDateTimeString) {
+ const utcDate = new Date(utcDateTimeString);
+ const clientTimezoneOffset = new Date().getTimezoneOffset();
+ const clientDate = new Date(utcDate.getTime() - clientTimezoneOffset * 60 * 1000);
+
+ return clientDate;
}
}
diff --git a/console/app/router.js b/console/app/router.js
index 5b5de27d..562fb2cf 100644
--- a/console/app/router.js
+++ b/console/app/router.js
@@ -53,16 +53,16 @@ Router.map(function () {
path: 'developers'
});
- this.mount('@fleetbase/iam-engine', {
- as: 'iam',
- path: 'iam'
- });
-
this.mount('@fleetbase/fleetops-engine', {
as: 'fleet-ops',
path: 'fleet-ops'
});
+ this.mount('@fleetbase/iam-engine', {
+ as: 'iam',
+ path: 'iam'
+ });
+
this.mount('@fleetbase/storefront-engine', {
as: 'storefront',
path: 'storefront'
diff --git a/console/app/routes/auth/two-fa.js b/console/app/routes/auth/two-fa.js
index cde2e2a0..670994f1 100644
--- a/console/app/routes/auth/two-fa.js
+++ b/console/app/routes/auth/two-fa.js
@@ -36,6 +36,11 @@ export default class AuthTwoFaRoute extends Route {
*/
@tracked identity;
+ /**
+ * The client token from the validated 2fa session
+ */
+ @tracked clientToken;
+
/**
* Query parameters for the route.
*
@@ -46,6 +51,10 @@ export default class AuthTwoFaRoute extends Route {
refreshModel: false,
replace: true,
},
+ client_token: {
+ refreshModel: false,
+ replace: true
+ }
};
/**
@@ -69,9 +78,11 @@ export default class AuthTwoFaRoute extends Route {
return this.fetch
.post('two-fa/validate-session', { token, identity: two_fa_identity })
- .then(() => {
+ .then(({ client_token }) => {
// clear session data after validated 2fa session
- this.session.store.clear();
+ // this.session.store.clear();
+ // set the client token to the url
+ this.clientToken = client_token;
})
.catch((error) => {
this.notifications.serverError(error);
@@ -79,4 +90,18 @@ export default class AuthTwoFaRoute extends Route {
});
});
}
+
+ /**
+ * Sets up the controller, including client token and session expiration details.
+ *
+ * @param {Object} controller - The controller for the route.
+ */
+ setupController(controller) {
+ super.setupController(...arguments);
+
+ // set client token to controller
+ controller.clientToken = this.clientToken;
+ controller.twoFactorSessionExpiresAfter = controller.getExpirationDateFromClientToken(this.clientToken);
+ controller.countdownReady = true;
+ }
}
diff --git a/console/app/templates/auth/two-fa.hbs b/console/app/templates/auth/two-fa.hbs
index 7ebb20e6..e3e89578 100644
--- a/console/app/templates/auth/two-fa.hbs
+++ b/console/app/templates/auth/two-fa.hbs
@@ -33,7 +33,9 @@
/>
</div>
</div>
-
+ {{#if this.countdownReady}}
+ <Countdown @expiry={{this.twoFactorSessionExpiresAfter}} />
+ {{/if}}
<div class="text-right">
<a href="#" class="text-sm text-blue-500 hover:underline" {{on "click" this.resendCode}}>
Resend Code
@@ -44,9 +46,5 @@
<Button @icon="check-circle" @type="primary" @buttonType="submit" @text="Verify Code" @onClick={{this.verifyCode}} @isLoading={{this.isLoading}} />
<Button @buttonType="button" @text="Cancel" @onClick={{fn (transition-to "auth.login")}} @disabled={{this.isLoading}} />
</div>
-
- <div class="text-right">
- <span class="text-sm text-gray-500" id="countdown-timer">{{this.timerValue}}</span>
- </div>
</form>
</div>
\ No newline at end of file