From 3348f72772e1f1578de482b6e56dbd5cb9f89a7d Mon Sep 17 00:00:00 2001 From: yibaebi Date: Tue, 25 Jul 2023 14:20:04 +0100 Subject: [PATCH] add programmatic redirect to dashbord or freshdesk from login page --- app/authenticators/irene.js | 71 +++++++++++++++++++++++-------------- app/routes/login.js | 25 ++++++++++++- 2 files changed, 68 insertions(+), 28 deletions(-) diff --git a/app/authenticators/irene.js b/app/authenticators/irene.js index a37231085..d63757b87 100644 --- a/app/authenticators/irene.js +++ b/app/authenticators/irene.js @@ -4,12 +4,12 @@ import ENV from 'irene/config/environment'; import { inject as service } from '@ember/service'; import { getOwner } from '@ember/application'; - -const b64EncodeUnicode = str => - btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, (match, p1) => String.fromCharCode(`0x${p1}`)) - ) - ; - +const b64EncodeUnicode = (str) => + btoa( + encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, (match, p1) => + String.fromCharCode(`0x${p1}`) + ) + ); const getB64Token = (user, token) => b64EncodeUnicode(`${user}:${token}`); const processData = (data) => { @@ -18,55 +18,72 @@ const processData = (data) => { }; const IreneAuthenticator = Base.extend({ - ajax: service(), + router: service(), + window: service('browser/window'), resumeTransistion() { - const authenticatedRoute = getOwner(this).lookup("route:authenticated"); - const lastTransition = authenticatedRoute.get("lastTransition"); + const authenticatedRoute = getOwner(this).lookup('route:authenticated'); + const lastTransition = authenticatedRoute.get('lastTransition'); + if (lastTransition) { return lastTransition.retry(); } else { - const applicationRoute = getOwner(this).lookup("route:application"); - return applicationRoute.transitionTo(ENV['ember-simple-auth']["routeAfterAuthentication"]); + const applicationRoute = getOwner(this).lookup('route:application'); + return applicationRoute.transitionTo( + ENV['ember-simple-auth']['routeAfterAuthentication'] + ); + } + }, + + async checkAndPerformFrdeskRedirect(username) { + const window = this.get('window'); + const queryParams = this.get('router')?.currentRoute?.queryParams; + + if (queryParams?.next) { + const nextRoute = `${queryParams.next}&username=${username}`; + window.location = nextRoute; + + return; } }, async authenticate(identification, password, otp) { - const ajax = this.get("ajax"); + const ajax = this.get('ajax'); const data = { username: identification, password, - otp - } + otp, + }; const url = ENV['ember-simple-auth']['loginEndPoint']; - return ajax.post(url, { data }) - .then(data => { - data = processData(data); - this.resumeTransistion(); - return data; - }); + return ajax.post(url, { data }).then((data) => { + data = processData(data); + + this.checkAndPerformFrdeskRedirect(identification); + this.resumeTransistion(identification); + + return data; + }); }, async restore(data) { - const ajax = this.get("ajax"); + const ajax = this.get('ajax'); const url = ENV['ember-simple-auth']['checkEndPoint']; await ajax.post(url, { data: {}, headers: { - 'Authorization': `Basic ${data.b64token}` - } - }) + Authorization: `Basic ${data.b64token}`, + }, + }); return data; }, async invalidate() { - const ajax = this.get("ajax"); + const ajax = this.get('ajax'); const url = ENV['ember-simple-auth']['logoutEndPoint']; await ajax.post(url); location.reload(); - } + }, }); - export default IreneAuthenticator; diff --git a/app/routes/login.js b/app/routes/login.js index cc32c7d2b..3b22ea82f 100644 --- a/app/routes/login.js +++ b/app/routes/login.js @@ -1,3 +1,26 @@ import Route from '@ember/routing/route'; +import { inject as service } from '@ember/service'; -export default class LoginRoute extends Route {} +export default class LoginRoute extends Route { + @service session; + @service router; + @service store; + @service('browser/window') window; + + async beforeModel(transition) { + if (this.session.isAuthenticated) { + const { queryParams } = transition.to; + + if (queryParams?.next) { + const userId = this.session.data.authenticated.user_id; + const user = await this.store.findRecord('user', userId); + const username = user.username; + + const nextURL = `${queryParams.next}&username=${username}`; + this.window.location.assign(nextURL); + } else { + this.router.transitionTo('authenticated.projects'); + } + } + } +}