From 0cdb32d71b910677562310022373647a69262490 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B4=94=E5=BA=86=E6=89=8D=E4=B8=A8=E9=9D=99=E8=A7=85?= Date: Wed, 3 Jan 2024 02:22:40 +0800 Subject: [PATCH] Fix inviter id setting (#17) --- ...-5e569cba-c1c4-4bc5-83ee-98aab5540fbf.json | 7 +++++ src/App.vue | 7 +++-- src/components/common/AuthPanel.vue | 14 ++++++--- src/utils/cookies.ts | 31 ------------------- src/utils/index.ts | 1 - 5 files changed, 20 insertions(+), 40 deletions(-) create mode 100644 change/@zhishuyun-hub-5e569cba-c1c4-4bc5-83ee-98aab5540fbf.json delete mode 100644 src/utils/cookies.ts 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';