Modular logistics and supply chain operating system (LSOS)
JavaScript driver-appfleet-managementfreight-managementinfrastructureinventory-managementlast-mile-deliverylogisticsondemandopen-sourceorder-managementroute-optimizationsupply-chain-managementtracking-and-monitoringtransport-management-systemtransportationwarehouse-management
feat: add the verification link page and register the email change route
Ronald A. Richardson1bad938b76bf
diff --git a/console/app/controllers/auth/verify-contact.js b/console/app/controllers/auth/verify-contact.jsnew file mode 100644index 00000000..4accdf62--- /dev/null+++ b/console/app/controllers/auth/verify-contact.js@@ -0,0 +1,64 @@+import Controller from '@ember/controller';+import { inject as service } from '@ember/service';+import { tracked } from '@glimmer/tracking';+import { action } from '@ember/object';+import { task } from 'ember-concurrency';++export default class AuthVerifyContactController extends Controller {+ @service fetch;+ @service notifications;+ @service router;+ @service intl;+ @service session;++ /**+ * The verification code query param.+ *+ * @memberof AuthVerifyContactController+ */+ @tracked code;++ /**+ * Whether the email address or phone number has been verified.+ *+ * @memberof AuthVerifyContactController+ */+ @tracked verified = false;++ /**+ * Query parameters.+ *+ * @memberof AuthVerifyContactController+ */+ queryParams = ['code'];++ /**+ * Confirms the email address or phone number from the link.+ *+ * @memberof AuthVerifyContactController+ */+ @task *verify(event) {+ event?.preventDefault();++ const { code } = this;+ const { id } = this.model;++ try {+ yield this.fetch.post('auth/confirm-contact-verification', { link: id, code });+ } catch (error) {+ return this.notifications.serverError(error);+ }++ this.verified = true;+ this.notifications.success(this.intl.t(this.model.channel === 'phone' ? 'auth.verify-contact.phone-verified' : 'auth.verify-contact.email-verified'));+ }++ /**+ * Continue to the console, or to sign in.+ *+ * @memberof AuthVerifyContactController+ */+ @action continue() {+ return this.router.transitionTo(this.session.isAuthenticated ? 'console' : 'auth.login');+ }+}diff --git a/console/app/router.js b/console/app/router.jsindex 53ee0fc5..a734dc43 100644--- a/console/app/router.js+++ b/console/app/router.js@@ -18,6 +18,8 @@ Router.map(function () {this.route('reset-password', { path: '/reset-password/:id' });this.route('two-fa');this.route('verification');+ this.route('confirm-email-change', { path: '/confirm-email-change/:id' });+ this.route('verify-contact', { path: '/verify-contact/:id' });this.route('portal-login', { path: '/portal' });});this.route('invite', { path: 'join' }, function () {diff --git a/console/app/routes/auth/verify-contact.js b/console/app/routes/auth/verify-contact.jsnew file mode 100644index 00000000..d7ad872c--- /dev/null+++ b/console/app/routes/auth/verify-contact.js@@ -0,0 +1,43 @@+import Route from '@ember/routing/route';+import { inject as service } from '@ember/service';+import { action } from '@ember/object';+import removeBootLoader from '../../utils/remove-boot-loader';++/**+ * Landing page for the verification link an administrator sends from IAM. The+ * link confirms the user's email address or phone number without signing in.+ */+export default class AuthVerifyContactRoute extends Route {+ @service store;+ @service fetch;+ @service router;+ @service notifications;+ @service intl;++ @action activate() {+ removeBootLoader();+ }++ async model({ id }) {+ const [email, phone] = await Promise.all([+ this.fetch.get('auth/validate-verification', { id, for: 'email_verification' }),+ this.fetch.get('auth/validate-verification', { id, for: 'phone_verification' }),+ ]);++ return {+ id,+ is_valid: Boolean(email?.is_valid || phone?.is_valid),+ channel: phone?.is_valid ? 'phone' : 'email',+ };+ }++ async setupController(controller, model) {+ super.setupController(...arguments);+ if (model.is_valid === false) {+ this.notifications.warning(this.intl.t('auth.verify-contact.invalid-link'));+ return this.router.transitionTo('auth.login');+ }++ controller.brand = await this.store.findRecord('brand', 1);+ }+}diff --git a/console/app/templates/auth/verify-contact.hbs b/console/app/templates/auth/verify-contact.hbsnew file mode 100644index 00000000..e2a35458--- /dev/null+++ b/console/app/templates/auth/verify-contact.hbs@@ -0,0 +1,42 @@+<div class="bg-white dark:bg-gray-800 py-8 px-4 shadow rounded-lg">+ <div class="mb-4">+ <LinkTo @route="console" class="flex items-center justify-center">+ <LogoIcon @brand={{this.brand}} @size="12" class="rounded-md" />+ </LinkTo>+ <h2 class="text-center text-lg font-extrabold text-gray-900 dark:text-white truncate">+ {{if (eq this.model.channel "phone") (t "auth.verify-contact.phone-title") (t "auth.verify-contact.email-title")}}+ </h2>+ </div>++ {{#if this.verified}}+ <InfoBlock @type="success" @icon="circle-check" @iconSize="lg">+ {{if (eq this.model.channel "phone") (t "auth.verify-contact.phone-verified") (t "auth.verify-contact.email-verified")}}+ </InfoBlock>+ <div class="mt-8 flex">+ <Button @size="lg" @type="primary" @icon="arrow-right" @text={{t "auth.verify-contact.continue-button"}} @onClick={{this.continue}} />+ </div>+ {{else}}+ <InfoBlock @type="info" @icon={{if (eq this.model.channel "phone") "mobile-screen" "envelope-circle-check"}} @iconSize="lg">+ {{if (eq this.model.channel "phone") (t "auth.verify-contact.phone-message") (t "auth.verify-contact.email-message")}}+ </InfoBlock>++ <form class="mt-8 space-y-6" {{on "submit" (perform this.verify)}}>+ {{#unless this.code}}+ <InputGroup @name={{t "auth.verify-contact.form.code.label"}} @value={{this.code}} @inputClass="form-input-lg" @helpText={{t "auth.verify-contact.form.code.help-text"}} />+ {{/unless}}++ <div class="flex space-x-2">+ <Button+ @icon="check"+ @size="lg"+ @type="primary"+ @buttonType="submit"+ @text={{t "auth.verify-contact.form.submit-button"}}+ @onClick={{perform this.verify}}+ @isLoading={{not this.verify.isIdle}}+ />+ <Button @size="lg" @buttonType="button" @text={{t "auth.verify-contact.form.back-button"}} @onClick={{fn (transition-to "auth.login")}} @disabled={{not this.verify.isIdle}} />+ </div>+ </form>+ {{/if}}+</div>diff --git a/console/tests/unit/controllers/auth/verify-contact-test.js b/console/tests/unit/controllers/auth/verify-contact-test.jsnew file mode 100644index 00000000..85773a04--- /dev/null+++ b/console/tests/unit/controllers/auth/verify-contact-test.js@@ -0,0 +1,107 @@+import { module, test } from 'qunit';+import { setupTest } from '@fleetbase/console/tests/helpers';+import Service from '@ember/service';++module('Unit | Controller | auth/verify-contact', function (hooks) {+ setupTest(hooks);++ hooks.beforeEach(function () {+ this.posted = [];+ this.postRejectsWith = null;+ this.isAuthenticated = false;+ const context = this;++ class FetchStub extends Service {+ post(path, payload) {+ context.posted.push({ path, payload });+ return context.postRejectsWith ? Promise.reject(context.postRejectsWith) : Promise.resolve({ status: 'ok' });+ }+ }+ class NotificationsStub extends Service {+ successes = [];+ serverErrors = [];+ success(message) {+ this.successes.push(message);+ }+ serverError(error) {+ this.serverErrors.push(error);+ return 'reported';+ }+ }+ class IntlStub extends Service {+ t(key) {+ return key;+ }+ }+ class SessionStub extends Service {+ get isAuthenticated() {+ return context.isAuthenticated;+ }+ }++ this.owner.register('service:fetch', FetchStub);+ this.owner.register('service:notifications', NotificationsStub);+ this.owner.register('service:intl', IntlStub);+ this.owner.register('service:session', SessionStub);++ this.build = (channel = 'email') => {+ const controller = this.owner.lookup('controller:auth/verify-contact');+ controller.model = { id: 'link_1', channel };+ controller.code = '123456';+ controller.verified = false;++ this.transitions = [];+ Object.defineProperty(controller.router, 'transitionTo', {+ configurable: true,+ value: (routeName) => {+ this.transitions.push(routeName);+ return routeName;+ },+ });++ return controller;+ };+ this.notifications = () => this.owner.lookup('service:notifications');+ });++ test('verifying posts the link and code and shows the verified state', async function (assert) {+ const controller = this.build('phone');+ let prevented = false;++ await controller.verify.perform({ preventDefault: () => (prevented = true) });++ assert.true(prevented);+ assert.deepEqual(this.posted, [{ path: 'auth/confirm-contact-verification', payload: { link: 'link_1', code: '123456' } }]);+ assert.true(controller.verified);+ assert.deepEqual(this.notifications().successes, ['auth.verify-contact.phone-verified']);+ });++ test('an email link reports the email as verified', async function (assert) {+ const controller = this.build('email');++ await controller.verify.perform();++ assert.deepEqual(this.notifications().successes, ['auth.verify-contact.email-verified']);+ });++ test('a rejected verification is reported and the page stays unverified', async function (assert) {+ const failure = new Error('link expired');+ this.postRejectsWith = failure;+ const controller = this.build();++ await controller.verify.perform();++ assert.deepEqual(this.notifications().serverErrors, [failure]);+ assert.false(controller.verified);+ });++ test('continue goes to the console when signed in and to login otherwise', function (assert) {+ const controller = this.build();++ controller.continue();+ this.isAuthenticated = true;+ controller.continue();++ assert.deepEqual(this.transitions, ['auth.login', 'console']);+ });+});diff --git a/console/tests/unit/routes/auth/verify-contact-test.js b/console/tests/unit/routes/auth/verify-contact-test.jsnew file mode 100644index 00000000..cb603699--- /dev/null+++ b/console/tests/unit/routes/auth/verify-contact-test.js@@ -0,0 +1,78 @@+import { module, test } from 'qunit';+import { setupTest } from '@fleetbase/console/tests/helpers';+import Service from '@ember/service';++class NotificationsStub extends Service {+ warnings = [];+ warning(message) {+ this.warnings.push(message);+ }+}++module('Unit | Route | auth/verify-contact', function (hooks) {+ setupTest(hooks);++ hooks.beforeEach(function () {+ this.validFor = null;+ const context = this;++ class FetchStub extends Service {+ requests = [];+ get(path, params) {+ this.requests.push({ path, params });+ return Promise.resolve({ is_valid: params.for === context.validFor, id: params.id });+ }+ }+ this.owner.register('service:fetch', FetchStub);+ this.owner.register('service:notifications', NotificationsStub);+ });++ test('model validates the link as an email or phone verification', async function (assert) {+ const route = this.owner.lookup('route:auth/verify-contact');++ this.validFor = 'phone_verification';+ const phone = await route.model({ id: 'code_1' });+ this.validFor = 'email_verification';+ const email = await route.model({ id: 'code_1' });+ this.validFor = null;+ const invalid = await route.model({ id: 'code_1' });++ assert.deepEqual(+ route.fetch.requests.slice(0, 2).map((request) => request.params),+ [+ { id: 'code_1', for: 'email_verification' },+ { id: 'code_1', for: 'phone_verification' },+ ]+ );+ assert.deepEqual(phone, { id: 'code_1', is_valid: true, channel: 'phone' });+ assert.deepEqual(email, { id: 'code_1', is_valid: true, channel: 'email' });+ assert.false(invalid.is_valid);+ });++ test('setupController warns and redirects to login when the link is invalid', async function (assert) {+ const route = this.owner.lookup('route:auth/verify-contact');+ let transitioned;+ route.router.transitionTo = (name) => (transitioned = name);++ const controller = {};+ await route.setupController(controller, { is_valid: false });++ assert.strictEqual(transitioned, 'auth.login');+ assert.strictEqual(route.notifications.warnings.length, 1, 'the user is warned');+ assert.notOk(controller.brand, 'no brand is loaded for an invalid link');+ });++ test('setupController loads the brand when the link is valid', async function (assert) {+ const route = this.owner.lookup('route:auth/verify-contact');+ route.store.findRecord = (type, id) => Promise.resolve(`${type}:${id}`);++ let transitioned;+ route.router.transitionTo = (name) => (transitioned = name);++ const controller = {};+ await route.setupController(controller, { is_valid: true });++ assert.strictEqual(controller.brand, 'brand:1');+ assert.strictEqual(transitioned, undefined, 'no redirect for a valid link');+ });+});diff --git a/console/translations/en-us.yaml b/console/translations/en-us.yamlindex fb5ea141..66d9e35c 100644--- a/console/translations/en-us.yaml+++ b/console/translations/en-us.yaml@@ -681,6 +681,21 @@ auth:help-text: The confirmation code from your email.submit-button: Confirm Email Changeback-button: Back+ verify-contact:+ invalid-link: This verification link is invalid or has expired.+ email-title: Verify your email address+ phone-title: Verify your phone number+ email-message: Confirm that this email address belongs to you.+ phone-message: Confirm that this phone number belongs to you.+ email-verified: Your email address is verified.+ phone-verified: Your phone number is verified.+ continue-button: Continue+ form:+ code:+ label: Verification code+ help-text: The verification code from your email or text message.+ submit-button: Verify+ back-button: Backconsole:create-or-join-organization: