add authenticate function.

This commit is contained in:
Miroslav Šedivý 2021-06-19 21:32:09 +02:00
parent 45c6c33647
commit 5c8c8c1330

View File

@ -184,9 +184,18 @@
Vue.set(this.state, 'authenticated', false) Vue.set(this.state, 'authenticated', false)
} }
// check if is user logged in if (!this.autologin) return
if (this.autologin) { await this.authenticate()
const token = localStorage.getItem('neko_session')
if (!this.autoconnect) return
await this.connect()
}
public async authenticate(token?: string) {
if (!token && this.autologin) {
token = localStorage.getItem('neko_session') ?? undefined
}
if (token) { if (token) {
this.api.setToken(token) this.api.setToken(token)
this.connection.setToken(token) this.connection.setToken(token)
@ -195,9 +204,8 @@
await this.api.session.whoami() await this.api.session.whoami()
Vue.set(this.state, 'authenticated', true) Vue.set(this.state, 'authenticated', true)
if (this.autoconnect) { if (token && this.autologin) {
await this.connect() localStorage.setItem('neko_session', token)
}
} }
} }
@ -217,10 +225,6 @@
} }
Vue.set(this.state, 'authenticated', true) Vue.set(this.state, 'authenticated', true)
if (this.autoconnect) {
await this.connect()
}
} }
public async logout() { public async logout() {
@ -246,7 +250,6 @@
} }
} }
// TODO: Refactor.
public async connect(video?: string) { public async connect(video?: string) {
if (!this.state.authenticated) { if (!this.state.authenticated) {
throw new Error('client not authenticated') throw new Error('client not authenticated')
@ -312,7 +315,6 @@
this.connection.setVideo(video) this.connection.setVideo(video)
} }
// TODO: Refactor.
public setWebRTCAuto(auto: boolean = true) { public setWebRTCAuto(auto: boolean = true) {
Vue.set(this.state.connection.webrtc, 'auto', auto) Vue.set(this.state.connection.webrtc, 'auto', auto)
} }
@ -363,7 +365,6 @@
this.clear() this.clear()
}) })
// webrtc
this.connection.webrtc.on('track', (event: RTCTrackEvent) => { this.connection.webrtc.on('track', (event: RTCTrackEvent) => {
const { track, streams } = event const { track, streams } = event
if (track.kind === 'audio') return if (track.kind === 'audio') return