Add: Login/connection error messages

This commit is contained in:
advplyr 2021-11-21 06:54:10 -06:00
parent f5d455feb1
commit 708f399916
6 changed files with 94 additions and 42 deletions

View file

@ -64,23 +64,28 @@ class Server extends EventEmitter {
async connect(url, token) {
if (this.connected) {
console.warn('[SOCKET] Connection already established for ' + this.url)
return true
return { success: true }
}
if (!url) {
console.error('Invalid url to connect')
return false
return {
error: 'Invalid URL'
}
}
var serverUrl = this.getServerUrl(url)
var res = await this.ping(serverUrl)
if (!res || !res.success) {
//this.setServerUrl(null)
return false
return {
error: res ? res.error : 'Unknown Error'
}
}
var authRes = await this.authorize(serverUrl, token)
if (!authRes || !authRes.user) {
return false
if (!authRes || authRes.error) {
return {
error: authRes ? authRes.error : 'Authorization Error'
}
}
this.setServerUrl(serverUrl)
@ -88,19 +93,26 @@ class Server extends EventEmitter {
this.setUser(authRes.user)
this.connectSocket()
return true
return { success: true }
}
async check(url) {
var serverUrl = this.getServerUrl(url)
if (!serverUrl) {
return false
return {
error: 'Invalid server url'
}
}
var res = await this.ping(serverUrl)
if (!res || !res.success) {
return false
if (!res || res.error) {
return {
error: res ? res.error : 'Ping Failed'
}
}
return {
success: true,
serverUrl
}
return serverUrl
}
async login(url, username, password) {
@ -122,8 +134,16 @@ class Server extends EventEmitter {
}
}).catch(error => {
console.error('[Server] Server auth failed', error)
var errorMsg = null
if (error.response) {
errorMsg = error.response.data || 'Unknown Error'
} else if (error.request) {
errorMsg = 'Server did not respond'
} else {
errorMsg = 'Failed to send request'
}
return {
error: 'Request Failed'
error: errorMsg
}
})
}
@ -142,7 +162,17 @@ class Server extends EventEmitter {
return res.data
}).catch(error => {
console.error('[Server] Server auth failed', error)
return false
var errorMsg = null
if (error.response) {
errorMsg = error.response.data || 'Unknown Error'
} else if (error.request) {
errorMsg = 'Server did not respond'
} else {
errorMsg = 'Failed to send request'
}
return {
error: errorMsg
}
})
}
@ -153,7 +183,18 @@ class Server extends EventEmitter {
return res.data
}).catch(error => {
console.error('Server check failed', error)
return false
var errorMsg = null
if (error.response) {
errorMsg = error.response.data || 'Unknown Error'
} else if (error.request) {
errorMsg = 'Server did not respond'
} else {
errorMsg = 'Failed to send request'
}
return {
success: false,
error: errorMsg
}
})
}

View file

@ -13,8 +13,8 @@ android {
applicationId "com.audiobookshelf.app"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 44
versionName "0.9.25-beta"
versionCode 45
versionName "0.9.26-beta"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
aaptOptions {
// Files and dirs to omit from the packaged assets dir, modified to accommodate modern web apps.

View file

@ -5,7 +5,6 @@
<Nuxt />
</div>
<app-audio-player-container ref="streamContainer" />
<!-- <modals-downloads-modal ref="downloadsModal" @deleteDownload="deleteDownload" /> -->
<modals-libraries-modal />
<app-side-drawer />
<readers-reader />
@ -14,7 +13,6 @@
<script>
import { Capacitor } from '@capacitor/core'
import { Network } from '@capacitor/network'
import { AppUpdate } from '@robingenz/capacitor-app-update'
import AudioDownloader from '@/plugins/audio-downloader'
import StorageManager from '@/plugins/storage-manager'
@ -276,15 +274,6 @@ export default {
this.$store.commit('setHasStoragePermission', true)
}
},
async setupNetworkListener() {
var status = await Network.getStatus()
this.$store.commit('setNetworkStatus', status)
Network.addListener('networkStatusChange', (status) => {
console.log('Network status changed', status.connected, status.connectionType)
this.$store.commit('setNetworkStatus', status)
})
},
showErrorToast(message) {
this.$toast.error(message)
},
@ -333,7 +322,7 @@ export default {
if (this.$store.state.isFirstLoad) {
this.$store.commit('setIsFirstLoad', false)
await this.setupNetworkListener()
await this.$store.dispatch('setupNetworkListener')
this.attemptConnection()
this.checkForUpdate()
this.initMediaStore()

View file

@ -1,6 +1,6 @@
{
"name": "audiobookshelf-app",
"version": "v0.9.25-beta",
"version": "v0.9.26-beta",
"author": "advplyr",
"scripts": {
"dev": "nuxt --hostname localhost --port 1337",

View file

@ -97,11 +97,11 @@ export default {
}
this.processing = true
this.error = null
var success = await this.$server.check(this.serverUrl)
var response = await this.$server.check(this.serverUrl)
this.processing = false
if (!success) {
if (!response || response.error) {
console.error('Server invalid')
this.error = 'Invalid Server'
this.error = response ? response.error : 'Invalid Server'
} else {
this.showAuth = true
}
@ -143,6 +143,8 @@ export default {
this.redirect()
},
async init() {
await this.$store.dispatch('setupNetworkListener')
if (!this.$server) {
console.error('Invalid server not initialized')
return
@ -162,16 +164,18 @@ export default {
this.serverUrl = localServerUrl
if (localUserToken) {
this.processing = true
var success = await this.$server.connect(localServerUrl, localUserToken)
if (!success && !this.$server.url) {
var response = await this.$server.connect(localServerUrl, localUserToken)
if (!response || response.error) {
var errorMsg = response ? response.error : 'Unknown Error'
this.processing = false
this.error = errorMsg
if (!this.$server.url) {
this.serverUrl = null
this.showAuth = false
} else if (!success) {
console.log('Server connect success')
this.processing = false
}
return
}
console.log('Server connect success')
this.showAuth = true
} else {
this.submit()

View file

@ -1,4 +1,5 @@
import Vue from 'vue'
import { Network } from '@capacitor/network'
export const state = () => ({
streamAudiobook: null,
@ -17,7 +18,8 @@ export const state = () => ({
downloadFolder: null,
showSideDrawer: false,
bookshelfView: 'grid'
bookshelfView: 'grid',
isNetworkListenerInit: false
})
export const getters = {
@ -35,7 +37,20 @@ export const getters = {
}
}
export const actions = {}
export const actions = {
async setupNetworkListener({ state, commit }) {
if (state.isNetworkListenerInit) return
commit('setNetworkListenerInit', true)
var status = await Network.getStatus()
commit('setNetworkStatus', status)
Network.addListener('networkStatusChange', (status) => {
console.log('Network status changed', status.connected, status.connectionType)
commit('setNetworkStatus', status)
})
}
}
export const mutations = {
setHasStoragePermission(state, val) {
@ -80,6 +95,9 @@ export const mutations = {
setSocketConnected(state, val) {
state.socketConnected = val
},
setNetworkListenerInit(state, val) {
state.isNetworkListenerInit = val
},
setNetworkStatus(state, val) {
state.networkConnected = val.connected
state.networkConnectionType = val.connectionType