mirror of
https://github.com/m1k1o/neko.git
synced 2024-07-24 14:40:50 +12:00
use token if returned in session payload.
This commit is contained in:
parent
6312ae2cd2
commit
bf7a3b9163
@ -3,13 +3,15 @@ import * as Api from '../api'
|
|||||||
export class NekoApi {
|
export class NekoApi {
|
||||||
api_configuration = new Api.Configuration({
|
api_configuration = new Api.Configuration({
|
||||||
basePath: location.href.replace(/\/+$/, ''),
|
basePath: location.href.replace(/\/+$/, ''),
|
||||||
|
baseOptions: { withCredentials: true },
|
||||||
})
|
})
|
||||||
|
|
||||||
public setUrl(url: string) {
|
public setUrl(url: string) {
|
||||||
this.api_configuration = new Api.Configuration({
|
this.api_configuration.basePath = url.replace(/\/+$/, '')
|
||||||
basePath: url.replace(/\/+$/, ''),
|
}
|
||||||
baseOptions: { withCredentials: true },
|
|
||||||
})
|
public setToken(token: string) {
|
||||||
|
this.api_configuration.accessToken = token
|
||||||
}
|
}
|
||||||
|
|
||||||
get url(): string {
|
get url(): string {
|
||||||
|
@ -17,6 +17,7 @@ export class NekoWebSocket extends EventEmitter<NekoWebSocketEvents> {
|
|||||||
private _connTimer?: NodeJS.Timeout
|
private _connTimer?: NodeJS.Timeout
|
||||||
private _log: Logger
|
private _log: Logger
|
||||||
private _url: string
|
private _url: string
|
||||||
|
private _token: string
|
||||||
|
|
||||||
// reconnection
|
// reconnection
|
||||||
private _reconTimer?: NodeJS.Timeout
|
private _reconTimer?: NodeJS.Timeout
|
||||||
@ -28,6 +29,7 @@ export class NekoWebSocket extends EventEmitter<NekoWebSocketEvents> {
|
|||||||
this._log = new Logger('websocket')
|
this._log = new Logger('websocket')
|
||||||
|
|
||||||
this._url = ''
|
this._url = ''
|
||||||
|
this._token = ''
|
||||||
this.setUrl(location.href)
|
this.setUrl(location.href)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,6 +37,10 @@ export class NekoWebSocket extends EventEmitter<NekoWebSocketEvents> {
|
|||||||
this._url = url.replace(/^http/, 'ws').replace(/\/+$/, '') + '/api/ws'
|
this._url = url.replace(/^http/, 'ws').replace(/\/+$/, '') + '/api/ws'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public setToken(token: string) {
|
||||||
|
this._token = token
|
||||||
|
}
|
||||||
|
|
||||||
get supported() {
|
get supported() {
|
||||||
return typeof WebSocket !== 'undefined' && WebSocket.OPEN === 1
|
return typeof WebSocket !== 'undefined' && WebSocket.OPEN === 1
|
||||||
}
|
}
|
||||||
@ -55,7 +61,12 @@ export class NekoWebSocket extends EventEmitter<NekoWebSocketEvents> {
|
|||||||
|
|
||||||
this.emit('connecting')
|
this.emit('connecting')
|
||||||
|
|
||||||
this._ws = new WebSocket(this._url)
|
let url = this._url
|
||||||
|
if (this._token) {
|
||||||
|
url += '?token=' + encodeURIComponent(this._token)
|
||||||
|
}
|
||||||
|
|
||||||
|
this._ws = new WebSocket(url)
|
||||||
this._log.info(`connecting`)
|
this._log.info(`connecting`)
|
||||||
|
|
||||||
this._ws.onopen = this.onConnected.bind(this)
|
this._ws.onopen = this.onConnected.bind(this)
|
||||||
|
@ -206,7 +206,12 @@
|
|||||||
throw new Error('client already authenticated')
|
throw new Error('client already authenticated')
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.api.session.login({ username, password })
|
const res = await this.api.session.login({ username, password })
|
||||||
|
if (res.data.token) {
|
||||||
|
this.api.setToken(res.data.token)
|
||||||
|
this.websocket.setToken(res.data.token)
|
||||||
|
}
|
||||||
|
|
||||||
Vue.set(this.state.connection, 'authenticated', true)
|
Vue.set(this.state.connection, 'authenticated', true)
|
||||||
this.websocket.connect()
|
this.websocket.connect()
|
||||||
}
|
}
|
||||||
@ -223,6 +228,9 @@
|
|||||||
try {
|
try {
|
||||||
await this.api.session.logout()
|
await this.api.session.logout()
|
||||||
} finally {
|
} finally {
|
||||||
|
this.api.setToken('')
|
||||||
|
this.websocket.setToken('')
|
||||||
|
|
||||||
Vue.set(this.state.connection, 'authenticated', false)
|
Vue.set(this.state.connection, 'authenticated', false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user