Skip to content

Modular logistics and supply chain operating system (LSOS)

Succesfully implemented verify-code process

TemuulenBMea35a6f81f8b
diff --git a/console/app/controllers/auth/login.js b/console/app/controllers/auth/login.js
index 620fbd61..aa29518d 100644
--- a/console/app/controllers/auth/login.js
+++ b/console/app/controllers/auth/login.js
@@ -113,7 +113,7 @@ export default class AuthLoginController extends Controller {
let { twoFaSession, isTwoFaEnabled, isTwoFaValidated } = await this.session.checkForTwoFactor(identity);
if (isTwoFaEnabled && !isTwoFaValidated) {
- return this.session.store.persist({ two_fa_identity: identity }).then(() => {
+ return this.session.store.persist({ identity }).then(() => {
return this.router.transitionTo('auth.two-fa', { queryParams: { token: twoFaSession } });
});
}
diff --git a/console/app/controllers/auth/two-fa.js b/console/app/controllers/auth/two-fa.js
index ffe63caf..3008164a 100644
--- a/console/app/controllers/auth/two-fa.js
+++ b/console/app/controllers/auth/two-fa.js
@@ -1,12 +1,41 @@
import Controller from '@ember/controller';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
+import { inject as service } from '@ember/service';
/**
* Controller responsible for handling two-factor authentication.
* @class AuthTwoFaController
* @extends Controller
*/
export default class AuthTwoFaController extends Controller {
+ /**
+ * Router service.
+ *
+ * @var {Service}
+ */
+ @service router;
+
+ /**
+ * Fetch service for making HTTP requests.
+ *
+ * @var {Service}
+ */
+ @service fetch;
+
+ /**
+ * Notifications service for handling notifications.
+ *
+ * @var {Service}
+ */
+ @service notifications;
+
+ /**
+ * Session service for managing user sessions.
+ *
+ * @var {Service}
+ */
+ @service session;
+
/**
* Tracked property for storing the verification token.
*
@@ -15,6 +44,11 @@ export default class AuthTwoFaController extends Controller {
*/
@tracked token;
+ /**
+ * The current 2FA identity in memory
+ */
+ @tracked identity;
+
/**
* Tracked property representing the remaining time in seconds for the countdown.
*
@@ -55,7 +89,7 @@ export default class AuthTwoFaController extends Controller {
*
* @property {Array} queryParams
*/
- queryParams = ['token'];
+ queryParams = ['token', 'clientToken'];
/**
* Action method for verifying the entered verification code.
@@ -63,22 +97,28 @@ export default class AuthTwoFaController extends Controller {
* @method verifyCode
* @action
*/
- @action async verifyCode() {
+ @action async verifyCode(event) {
+ // prevent form default behaviour
+ event.preventDefault();
+
try {
- const { token, verificationCode } = this;
+ const { token, verificationCode, clientToken, identity } = this;
// Call the backend API to verify the entered verification code
const { authToken } = await this.fetch.post('two-fa/verify-code', {
token,
verificationCode,
+ clientToken,
+ identity,
});
// If verification is successful, transition to the desired route
this.notifications.success('Verification successful!');
// authenticate user
- this.session.manuallyAuthenticate(authToken);
- this.router.transitionTo('console');
+ return this.session.authenticate('authenticator:fleetbase', { authToken }).then((response) => {
+ return this.router.transitionTo('console');
+ });
} catch (error) {
// Handle verification failure
this.notifications.error('Verification failed. Please try again.');
@@ -91,7 +131,23 @@ export default class AuthTwoFaController extends Controller {
* @method resendCode
* @action
*/
- @action resendCode() {}
+ @action async resendCode() {
+ try {
+ const { identity, token, clientToken } = this;
+
+ // Call the backend API to resend the verification code
+ await this.fetch.post('two-fa/resend-code', {
+ identity,
+ token,
+ clientToken,
+ });
+
+ this.notifications.success('Verification code resent successfully.');
+ } catch (error) {
+ // Handle resend failure
+ this.notifications.error('Failed to resend verification code. Please try again.');
+ }
+ }
/**
* Converts a base64 encoded client token to a Date representing the expiration date.
diff --git a/console/app/router.js b/console/app/router.js
index 562fb2cf..90f3e424 100644
--- a/console/app/router.js
+++ b/console/app/router.js
@@ -53,11 +53,6 @@ Router.map(function () {
path: 'developers'
});
- this.mount('@fleetbase/fleetops-engine', {
- as: 'fleet-ops',
- path: 'fleet-ops'
- });
-
this.mount('@fleetbase/iam-engine', {
as: 'iam',
path: 'iam'
@@ -67,6 +62,11 @@ Router.map(function () {
as: 'storefront',
path: 'storefront'
});
+
+ this.mount('@fleetbase/fleetops-engine', {
+ as: 'fleet-ops',
+ path: 'fleet-ops'
+ });
});
this.route('install');
});
diff --git a/console/app/routes/auth/two-fa.js b/console/app/routes/auth/two-fa.js
index 670994f1..ed48eee8 100644
--- a/console/app/routes/auth/two-fa.js
+++ b/console/app/routes/auth/two-fa.js
@@ -1,6 +1,5 @@
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
-import { tracked } from '@glimmer/tracking';
export default class AuthTwoFaRoute extends Route {
/**
@@ -31,16 +30,6 @@ export default class AuthTwoFaRoute extends Route {
*/
@service session;
- /**
- * The current 2FA identity in memory
- */
- @tracked identity;
-
- /**
- * The client token from the validated 2fa session
- */
- @tracked clientToken;
-
/**
* Query parameters for the route.
*
@@ -51,10 +40,10 @@ export default class AuthTwoFaRoute extends Route {
refreshModel: false,
replace: true,
},
- client_token: {
+ clientToken: {
refreshModel: false,
- replace: true
- }
+ replace: true,
+ },
};
/**
@@ -65,24 +54,22 @@ export default class AuthTwoFaRoute extends Route {
*/
beforeModel(transition) {
// validate 2fa session with server
- const { token } = transition.to.queryParams;
+ let { token, clientToken } = transition.to.queryParams;
- return this.session.store.restore().then(({ two_fa_identity }) => {
- if (!two_fa_identity) {
- this.notifications.error('Unable to initiate 2FA.');
+ return this.session.store.restore().then(({ identity }) => {
+ if (!identity) {
+ this.notifications.error('Two Factor Authentication failed to start.');
return this.router.transitionTo('auth.login');
}
- // store to current route
- this.identity = two_fa_identity;
-
return this.fetch
- .post('two-fa/validate-session', { token, identity: two_fa_identity })
- .then(({ client_token }) => {
+ .post('two-fa/validate-session', { token, identity, clientToken })
+ .then(({ clientToken }) => {
// clear session data after validated 2fa session
- // this.session.store.clear();
- // set the client token to the url
- this.clientToken = client_token;
+ this.session.store.persist({
+ identity,
+ clientToken,
+ });
})
.catch((error) => {
this.notifications.serverError(error);
@@ -99,9 +86,11 @@ export default class AuthTwoFaRoute extends Route {
setupController(controller) {
super.setupController(...arguments);
- // set client token to controller
- controller.clientToken = this.clientToken;
- controller.twoFactorSessionExpiresAfter = controller.getExpirationDateFromClientToken(this.clientToken);
- controller.countdownReady = true;
+ this.session.store.restore().then(({ clientToken, identity }) => {
+ controller.clientToken = clientToken;
+ controller.identity = identity;
+ controller.twoFactorSessionExpiresAfter = controller.getExpirationDateFromClientToken(clientToken);
+ controller.countdownReady = true;
+ });
}
}
diff --git a/console/app/templates/auth/login.hbs b/console/app/templates/auth/login.hbs
index 3a5c1c7e..7a43053f 100644
--- a/console/app/templates/auth/login.hbs
+++ b/console/app/templates/auth/login.hbs
@@ -25,19 +25,6 @@
<form class="mt-8" {{on "submit" this.login}}>
<input type="hidden" name="remember" value="true" />
<div class="rounded-md shadow-sm">
- {{!-- <div>
- <Input
- @value={{this.email}}
- aria-label="Email address"
- name="email"
- @type="email"
- autocomplete="username"
- required
- class="relative block w-full px-3 py-2 text-gray-900 placeholder-gray-500 border border-gray-300 rounded-none appearance-none rounded-t-md focus:outline-none focus:shadow-outline-blue focus:border-blue-300 focus:z-10 sm:text-sm sm:leading-5 dark:text-white dark:bg-gray-700 dark:border-gray-900"
- placeholder="Email address"
- disabled={{this.isLoading}}
- />
- </div> --}}
<div>
<Input
@value={{this.identity}}
diff --git a/console/app/templates/auth/two-fa.hbs b/console/app/templates/auth/two-fa.hbs
index e3e89578..afe3e0a1 100644
--- a/console/app/templates/auth/two-fa.hbs
+++ b/console/app/templates/auth/two-fa.hbs
@@ -34,7 +34,7 @@
</div>
</div>
{{#if this.countdownReady}}
- <Countdown @expiry={{this.twoFactorSessionExpiresAfter}} />
+ <Countdown @expiry={{this.twoFactorSessionExpiresAfter}} />
{{/if}}
<div class="text-right">
<a href="#" class="text-sm text-blue-500 hover:underline" {{on "click" this.resendCode}}>
@@ -43,7 +43,7 @@
</div>
<div class="flex flex-row space-x-2">
- <Button @icon="check-circle" @type="primary" @buttonType="submit" @text="Verify Code" @onClick={{this.verifyCode}} @isLoading={{this.isLoading}} />
+ <Button @icon="check-circle" @type="primary" @buttonType="submit" @text="Verify Code" @isLoading={{this.isLoading}} />
<Button @buttonType="button" @text="Cancel" @onClick={{fn (transition-to "auth.login")}} @disabled={{this.isLoading}} />
</div>
</form>