Add Cookie of inviter id perist (#15)
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"type": "patch",
|
||||
"comment": "add inviter id cookies persist",
|
||||
"packageName": "@zhishuyun/hub",
|
||||
"email": "cqc@cuiqingcai.com",
|
||||
"dependentChangeType": "patch"
|
||||
}
|
||||
+18
-1
@@ -8,6 +8,7 @@
|
||||
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: {
|
||||
@@ -15,8 +16,24 @@ export default defineComponent({
|
||||
},
|
||||
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>
|
||||
|
||||
@@ -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,3 +1,4 @@
|
||||
export * from './baseUrl';
|
||||
export * from './mode';
|
||||
export * from './log';
|
||||
export * from './cookies';
|
||||
|
||||
Reference in New Issue
Block a user