diff --git a/change/@zhishuyun-hub-5e569cba-c1c4-4bc5-83ee-98aab5540fbf.json b/change/@zhishuyun-hub-5e569cba-c1c4-4bc5-83ee-98aab5540fbf.json new file mode 100644 index 0000000..7436e10 --- /dev/null +++ b/change/@zhishuyun-hub-5e569cba-c1c4-4bc5-83ee-98aab5540fbf.json @@ -0,0 +1,7 @@ +{ + "type": "patch", + "comment": "fix inviter id setting", + "packageName": "@zhishuyun/hub", + "email": "cqc@germey.cn", + "dependentChangeType": "patch" +} diff --git a/src/App.vue b/src/App.vue index 3b139f4..21a7b74 100644 --- a/src/App.vue +++ b/src/App.vue @@ -9,8 +9,8 @@ import { defineComponent } from 'vue'; import { ElConfigProvider } from 'element-plus'; import zhCn from 'element-plus/dist/locale/zh-cn.mjs'; -import { getDomain, getPath, setCookie } from './utils'; import AuthPanel from './components/common/AuthPanel.vue'; +import { setCookie } from 'typescript-cookie'; export default defineComponent({ components: { @@ -23,6 +23,9 @@ export default defineComponent({ inviterId: this.$route.query.inviter_id?.toString() }; }, + mounted() { + this.onSetCookie(); + }, methods: { onSetCookie() { // set inviter to cookies to persist @@ -31,8 +34,6 @@ export default defineComponent({ const expiration = new Date(); expiration.setDate(expiration.getDate() + 7); setCookie('INVITER_ID', this.inviterId, { - domain: getDomain(), - path: getPath(), expires: expiration }); } diff --git a/src/components/common/AuthPanel.vue b/src/components/common/AuthPanel.vue index f0b6192..babb859 100644 --- a/src/components/common/AuthPanel.vue +++ b/src/components/common/AuthPanel.vue @@ -15,18 +15,22 @@ import { defineComponent } from 'vue'; import { ElDialog } from 'element-plus'; import { getBaseUrlAuth } from '@/utils'; +import { getCookie } from 'typescript-cookie'; export default defineComponent({ name: 'AuthPanel', components: { ElDialog }, - data() { - return { - iframeUrl: `${getBaseUrlAuth()}/auth/login` - }; - }, computed: { + iframeUrl() { + return `${getBaseUrlAuth()}/auth/login?inviter_id=${this.inviterId}`; + }, + inviterId() { + const result = getCookie('INVITER_ID') || this.$route.query.inviter_id?.toString(); + console.debug('inviterId', result); + return result; + }, authenticated() { return !!this.$store.state.token.access; } diff --git a/src/utils/cookies.ts b/src/utils/cookies.ts deleted file mode 100644 index 7568674..0000000 --- a/src/utils/cookies.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { removeCookie, setCookie as baseSetCookie } from 'typescript-cookie'; -import { CookieAttributes } from 'typescript-cookie/dist/types'; - -export const getDomain = () => { - const host = window.location.host; - // process test env and prod env, for example: - // auth.zhishuyun.com -> .zhishuyun.com - // auth.test.zhishuyun.com -> .zhishuyun.com - const domain = host.replace(/^\S+?\.(test\.|local\.)?/, '.'); - return domain; -}; - -export const getPath = () => { - return '/'; -}; - -export const setCookie = (key: string, value: string, attributes: CookieAttributes) => { - baseSetCookie(key, value, { - domain: attributes?.domain || getDomain(), - path: attributes?.path || getPath(), - expires: attributes?.expires, - sameSite: attributes?.sameSite, - secure: attributes?.secure - }); -}; - -export const resetCookies = () => { - // remove tokens from cookies - removeCookie('ACCESS_TOKEN', { domain: getDomain(), path: getPath() }); - removeCookie('REFRESH_TOKEN', { domain: getDomain(), path: getPath() }); -}; diff --git a/src/utils/index.ts b/src/utils/index.ts index 88063ad..c435920 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,4 +1,3 @@ export * from './baseUrl'; export * from './mode'; export * from './log'; -export * from './cookies';