send client id header with every request initiated from frontend to backend
This commit is contained in:
parent
e7628895c9
commit
8f31a669d5
6 changed files with 28 additions and 17 deletions
|
|
@ -3,6 +3,7 @@
|
||||||
import { ClientEvent, EncodedGame, GameEvent, ReplayData, ServerEvent } from '../common/Types'
|
import { ClientEvent, EncodedGame, GameEvent, ReplayData, ServerEvent } from '../common/Types'
|
||||||
import Util, { logger } from '../common/Util'
|
import Util, { logger } from '../common/Util'
|
||||||
import Protocol from './../common/Protocol'
|
import Protocol from './../common/Protocol'
|
||||||
|
import xhr from './xhr'
|
||||||
|
|
||||||
const log = logger('Communication.js')
|
const log = logger('Communication.js')
|
||||||
|
|
||||||
|
|
@ -117,7 +118,7 @@ async function requestReplayData(
|
||||||
offset: number
|
offset: number
|
||||||
): Promise<ReplayData> {
|
): Promise<ReplayData> {
|
||||||
const args = { gameId, offset }
|
const args = { gameId, offset }
|
||||||
const res = await fetch(`/api/replay-data${Util.asQueryArgs(args)}`)
|
const res = await xhr.get(`/api/replay-data${Util.asQueryArgs(args)}`, {})
|
||||||
const json: ReplayData = await res.json()
|
const json: ReplayData = await res.json()
|
||||||
return json
|
return json
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@
|
||||||
</template>
|
</template>
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { defineComponent } from 'vue'
|
import { defineComponent } from 'vue'
|
||||||
|
import xhr from '../xhr'
|
||||||
|
|
||||||
export default defineComponent({
|
export default defineComponent({
|
||||||
name: 'upload',
|
name: 'upload',
|
||||||
|
|
@ -21,8 +22,7 @@ export default defineComponent({
|
||||||
if (!file) return;
|
if (!file) return;
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
formData.append('file', file, file.name);
|
formData.append('file', file, file.name);
|
||||||
const res = await fetch('/upload', {
|
const res = await xhr.post('/upload', {
|
||||||
method: 'post',
|
|
||||||
body: formData,
|
body: formData,
|
||||||
})
|
})
|
||||||
const j = await res.json()
|
const j = await res.json()
|
||||||
|
|
|
||||||
|
|
@ -7,19 +7,23 @@ import NewGame from './views/NewGame.vue'
|
||||||
import Game from './views/Game.vue'
|
import Game from './views/Game.vue'
|
||||||
import Replay from './views/Replay.vue'
|
import Replay from './views/Replay.vue'
|
||||||
import Util from './../common/Util'
|
import Util from './../common/Util'
|
||||||
|
import settings from './settings'
|
||||||
|
import xhr from './xhr'
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
const res = await fetch(`/api/conf`)
|
function initClientId() {
|
||||||
const conf = await res.json()
|
let ID = settings.getStr('ID', '')
|
||||||
|
|
||||||
function initme() {
|
|
||||||
let ID = localStorage.getItem('ID')
|
|
||||||
if (!ID) {
|
if (!ID) {
|
||||||
ID = Util.uniqId()
|
ID = Util.uniqId()
|
||||||
localStorage.setItem('ID', ID)
|
settings.setStr('ID', ID)
|
||||||
}
|
}
|
||||||
return ID
|
return ID
|
||||||
}
|
}
|
||||||
|
const clientId = initClientId()
|
||||||
|
xhr.setClientId(clientId)
|
||||||
|
|
||||||
|
const res = await xhr.get(`/api/conf`, {})
|
||||||
|
const conf = await res.json()
|
||||||
|
|
||||||
const router = VueRouter.createRouter({
|
const router = VueRouter.createRouter({
|
||||||
history: VueRouter.createWebHashHistory(),
|
history: VueRouter.createWebHashHistory(),
|
||||||
|
|
@ -40,7 +44,7 @@ import Util from './../common/Util'
|
||||||
|
|
||||||
const app = Vue.createApp(App)
|
const app = Vue.createApp(App)
|
||||||
app.config.globalProperties.$config = conf
|
app.config.globalProperties.$config = conf
|
||||||
app.config.globalProperties.$clientId = initme()
|
app.config.globalProperties.$clientId = clientId
|
||||||
app.use(router)
|
app.use(router)
|
||||||
app.mount('#app')
|
app.mount('#app')
|
||||||
})()
|
})()
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@
|
||||||
</template>
|
</template>
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { defineComponent } from 'vue'
|
import { defineComponent } from 'vue'
|
||||||
|
import xhr from '../xhr'
|
||||||
|
|
||||||
import GameTeaser from './../components/GameTeaser.vue'
|
import GameTeaser from './../components/GameTeaser.vue'
|
||||||
|
|
||||||
|
|
@ -27,7 +28,7 @@ export default defineComponent({
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
async created() {
|
async created() {
|
||||||
const res = await fetch('/api/index-data')
|
const res = await xhr.get('/api/index-data', {})
|
||||||
const json = await res.json()
|
const json = await res.json()
|
||||||
this.gamesRunning = json.gamesRunning
|
this.gamesRunning = json.gamesRunning
|
||||||
this.gamesFinished = json.gamesFinished
|
this.gamesFinished = json.gamesFinished
|
||||||
|
|
|
||||||
|
|
@ -133,7 +133,7 @@ export default defineComponent({
|
||||||
this.filtersChanged()
|
this.filtersChanged()
|
||||||
},
|
},
|
||||||
async loadImages () {
|
async loadImages () {
|
||||||
const res = await fetch(`/api/newgame-data${Util.asQueryArgs(this.filters)}`)
|
const res = await xhr.get(`/api/newgame-data${Util.asQueryArgs(this.filters)}`, {})
|
||||||
const json = await res.json()
|
const json = await res.json()
|
||||||
this.images = json.images
|
this.images = json.images
|
||||||
this.tags = json.tags
|
this.tags = json.tags
|
||||||
|
|
@ -165,8 +165,7 @@ export default defineComponent({
|
||||||
return await res.json()
|
return await res.json()
|
||||||
},
|
},
|
||||||
async saveImage (data: any) {
|
async saveImage (data: any) {
|
||||||
const res = await fetch('/api/save-image', {
|
const res = await xhr.post('/api/save-image', {
|
||||||
method: 'post',
|
|
||||||
headers: {
|
headers: {
|
||||||
'Accept': 'application/json',
|
'Accept': 'application/json',
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
|
|
@ -200,8 +199,7 @@ export default defineComponent({
|
||||||
this.dialog = 'new-game'
|
this.dialog = 'new-game'
|
||||||
},
|
},
|
||||||
async onNewGame(gameSettings: GameSettings) {
|
async onNewGame(gameSettings: GameSettings) {
|
||||||
const res = await fetch('/api/newgame', {
|
const res = await xhr.post('/api/newgame', {
|
||||||
method: 'post',
|
|
||||||
headers: {
|
headers: {
|
||||||
'Accept': 'application/json',
|
'Accept': 'application/json',
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ export interface Options {
|
||||||
onUploadProgress?: (ev: ProgressEvent<XMLHttpRequestEventTarget>) => any,
|
onUploadProgress?: (ev: ProgressEvent<XMLHttpRequestEventTarget>) => any,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let xhrClientId: string = ''
|
||||||
const request = async (
|
const request = async (
|
||||||
method: string,
|
method: string,
|
||||||
url: string,
|
url: string,
|
||||||
|
|
@ -22,6 +23,9 @@ const request = async (
|
||||||
for (const k in options.headers || {}) {
|
for (const k in options.headers || {}) {
|
||||||
xhr.setRequestHeader(k, options.headers[k])
|
xhr.setRequestHeader(k, options.headers[k])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
xhr.setRequestHeader('Client-Id', xhrClientId)
|
||||||
|
|
||||||
xhr.addEventListener('load', function (ev: ProgressEvent<XMLHttpRequestEventTarget>
|
xhr.addEventListener('load', function (ev: ProgressEvent<XMLHttpRequestEventTarget>
|
||||||
) {
|
) {
|
||||||
resolve({
|
resolve({
|
||||||
|
|
@ -41,7 +45,7 @@ const request = async (
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
xhr.send(options.body)
|
xhr.send(options.body || null)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -53,4 +57,7 @@ export default {
|
||||||
post: (url: string, options: any): Promise<Response> => {
|
post: (url: string, options: any): Promise<Response> => {
|
||||||
return request('post', url, options)
|
return request('post', url, options)
|
||||||
},
|
},
|
||||||
|
setClientId: (clientId: string): void => {
|
||||||
|
xhrClientId = clientId
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue