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
improved and enforced email verification flows
Ronald A. Richardsonbb1444dc3114
diff --git a/console/app/controllers/auth/login.js b/console/app/controllers/auth/login.jsindex 4a98843b..ab85465e 100644--- a/console/app/controllers/auth/login.js+++ b/console/app/controllers/auth/login.js@@ -40,6 +40,13 @@ export default class AuthLoginController extends Controller {*/@service router;+ /**+ * Inject the `fetch` service+ *+ * @var {Service}+ */+ @service fetch;+/*** Whether or not to remember the users session*@@ -108,6 +115,16 @@ export default class AuthLoginController extends Controller {// get user credentialsconst { email, password, rememberMe } = this;+ // If no password error+ if (!email) {+ return this.notifications.warning('Did you forget to enter your email?');+ }++ // If no password error+ if (!password) {+ return this.notifications.warning('Did you forget to enter your password?');+ }+// start loaderthis.set('isLoading', true);@@ -119,6 +136,11 @@ export default class AuthLoginController extends Controller {} catch (error) {this.failedAttempts++;+ // Handle unverified user+ if (error.toString().includes('not verified')) {+ return this.sendUserForEmailVerification(email);+ }+return this.failure(error);}@@ -145,6 +167,30 @@ export default class AuthLoginController extends Controller {});}+ /**+ * Creates an email verification session and transitions user to verification route.+ *+ * @param {String} email+ * @return {Promise<Transition>}+ * @memberof AuthLoginController+ */+ sendUserForEmailVerification(email) {+ return this.fetch.post('auth/create-verification-session', { email, send: true }).then(({ token }) => {+ return this.session.store.persist({ email }).then(() => {+ this.notifications.warning('Your account needs to be verified to proceed.');+ return this.router+ .transitionTo('auth.verification', {+ queryParams: {+ token,+ },+ })+ .then(() => {+ this.reset('error');+ });+ });+ });+ }+/*** Sets correct route to send user to after login.*diff --git a/console/app/controllers/auth/verification.js b/console/app/controllers/auth/verification.jsnew file mode 100644index 00000000..aef5943a--- /dev/null+++ b/console/app/controllers/auth/verification.js@@ -0,0 +1,220 @@+import Controller from '@ember/controller';+import { inject as service } from '@ember/service';+import { tracked } from '@glimmer/tracking';+import { action } from '@ember/object';+import { later } from '@ember/runloop';+import { not } from '@ember/object/computed';++export default class AuthVerificationController extends Controller {+ /**+ * Inject the `fetch` service+ *+ * @memberof OnboardIndexController+ */+ @service fetch;++ /**+ * Inject the `notifications` service+ *+ * @memberof OnboardIndexController+ */+ @service notifications;++ /**+ * Inject the `modalsManager` service+ *+ * @memberof OnboardIndexController+ */+ @service modalsManager;++ /**+ * Inject the `currentUser` service+ *+ * @memberof OnboardIndexController+ */+ @service currentUser;++ /**+ * Inject the `router` service+ *+ * @memberof OnboardIndexController+ */+ @service router;++ /**+ * Inject the `session` service+ *+ * @memberof OnboardIndexController+ */+ @service session;++ /**+ * The session paramerer.+ *+ * @memberof OnboardVerifyEmailController+ */+ @tracked hello;++ /**+ * The token paramerer.+ *+ * @memberof OnboardVerifyEmailController+ */+ @tracked token;++ /**+ * The loading state of the verification request.+ *+ * @memberof OnboardVerifyEmailController+ */+ @tracked isLoading = false;++ /**+ * Validation state tracker.+ *+ * @memberof OnboardVerifyEmailController+ */+ @tracked isReadyToSubmit = false;++ /**+ * The request timeout to trigger alternative verification options.+ *+ * @memberof OnboardVerifyEmailController+ */+ @tracked waitTimeout = 800 * 60 * 2;++ /**+ * Determines if Fleetbase is still awaiting verification after a certain time.+ *+ * @memberof OnboardVerifyEmailController+ */+ @tracked stillWaiting = false;++ /**+ * the input code.+ *+ * @memberof OnboardVerifyEmailController+ */+ @tracked code;++ /**+ * The query param for the session token.+ *+ * @memberof OnboardVerifyEmailController+ */+ @tracked queryParams = ['hello', 'token'];++ /**+ * The boolean opposite of `isReadyToSubmit`+ *+ * @memberof OnboardVerifyEmailController+ */+ @not('isReadyToSubmit') isNotReadyToSubmit;++ /**+ * Creates an instance of OnboardVerifyEmailController.+ * @memberof OnboardVerifyEmailController+ */+ constructor() {+ super(...arguments);++ later(+ this,+ () => {+ this.stillWaiting = true;+ },+ this.waitTimeout+ );+ }++ /**+ * Validates the input+ *+ * @param {InputEvent} { target: { value } }+ * @memberof OnboardVerifyEmailController+ */+ @action validateInput({ target: { value } }) {+ if (value.length > 5) {+ this.isReadyToSubmit = true;+ } else {+ this.isReadyToSubmit = false;+ }+ }++ /**+ * Submits to verify code.+ *+ * @return {Promise}+ * @memberof OnboardVerifyEmailController+ */+ @action verifyCode() {+ const { token, code, email } = this;++ this.isLoading = true;++ return this.fetch+ .post('auth/verify-email', { token, code, email, authenticate: true })+ .then(({ status, token }) => {+ if (status === 'ok') {+ this.notifications.success('Email successfully verified!');++ if (token) {+ this.notifications.info('Welcome to Fleetbase!');+ this.session.manuallyAuthenticate(token);++ return this.router.transitionTo('console');+ }++ return this.router.transitionTo('auth.login');+ }+ })+ .catch((error) => {+ this.notifications.serverError(error);+ })+ .finally(() => {+ this.isLoading = false;+ });+ }++ /**+ * Action to resend verification code by SMS.+ *+ * @memberof OnboardVerifyEmailController+ */+ @action resendBySms() {+ this.modalsManager.show('modals/verify-by-sms', {+ title: 'Verify Account by Phone',+ acceptButtonText: 'Send',+ phone: this.currentUser.phone,+ confirm: (modal) => {+ modal.startLoading();+ const phone = modal.getOption('phone');++ return this.fetch.post('onboard/send-verification-sms', { phone }).then(() => {+ this.notifications.success('Verification code SMS sent!');+ });+ },+ });+ }++ /**+ * Action to resend verification code by email.+ *+ * @memberof OnboardVerifyEmailController+ */+ @action resendEmail() {+ this.modalsManager.show('modals/resend-verification-email', {+ title: 'Resend Verification Code',+ acceptButtonText: 'Send',+ email: this.currentUser.email,+ confirm: (modal) => {+ modal.startLoading();+ const email = modal.getOption('email');++ return this.fetch.post('onboard/send-verification-email', { email }).then(() => {+ this.notifications.success('Verification code email sent!');+ });+ },+ });+ }+}diff --git a/console/app/controllers/onboard/index.js b/console/app/controllers/onboard/index.jsindex 77ef988b..0884c476 100644--- a/console/app/controllers/onboard/index.js+++ b/console/app/controllers/onboard/index.js@@ -124,17 +124,18 @@ export default class OnboardIndexController extends Controller {return this.fetch.post('onboard/create-account', input)- .then((response) => {- if (response.status === 'success') {- this.session.isOnboarding().manuallyAuthenticate(response.token);+ .then(({ status, skipVerification, token, session }) => {+ if (status === 'success') {+ if (skipVerification === true && token) {+ // only manually authenticate if skip verification+ this.session.isOnboarding().manuallyAuthenticate(token);- if (response.skipVerification === true) {return this.router.transitionTo('console').then(() => {this.notifications.success('Welcome to Fleetbase!');});}- return this.router.transitionTo('onboard.verify-email', { queryParams: { hello: response.session } });+ return this.router.transitionTo('onboard.verify-email', { queryParams: { hello: session } });}}).catch((error) => {diff --git a/console/app/controllers/onboard/verify-email.js b/console/app/controllers/onboard/verify-email.jsindex 63911089..f9c23788 100644--- a/console/app/controllers/onboard/verify-email.js+++ b/console/app/controllers/onboard/verify-email.js@@ -1,132 +1,7 @@-import Controller from '@ember/controller';-import { inject as service } from '@ember/service';-import { tracked } from '@glimmer/tracking';+import AuthVerificationController from '../auth/verification';import { action } from '@ember/object';-import { later } from '@ember/runloop';-import { not } from '@ember/object/computed';--export default class OnboardVerifyEmailController extends Controller {- /**- * Inject the `fetch` service- *- * @memberof OnboardIndexController- */- @service fetch;-- /**- * Inject the `notifications` service- *- * @memberof OnboardIndexController- */- @service notifications;-- /**- * Inject the `modalsManager` service- *- * @memberof OnboardIndexController- */- @service modalsManager;-- /**- * Inject the `currentUser` service- *- * @memberof OnboardIndexController- */- @service currentUser;-- /**- * Inject the `router` service- *- * @memberof OnboardIndexController- */- @service router;-- /**- * The session paramerer.- *- * @memberof OnboardVerifyEmailController- */- @tracked hello;-- /**- * The loading state of the verification request.- *- * @memberof OnboardVerifyEmailController- */- @tracked isLoading = false;-- /**- * Validation state tracker.- *- * @memberof OnboardVerifyEmailController- */- @tracked isReadyToSubmit = false;-- /**- * The request timeout to trigger alternative verification options.- *- * @memberof OnboardVerifyEmailController- */- @tracked waitTimeout = 800 * 60 * 2;-- /**- * Determines if Fleetbase is still awaiting verification after a certain time.- *- * @memberof OnboardVerifyEmailController- */- @tracked stillWaiting = false;-- /**- * the input code.- *- * @memberof OnboardVerifyEmailController- */- @tracked code;-- /**- * The query param for the session token.- *- * @memberof OnboardVerifyEmailController- */- @tracked queryParams = ['hello'];-- /**- * The boolean opposite of `isReadyToSubmit`- *- * @memberof OnboardVerifyEmailController- */- @not('isReadyToSubmit') isNotReadyToSubmit;-- /**- * Creates an instance of OnboardVerifyEmailController.- * @memberof OnboardVerifyEmailController- */- constructor() {- super(...arguments);-- later(- this,- () => {- this.stillWaiting = true;- },- this.waitTimeout- );- }-- /**- * Validates the input- *- * @param {InputEvent} { target: { value } }- * @memberof OnboardVerifyEmailController- */- @action validateInput({ target: { value } }) {- if (value.length > 5) {- this.isReadyToSubmit = true;- } else {- this.isReadyToSubmit = false;- }- }+export default class OnboardVerifyEmailController extends AuthVerificationController {/*** Submits to verify code.*@@ -134,19 +9,24 @@ export default class OnboardVerifyEmailController extends Controller {* @memberof OnboardVerifyEmailController*/@action verifyCode() {- const session = this.hello;- const code = this.code;+ const { hello, code } = this;this.isLoading = true;return this.fetch- .post('onboard/verify-email', { session, code })- .then((response) => {- if (response.status === 'success') {+ .post('onboard/verify-email', { session: hello, code })+ .then(({ status, token }) => {+ if (status === 'ok') {this.notifications.success('Email successfully verified!');- this.notifications.info('Welcome to Fleetbase!');- return this.router.transitionTo('console');+ if (token) {+ this.notifications.info('Welcome to Fleetbase!');+ this.session.manuallyAuthenticate(token);++ return this.router.transitionTo('console');+ }++ return this.router.transitionTo('auth.login');}}).catch((error) => {@@ -156,46 +36,4 @@ export default class OnboardVerifyEmailController extends Controller {this.isLoading = false;});}-- /**- * Action to resend verification code by SMS.- *- * @memberof OnboardVerifyEmailController- */- @action resendBySms() {- this.modalsManager.show('modals/verify-by-sms', {- title: 'Verify Account by Phone',- acceptButtonText: 'Send',- phone: this.currentUser.phone,- confirm: (modal) => {- modal.startLoading();- const phone = modal.getOption('phone');-- return this.fetch.post('onboard/send-verification-sms', { phone }).then(() => {- this.notifications.success('Verification code SMS sent!');- });- },- });- }-- /**- * Action to resend verification code by email.- *- * @memberof OnboardVerifyEmailController- */- @action resendEmail() {- this.modalsManager.show('modals/resend-verification-email', {- title: 'Resend Verification Code',- acceptButtonText: 'Send',- email: this.currentUser.email,- confirm: (modal) => {- modal.startLoading();- const email = modal.getOption('email');-- return this.fetch.post('onboard/send-verification-email', { email }).then(() => {- this.notifications.success('Verification code email sent!');- });- },- });- }}diff --git a/console/app/routes/auth/verification.js b/console/app/routes/auth/verification.jsnew file mode 100644index 00000000..99570cea--- /dev/null+++ b/console/app/routes/auth/verification.js@@ -0,0 +1,33 @@+import Route from '@ember/routing/route';+import { inject as service } from '@ember/service';++export default class AuthVerificationRoute extends Route {+ @service session;+ @service fetch;+ @service router;++ queryParams = {+ token: {+ refreshModel: false,+ replace: true,+ },+ };++ beforeModel(transition) {+ let { token } = transition.to.queryParams;++ return this.session.store.restore().then(({ email }) => {+ return this.fetch.post('auth/validate-verification-session', { email, token }).then(({ valid }) => {+ if (!valid) {+ return this.router.transitionTo('auth.login');+ }+ });+ });+ }++ async setupController(controller) {+ super.setupController(...arguments);+ let { email } = await this.session.store.restore();+ controller.email = email;+ }+}diff --git a/console/app/templates/auth/verification.hbs b/console/app/templates/auth/verification.hbsnew file mode 100644index 00000000..f65cfd81--- /dev/null+++ b/console/app/templates/auth/verification.hbs@@ -0,0 +1,46 @@+{{page-title "Account Verification"}}+<div class="bg-white dark:bg-gray-800 py-8 px-4 shadow rounded-lg w-full">+ <div class="mb-8">+ <img class="mx-auto h-12 w-auto " src="/images/fleetbase-logo-svg.svg" alt={{t "app.name"}}>+ <h2 class="mt-6 text-center text-lg font-extrabold text-gray-900 dark:text-white truncate">+ Verify your email address+ </h2>+ </div>++ <div class="flex px-3 py-2 mb-6 rounded-md shadow-sm bg-blue-200">+ <div>+ <FaIcon @icon="shield-check" @size="lg" class="text-blue-900 mr-4" />+ </div>+ <p class="flex-1 text-sm text-blue-900 dark:text-blue-900">+ <strong>Almost done!</strong><br> Check your email for a verification code.+ </p>+ </div>++ <form class="mt-8 space-y-6" {{on "submit" this.verifyCode}}>+ <InputGroup @type="tel" @name="Verification Code" @value={{this.code}} @helpText="Enter the verification code you received via email." @inputClass="input-lg" {{on "input" this.validateInput}} />++ <div>+ <Button @icon="check" @iconPrefix="fas" @buttonType="submit" @type="primary" @size="lg" @text="Verify & Continue" @isLoading={{this.isLoading}} @disabled={{this.isNotReadyToSubmit}} @onClick={{this.verifyCode}} />+ </div>++ {{#if this.stillWaiting}}+ <div class="bg-yellow-50 rounded shadow-sm border-l-4 border-yellow-400 px-4 py-2">+ <div class="flex">+ <div class="flex-shrink-0">+ <FaIcon @icon="exclamation-triangle" @size="lg" class="text-yellow-400" />+ </div>+ <div class="ml-3 flex items-center">+ <span class="text-lg font-extrabold text-yellow-800">Didn't receive an email yet?</span>+ </div>+ </div>+ <div class="py-3">+ <p class="text-yellow-700">Use alternaitve options below to verify your account.</p>+ <div class="flex items-center mt-3">+ <Button @type="default" class="mr-2" @onClick={{this.resendEmail}}>Resend Email</Button>+ <Button @type="default" @onClick={{this.resendBySms}}>Send by SMS</Button>+ </div>+ </div>+ </div>+ {{/if}}+ </form>+</div>\ No newline at end of filediff --git a/console/app/templates/onboard/verify-email.hbs b/console/app/templates/onboard/verify-email.hbsindex 0e42ca76..f65cfd81 100644--- a/console/app/templates/onboard/verify-email.hbs+++ b/console/app/templates/onboard/verify-email.hbs@@ -1,3 +1,4 @@+{{page-title "Account Verification"}}<div class="bg-white dark:bg-gray-800 py-8 px-4 shadow rounded-lg w-full"><div class="mb-8"><img class="mx-auto h-12 w-auto " src="/images/fleetbase-logo-svg.svg" alt={{t "app.name"}}>diff --git a/console/tests/unit/controllers/auth/verification-test.js b/console/tests/unit/controllers/auth/verification-test.jsnew file mode 100644index 00000000..b4dc9c1c--- /dev/null+++ b/console/tests/unit/controllers/auth/verification-test.js@@ -0,0 +1,12 @@+import { module, test } from 'qunit';+import { setupTest } from '@fleetbase/console/tests/helpers';++module('Unit | Controller | auth/verification', function (hooks) {+ setupTest(hooks);++ // TODO: Replace this with your real tests.+ test('it exists', function (assert) {+ let controller = this.owner.lookup('controller:auth/verification');+ assert.ok(controller);+ });+});diff --git a/console/tests/unit/routes/auth/verification-test.js b/console/tests/unit/routes/auth/verification-test.jsnew file mode 100644index 00000000..df997575--- /dev/null+++ b/console/tests/unit/routes/auth/verification-test.js@@ -0,0 +1,11 @@+import { module, test } from 'qunit';+import { setupTest } from '@fleetbase/console/tests/helpers';++module('Unit | Route | auth/verification', function (hooks) {+ setupTest(hooks);++ test('it exists', function (assert) {+ let route = this.owner.lookup('route:auth/verification');+ assert.ok(route);+ });+});