From 957fb6e48e1bbe8340e124c10b42cd94610d812d Mon Sep 17 00:00:00 2001 From: Yash Maheshwari Date: Fri, 8 Mar 2024 12:42:18 +0530 Subject: [PATCH 1/5] Implemented: support to login the user using the token and oms found in query params --- src/store/auth.ts | 12 +++++++++++- src/views/Login.vue | 24 ++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/store/auth.ts b/src/store/auth.ts index c15cf61..a55a03d 100644 --- a/src/store/auth.ts +++ b/src/store/auth.ts @@ -28,7 +28,7 @@ export const useAuthStore = defineStore('authStore', { getBaseUrl: (state) => { let baseURL = process.env.VUE_APP_BASE_URL if (!baseURL) baseURL = state.oms - return baseURL.startsWith('http') ? baseURL : `https://${baseURL}.hotwax.io/api/` + return baseURL.startsWith('http') ? baseURL.includes('/api') ? baseURL : `${baseURL}/api/` : `https://${baseURL}.hotwax.io/api/` }, getRedirectUrl: (state) => state.redirectUrl, }, @@ -102,6 +102,16 @@ export const useAuthStore = defineStore('authStore', { } this.redirectUrl = '' updateToken(''); + }, + async setToken(token: any, expirationTime: any) { + this.token = { + value: token, + expiration: expirationTime + } + updateToken(token) + }, + async setCurrent(current: any) { + this.current = current } }, persist: true diff --git a/src/views/Login.vue b/src/views/Login.vue index d6742c8..c915a14 100644 --- a/src/views/Login.vue +++ b/src/views/Login.vue @@ -113,8 +113,14 @@ export default defineComponent({ async initialise() { this.hideBackground = true await this.presentLoader("Processing"); - // SAML login handling as only token will be returned in the query - if (this.$route.query?.token) { + + // Run the basic login flow when oms and token both are found in query + if (this.$route.query?.oms && this.$route.query?.token) { + await this.basicLogin() + this.dismissLoader(); + return; + } else if (this.$route.query?.token) { + // SAML login handling as only token will be returned in the query when login through SAML await this.samlLogin() this.dismissLoader(); return @@ -258,6 +264,20 @@ export default defineComponent({ console.error(error) } }, + async basicLogin() { + try { + const { oms, token, expirationTime } = this.$route.query as any + await this.authStore.setOMS(oms); + + const current = await UserService.getUserProfile(token); + await this.authStore.setToken(token, expirationTime) + await this.authStore.setCurrent(current) + } catch (error) { + showToast(translate('Failed to fetch user-profile, please try again')); + console.error("error: ", error); + } + this.router.push('/') + }, async confirmActvSessnLoginOnRedrct() { this.isConfirmingForActiveSession = true const alert = await alertController From 56872b25ef2de3b67ebd5d1dd85a4d966e48a37b Mon Sep 17 00:00:00 2001 From: Yash Maheshwari Date: Fri, 8 Mar 2024 12:45:26 +0530 Subject: [PATCH 2/5] Improved: version for oms-api to latest release --- package-lock.json | 14 +++++++------- package.json | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2b590e2..d27e8e8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "2.1.0", "dependencies": { "@hotwax/dxp-components": "^1.11.0", - "@hotwax/oms-api": "^1.9.0", + "@hotwax/oms-api": "^1.13.0", "@ionic/core": "^7.6.0", "@ionic/vue": "^7.6.0", "@ionic/vue-router": "^7.6.0", @@ -2749,9 +2749,9 @@ } }, "node_modules/@hotwax/oms-api": { - "version": "1.9.0", - "resolved": "https://registry.npmjs.org/@hotwax/oms-api/-/oms-api-1.9.0.tgz", - "integrity": "sha512-1PcS95vP8PzzlBRwHQRk99eJT0xStBSmNdpdoATRUSlLamxmHZ8RAwAwY3usph5gNy7Z+WzAxl9fJXX9VgWP9g==", + "version": "1.13.0", + "resolved": "https://registry.npmjs.org/@hotwax/oms-api/-/oms-api-1.13.0.tgz", + "integrity": "sha512-D7bdS1XBfEu9fT23+mViC4FbyhFfg9GN95Epp/5V85Xcl8udxqOjHI9xcRMxANgrMjPNksshT1rm7F/TfEi/+g==", "dependencies": { "@types/node-json-transform": "^1.0.0", "axios": "^0.21.1", @@ -15925,9 +15925,9 @@ } }, "@hotwax/oms-api": { - "version": "1.9.0", - "resolved": "https://registry.npmjs.org/@hotwax/oms-api/-/oms-api-1.9.0.tgz", - "integrity": "sha512-1PcS95vP8PzzlBRwHQRk99eJT0xStBSmNdpdoATRUSlLamxmHZ8RAwAwY3usph5gNy7Z+WzAxl9fJXX9VgWP9g==", + "version": "1.13.0", + "resolved": "https://registry.npmjs.org/@hotwax/oms-api/-/oms-api-1.13.0.tgz", + "integrity": "sha512-D7bdS1XBfEu9fT23+mViC4FbyhFfg9GN95Epp/5V85Xcl8udxqOjHI9xcRMxANgrMjPNksshT1rm7F/TfEi/+g==", "requires": { "@types/node-json-transform": "^1.0.0", "axios": "^0.21.1", diff --git a/package.json b/package.json index bed7eb5..0aa05b8 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ }, "dependencies": { "@hotwax/dxp-components": "^1.11.0", - "@hotwax/oms-api": "^1.9.0", + "@hotwax/oms-api": "^1.13.0", "@ionic/core": "^7.6.0", "@ionic/vue": "^7.6.0", "@ionic/vue-router": "^7.6.0", From dabcad702c89b73a14f4e0b6df0c19276fa14b0a Mon Sep 17 00:00:00 2001 From: Yash Maheshwari Date: Fri, 8 Mar 2024 14:29:28 +0530 Subject: [PATCH 3/5] Implemented: support to present alert when user is already logged-in --- src/router/index.ts | 2 +- src/views/Login.vue | 25 ++++++++++++++++++++----- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/router/index.ts b/src/router/index.ts index 1016946..2a5fe6c 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -6,7 +6,7 @@ import { useAuthStore } from "@/store/auth"; const loginGuard = (to: any, from: any, next: any) => { const authStore = useAuthStore() - if (authStore.isAuthenticated && !to.query?.redirectUrl) { + if (authStore.isAuthenticated && !to.query?.redirectUrl && !to.query?.oms) { next('/home') } next(); diff --git a/src/views/Login.vue b/src/views/Login.vue index c915a14..b2c1e97 100644 --- a/src/views/Login.vue +++ b/src/views/Login.vue @@ -116,9 +116,14 @@ export default defineComponent({ // Run the basic login flow when oms and token both are found in query if (this.$route.query?.oms && this.$route.query?.token) { - await this.basicLogin() - this.dismissLoader(); - return; + // if a session is already active, present alert + if (this.authStore.isAuthenticated) { + await this.confirmActvSessnLoginOnRedrct(true) + } else { + await this.basicLogin() + this.dismissLoader(); + return; + } } else if (this.$route.query?.token) { // SAML login handling as only token will be returned in the query when login through SAML await this.samlLogin() @@ -278,7 +283,8 @@ export default defineComponent({ } this.router.push('/') }, - async confirmActvSessnLoginOnRedrct() { + // Pass redirect as true when you want to remove all the url params when user clicks on login + async confirmActvSessnLoginOnRedrct(redirect = false) { this.isConfirmingForActiveSession = true const alert = await alertController .create({ @@ -289,7 +295,11 @@ export default defineComponent({ buttons: [{ text: translate('Resume'), handler: () => { - window.location.href = `${this.authStore.getRedirectUrl}?oms=${this.authStore.oms}&token=${this.authStore.token.value}&expirationTime=${this.authStore.token.expiration}` + if(this.authStore.getRedirectUrl) { + window.location.href = `${this.authStore.getRedirectUrl}?oms=${this.authStore.oms}&token=${this.authStore.token.value}&expirationTime=${this.authStore.token.expiration}` + } else { + this.router.push('/') + } this.isConfirmingForActiveSession = false; } }, { @@ -297,6 +307,11 @@ export default defineComponent({ handler: async () => { const redirectUrl = this.authStore.getRedirectUrl await this.authStore.logout() + + if(redirect) { + this.router.push('/login') + } + this.authStore.setRedirectUrl(redirectUrl) this.isConfirmingForActiveSession = false; } From 6ee7daf586f65bcf87f92f731eefdf72fb158c84 Mon Sep 17 00:00:00 2001 From: Yash Maheshwari Date: Fri, 8 Mar 2024 15:20:37 +0530 Subject: [PATCH 4/5] Improved: support to login with the credentials coming in url when confirming the login button in the alert --- src/views/Login.vue | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/views/Login.vue b/src/views/Login.vue index b2c1e97..54ac1c7 100644 --- a/src/views/Login.vue +++ b/src/views/Login.vue @@ -274,8 +274,10 @@ export default defineComponent({ const { oms, token, expirationTime } = this.$route.query as any await this.authStore.setOMS(oms); - const current = await UserService.getUserProfile(token); + // Setting token previous to getting user-profile, if not then the client method honors the state token await this.authStore.setToken(token, expirationTime) + + const current = await UserService.getUserProfile(token); await this.authStore.setCurrent(current) } catch (error) { showToast(translate('Failed to fetch user-profile, please try again')); @@ -307,13 +309,13 @@ export default defineComponent({ handler: async () => { const redirectUrl = this.authStore.getRedirectUrl await this.authStore.logout() + this.isConfirmingForActiveSession = false; + this.authStore.setRedirectUrl(redirectUrl) if(redirect) { - this.router.push('/login') + this.basicLogin() + return; } - - this.authStore.setRedirectUrl(redirectUrl) - this.isConfirmingForActiveSession = false; } }] }); From 96094285c9de55fbe700c562d2de6bf68c648c04 Mon Sep 17 00:00:00 2001 From: Yash Maheshwari Date: Fri, 8 Mar 2024 15:22:19 +0530 Subject: [PATCH 5/5] Fixed: unwanted diff change --- src/views/Login.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/views/Login.vue b/src/views/Login.vue index 54ac1c7..b46d810 100644 --- a/src/views/Login.vue +++ b/src/views/Login.vue @@ -309,8 +309,8 @@ export default defineComponent({ handler: async () => { const redirectUrl = this.authStore.getRedirectUrl await this.authStore.logout() - this.isConfirmingForActiveSession = false; this.authStore.setRedirectUrl(redirectUrl) + this.isConfirmingForActiveSession = false; if(redirect) { this.basicLogin()