Skip to content

Commit

Permalink
Add Cookie of inviter id perist (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
崔庆才丨静觅 authored Jan 2, 2024
1 parent 3e78923 commit 4997334
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "add inviter id cookies persist",
"packageName": "@zhishuyun/hub",
"email": "[email protected]",
"dependentChangeType": "patch"
}
19 changes: 18 additions & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,32 @@
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';
export default defineComponent({
components: {
ElConfigProvider
},
data() {
return {
locale: zhCn
locale: zhCn,
inviterId: this.$route.query.inviter_id?.toString()
};
},
methods: {
onSetCookie() {
// set inviter to cookies to persist
if (this.inviterId) {
// current date + 7 days
const expiration = new Date();
expiration.setDate(expiration.getDate() + 7);
setCookie('INVITER_ID', this.inviterId, {
domain: getDomain(),
path: getPath(),
expires: expiration
});
}
}
}
});
</script>
31 changes: 31 additions & 0 deletions src/utils/cookies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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() });
};
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './baseUrl';
export * from './mode';
export * from './log';
export * from './cookies';

0 comments on commit 4997334

Please sign in to comment.