Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed getCurrentInstance issue #110

Merged
merged 3 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
})
Loading