Skip to content

Modular logistics and supply chain operating system (LSOS)

Created 2fa validate session

TemuulenBMe348b026114f
diff --git a/console/app/controllers/auth/login.js b/console/app/controllers/auth/login.js
index d2b535c5..620fbd61 100644
--- a/console/app/controllers/auth/login.js
+++ b/console/app/controllers/auth/login.js
@@ -33,6 +33,13 @@ export default class AuthLoginController extends Controller {
*/
@service session;
+ /**
+ * Inject the `router` service
+ *
+ * @var {Service}
+ */
+ @service router;
+
/**
* Whether or not to remember the users session
*
@@ -103,10 +110,12 @@ export default class AuthLoginController extends Controller {
// send request to check for 2fa
try {
- let { twoFaSession, twoFaEnabled, isTwoFaValidated } = await this.session.checkForTwoFactor(identity);
+ let { twoFaSession, isTwoFaEnabled, isTwoFaValidated } = await this.session.checkForTwoFactor(identity);
- if (twoFaEnabled && !isTwoFaValidated) {
- return this.router.transitionTo('auth.two-fa', { token: twoFaSession });
+ if (isTwoFaEnabled && !isTwoFaValidated) {
+ return this.session.store.persist({ two_fa_identity: identity }).then(() => {
+ return this.router.transitionTo('auth.two-fa', { queryParams: { token: twoFaSession } });
+ });
}
} catch (error) {
return this.notifications.serverError(error);
@@ -124,31 +133,6 @@ export default class AuthLoginController extends Controller {
}
}
- /**
- * Authenticate the user
- *
- * @void
- */
- // @action async login(event) {
- // // firefox patch
- // event.preventDefault();
- // // get user credentials
- // const { email, password, rememberMe } = this;
- // // start loader
- // this.set('isLoading', true);
- // // set where to redirect on login
- // this.setRedirect();
- // try {
- // await this.session.authenticate('authenticator:fleetbase', { email, password }, rememberMe);
- // } catch (error) {
- // this.failedAttempts++;
- // return this.failure(error);
- // }
- // if (this.session.isAuthenticated) {
- // this.success();
- // }
- // }
-
/**
* Transition user to onboarding screen
*/
diff --git a/console/app/controllers/auth/two-fa.js b/console/app/controllers/auth/two-fa.js
index f0219fdf..3a05d6a2 100644
--- a/console/app/controllers/auth/two-fa.js
+++ b/console/app/controllers/auth/two-fa.js
@@ -1,6 +1,10 @@
import Controller from '@ember/controller';
+import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
export default class AuthTwoFaController extends Controller {
+ @tracked token;
+ @tracked timerValue = 60;
+ queryParams = ['token'];
@action verifyCode() {
// console.log('Verification code submitted!');
}
@@ -8,4 +12,7 @@ export default class AuthTwoFaController extends Controller {
@action resendCode() {
// console.log('Resending verification code...');
}
+
+ @action resetTimer() {
+ }
}
diff --git a/console/app/router.js b/console/app/router.js
index 562fb2cf..5b5de27d 100644
--- a/console/app/router.js
+++ b/console/app/router.js
@@ -53,16 +53,16 @@ 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'
});
+ this.mount('@fleetbase/fleetops-engine', {
+ as: 'fleet-ops',
+ path: 'fleet-ops'
+ });
+
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 b73111d1..cde2e2a0 100644
--- a/console/app/routes/auth/two-fa.js
+++ b/console/app/routes/auth/two-fa.js
@@ -1,42 +1,82 @@
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
+import { tracked } from '@glimmer/tracking';
export default class AuthTwoFaRoute extends Route {
+ /**
+ * Fetch service for making HTTP requests.
+ *
+ * @var {Service}
+ */
@service fetch;
+
+ /**
+ * Notifications service for handling notifications.
+ *
+ * @var {Service}
+ */
@service notifications;
+
+ /**
+ * Router service.
+ *
+ * @var {Service}
+ */
+ @service router;
+
+ /**
+ * Session service for managing user sessions.
+ *
+ * @var {Service}
+ */
@service session;
+ /**
+ * The current 2FA identity in memory
+ */
+ @tracked identity;
+
+ /**
+ * Query parameters for the route.
+ *
+ * @var {Object}
+ */
queryParams = {
token: {
refreshModel: false,
+ replace: true,
},
};
- // beforeModel(transition) {
- // // validate 2fa session with server
- // const { token } = transition.params;
-
- // return this.fetch.post('two-fa/validate-session', { token }).catch((error) => {
- // this.notifications.serverError(error);
- // return this.redirect('auth.login');
- // });
- // }
-
- async beforeModel(transition) {
- // Validate 2fa session with the server
+ /**
+ * Executes before the model is loaded, used for validating 2FA session with the server.
+ *
+ * @param {Object} transition - The transition object representing the route transition.
+ * @return {Promise} A promise that resolves if the 2FA session is valid, and rejects with an error otherwise.
+ */
+ beforeModel(transition) {
+ // validate 2fa session with server
const { token } = transition.to.queryParams;
- try {
- // Make a request to validate the 2FA session
- await this.fetch.post('two-fa/validate-session', { token });
+ return this.session.store.restore().then(({ two_fa_identity }) => {
+ if (!two_fa_identity) {
+ this.notifications.error('Unable to initiate 2FA.');
+ return this.router.transitionTo('auth.login');
+ }
- // If the session is valid, do nothing, and the transition will continue
- } catch (error) {
- // If the session is not valid, show an error notification
- this.notifications.serverError(error);
+ // store to current route
+ this.identity = two_fa_identity;
- // Redirect to the login route
- this.transitionTo('auth.login');
- }
+ return this.fetch
+ .post('two-fa/validate-session', { token, identity: two_fa_identity })
+ .then(() => {
+ // clear session data after validated 2fa session
+ this.session.store.clear();
+ })
+ .catch((error) => {
+ this.notifications.serverError(error);
+ return this.router.transitionTo('auth.login');
+ });
+ });
}
}
diff --git a/console/app/templates/auth/two-fa.hbs b/console/app/templates/auth/two-fa.hbs
index dea0ca48..7ebb20e6 100644
--- a/console/app/templates/auth/two-fa.hbs
+++ b/console/app/templates/auth/two-fa.hbs
@@ -44,5 +44,9 @@
<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