Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
adityasharma7 committed Jul 19, 2023
2 parents bef2b68 + f8db5d9 commit 245c911
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
23 changes: 12 additions & 11 deletions src/components/Login.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineComponent, getCurrentInstance } from "vue"
import { loginContext as context, useAuthStore } from "../index"
import { defineComponent } from "vue"
import { loginContext as context, useAuthStore, appContext } from "../index"

export default defineComponent({
template: `
Expand All @@ -12,17 +12,16 @@ export default defineComponent({
data() {
return {
errorMsg: '',
authStore: {} as any,
router: {} as any,
route: {} as any,
authStore: {} as any
}
},
async mounted() {
// exporting the same from index.ts through globalProperties does not work as expected
const instance = getCurrentInstance() as any
this.route = instance.appContext.config.globalProperties.$route
this.router = instance.appContext.config.globalProperties.$router
this.authStore = useAuthStore()
this.router = appContext.config.globalProperties.$router
this.route = appContext.config.globalProperties.$route

if (!Object.keys(this.route.query).length) {
this.errorMsg = 'Unable to login. Could not authenticate the user'
return
Expand All @@ -32,19 +31,21 @@ export default defineComponent({
const appToken = this.authStore.token.value
const appOms = this.authStore.oms
const appExpirationTime = this.authStore.token.expiration

// show alert if token/oms are different from the app's
// show alert if token/oms exist and are different from the app's
if ((appToken && token) && (appToken != token || appOms != oms)) {
// for backward compatibility
this.authStore.$patch({
token: { value: appToken, expiration: appExpirationTime },
oms: appOms
})
await context.confirmSessionEnd('dev-oms').then((isConfirmed: boolean) => {
context.confirmSessionEnd('dev-oms').then((isConfirmed: boolean) => {
isConfirmed
? this.handleUserFlow(token, oms, expirationTime)
: this.router.push('/')
})
} else {
this.handleUserFlow(token, oms, expirationTime)
}
},
methods: {
Expand All @@ -61,7 +62,7 @@ export default defineComponent({
context.loader.present('Logging in')
try {
await context.login({ token, oms })
this.router.replace({ path: '/' })
this.router.push('/')
} catch (error) {
console.error(error)
this.errorMsg = 'Unable to login. Please contact the administrator'
Expand Down
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ pinia.use(piniaPluginPersistedstate)

let loginContext = {} as any
let shopifyImgContext = {} as any

let appContext = {} as any
// executed on app initialization
export let dxpComponents = {
install(app: any, options: any) {
appContext = app

// registering pinia in the app
app.use(pinia);

Expand All @@ -39,5 +41,6 @@ export {
loginContext,
shopifyImgContext,
ShopifyImg,
goToOms
goToOms,
appContext
}
3 changes: 2 additions & 1 deletion src/store/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ export const useAuthStore = defineStore('userAuth', {
// console.log(error)
// }
}
}
},
persist: true
})

0 comments on commit 245c911

Please sign in to comment.