From 08f55c9680028ad00d13dd38741201090692bd49 Mon Sep 17 00:00:00 2001 From: Dennis Postma Date: Wed, 30 Oct 2024 07:17:07 +0100 Subject: [PATCH] #190 : Cookie domain improvement --- src/services/authentication.ts | 5 ++--- src/stores/gameStore.ts | 4 +--- src/utilities.ts | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/services/authentication.ts b/src/services/authentication.ts index ccf778f..04bb357 100644 --- a/src/services/authentication.ts +++ b/src/services/authentication.ts @@ -1,6 +1,7 @@ import axios from 'axios' import config from '@/config' import { useCookies } from '@vueuse/integrations/useCookies' +import { getDomain } from '@/utilities' export async function register(username: string, email: string, password: string) { try { @@ -16,9 +17,7 @@ export async function login(username: string, password: string) { try { const response = await axios.post(`${config.server_endpoint}/login`, { username, password }) useCookies().set('token', response.data.token as string, { - // for whole domain - // @TODO : #190 - // domain: window.location.hostname.split('.').slice(-2).join('.') + domain: getDomain() }) return { success: true, token: response.data.token } } catch (error: any) { diff --git a/src/stores/gameStore.ts b/src/stores/gameStore.ts index 5c8c39e..fa5b83e 100644 --- a/src/stores/gameStore.ts +++ b/src/stores/gameStore.ts @@ -105,9 +105,7 @@ export const useGameStore = defineStore('game', { this.connection?.disconnect() useCookies().remove('token', { - // for whole domain - // @TODO : #190 - // domain: window.location.hostname.split('.').slice(-2).join('.') + domain: getDomain() }) this.isAssetsLoaded = false diff --git a/src/utilities.ts b/src/utilities.ts index 905dc81..bfb15c8 100644 --- a/src/utilities.ts +++ b/src/utilities.ts @@ -5,3 +5,21 @@ export function uuidv4() { export function unduplicateArray(array: any[]) { return [...new Set(array.flat())] } + +export function getDomain() { + // Check if not localhost + if (window.location.hostname !== 'localhost') { + return window.location.hostname + } + + // Check if not IP address + if (window.location.hostname.match(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/)) { + return window.location.hostname + } + + if (window.location.hostname.split('.').length < 3) { + return window.location.hostname + } + + return window.location.hostname.split('.').slice(-2).join('.') +} \ No newline at end of file