2022-04-03 14:24:17 -05:00
|
|
|
<template>
|
2022-07-01 16:33:39 -05:00
|
|
|
<div class="w-full max-w-md mx-auto px-2 sm:px-4 lg:px-8 z-10">
|
|
|
|
<div v-show="!loggedIn" class="mt-8 bg-primary overflow-hidden shadow rounded-lg px-4 py-6 w-full">
|
2022-04-03 14:24:17 -05:00
|
|
|
<template v-if="!showForm">
|
|
|
|
<div v-for="config in serverConnectionConfigs" :key="config.id" class="flex items-center py-4 my-1 border-b border-white border-opacity-10 relative" @click="connectToServer(config)">
|
|
|
|
<span class="material-icons-outlined text-xl text-gray-300">dns</span>
|
|
|
|
<p class="pl-3 pr-6 text-base text-gray-200">{{ config.name }}</p>
|
|
|
|
|
|
|
|
<div class="absolute top-0 right-0 h-full px-4 flex items-center" @click.stop="editServerConfig(config)">
|
|
|
|
<span class="material-icons text-lg text-gray-300">more_vert</span>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="my-1 py-4 w-full">
|
|
|
|
<ui-btn class="w-full" @click="newServerConfigClick">Add New Server</ui-btn>
|
|
|
|
</div>
|
|
|
|
</template>
|
2022-04-17 16:59:49 -05:00
|
|
|
<div v-else class="w-full">
|
2022-04-03 14:24:17 -05:00
|
|
|
<form v-show="!showAuth" @submit.prevent="submit" novalidate class="w-full">
|
2022-07-19 17:32:49 -05:00
|
|
|
<div v-if="serverConnectionConfigs.length" class="flex items-center mb-4" @click="showServerList">
|
|
|
|
<span class="material-icons text-gray-300">arrow_back</span>
|
|
|
|
</div>
|
2022-04-03 14:24:17 -05:00
|
|
|
<h2 class="text-lg leading-7 mb-2">Server address</h2>
|
2022-07-04 12:15:08 -05:00
|
|
|
<ui-text-input v-model="serverConfig.address" :disabled="processing || !networkConnected || !!serverConfig.id" placeholder="http://55.55.55.55:13378" type="url" class="w-full h-10" />
|
2022-07-01 16:33:39 -05:00
|
|
|
<div class="flex justify-end items-center mt-6">
|
|
|
|
<!-- <div class="relative flex">
|
|
|
|
<button class="outline-none uppercase tracking-wide font-semibold text-xs text-gray-300" type="button" @click="addCustomHeaders">Add Custom Headers</button>
|
|
|
|
<div v-if="numCustomHeaders" class="rounded-full h-5 w-5 flex items-center justify-center text-xs bg-success bg-opacity-40 leading-3 font-semibold font-mono ml-1">{{ numCustomHeaders }}</div>
|
|
|
|
</div> -->
|
|
|
|
|
|
|
|
<ui-btn :disabled="processing || !networkConnected" type="submit" :padding-x="3" class="h-10">{{ networkConnected ? 'Submit' : 'No Internet' }}</ui-btn>
|
2022-04-03 14:24:17 -05:00
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
<template v-if="showAuth">
|
|
|
|
<div v-if="serverConfig.id" class="flex items-center mb-4" @click="showServerList">
|
|
|
|
<span class="material-icons text-gray-300">arrow_back</span>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="flex items-center">
|
|
|
|
<p class="text-gray-300">{{ serverConfig.address }}</p>
|
|
|
|
<div class="flex-grow" />
|
|
|
|
<span v-if="!serverConfig.id" class="material-icons" style="font-size: 1.1rem" @click="editServerAddress">edit</span>
|
|
|
|
</div>
|
|
|
|
<div class="w-full h-px bg-white bg-opacity-10 my-2" />
|
2023-09-25 17:30:39 -05:00
|
|
|
<form v-if="isLocalAuthEnabled" @submit.prevent="submitAuth" class="pt-3">
|
2022-04-03 14:24:17 -05:00
|
|
|
<ui-text-input v-model="serverConfig.username" :disabled="processing" placeholder="username" class="w-full mb-2 text-lg" />
|
|
|
|
<ui-text-input v-model="password" type="password" :disabled="processing" placeholder="password" class="w-full mb-2 text-lg" />
|
|
|
|
|
|
|
|
<div class="flex items-center pt-2">
|
|
|
|
<ui-icon-btn v-if="serverConfig.id" small bg-color="error" icon="delete" @click="removeServerConfigClick" />
|
|
|
|
<div class="flex-grow" />
|
|
|
|
<ui-btn :disabled="processing || !networkConnected" type="submit" class="mt-1 h-10">{{ networkConnected ? 'Submit' : 'No Internet' }}</ui-btn>
|
|
|
|
</div>
|
|
|
|
</form>
|
2023-09-25 17:30:39 -05:00
|
|
|
<div v-if="isLocalAuthEnabled && isOpenIDAuthEnabled" class="w-full h-px bg-white bg-opacity-10 my-2" />
|
|
|
|
<ui-btn v-if="isOpenIDAuthEnabled" :disabled="processing" class="mt-1 h-10" @click="clickLoginWithOpenId">Login with OpenId</ui-btn>
|
2022-04-03 14:24:17 -05:00
|
|
|
</template>
|
2022-04-17 16:59:49 -05:00
|
|
|
</div>
|
2022-04-03 14:24:17 -05:00
|
|
|
|
2022-04-17 16:59:49 -05:00
|
|
|
<div v-show="error" class="w-full rounded-lg bg-red-600 bg-opacity-10 border border-error border-opacity-50 py-3 px-2 flex items-center mt-4">
|
|
|
|
<span class="material-icons mr-2 text-error" style="font-size: 1.1rem">warning</span>
|
|
|
|
<p class="text-error">{{ error }}</p>
|
|
|
|
</div>
|
2022-04-03 14:24:17 -05:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<div :class="processing ? 'opacity-100' : 'opacity-0 pointer-events-none'" class="fixed w-full h-full top-0 left-0 bg-black bg-opacity-75 flex items-center justify-center z-30 transition-opacity duration-500">
|
|
|
|
<div>
|
|
|
|
<div class="absolute top-0 left-0 w-full p-6 flex items-center flex-col justify-center z-0 short:hidden">
|
|
|
|
<img src="/Logo.png" class="h-20 w-20 mb-2" />
|
|
|
|
</div>
|
|
|
|
<svg class="animate-spin w-16 h-16" viewBox="0 0 24 24">
|
|
|
|
<path fill="currentColor" d="M12,4V2A10,10 0 0,0 2,12H4A8,8 0 0,1 12,4Z" />
|
|
|
|
</svg>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-07-01 16:33:39 -05:00
|
|
|
|
|
|
|
<modals-custom-headers-modal v-model="showAddCustomHeaders" :custom-headers.sync="serverConfig.customHeaders" />
|
2022-04-03 14:24:17 -05:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2023-11-01 09:30:52 -05:00
|
|
|
import { Browser } from '@capacitor/browser'
|
2023-10-13 23:14:52 +02:00
|
|
|
import { CapacitorHttp } from '@capacitor/core'
|
|
|
|
import { Dialog } from '@capacitor/dialog'
|
2023-10-08 18:26:29 +02:00
|
|
|
|
2022-04-03 14:24:17 -05:00
|
|
|
export default {
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
loggedIn: false,
|
|
|
|
showAuth: false,
|
|
|
|
processing: false,
|
|
|
|
serverConfig: {
|
|
|
|
address: null,
|
2022-07-01 16:33:39 -05:00
|
|
|
username: null,
|
|
|
|
customHeaders: null
|
2022-04-03 14:24:17 -05:00
|
|
|
},
|
|
|
|
password: null,
|
|
|
|
error: null,
|
2022-07-01 16:33:39 -05:00
|
|
|
showForm: false,
|
2023-09-25 17:30:39 -05:00
|
|
|
showAddCustomHeaders: false,
|
|
|
|
authMethods: []
|
2022-04-03 14:24:17 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
2022-07-04 12:15:08 -05:00
|
|
|
deviceData() {
|
|
|
|
return this.$store.state.deviceData || {}
|
|
|
|
},
|
2022-04-03 14:24:17 -05:00
|
|
|
networkConnected() {
|
|
|
|
return this.$store.state.networkConnected
|
|
|
|
},
|
2022-04-03 14:52:30 -05:00
|
|
|
serverConnectionConfigs() {
|
|
|
|
return this.deviceData ? this.deviceData.serverConnectionConfigs || [] : []
|
|
|
|
},
|
2022-04-03 14:24:17 -05:00
|
|
|
lastServerConnectionConfigId() {
|
|
|
|
return this.deviceData ? this.deviceData.lastServerConnectionConfigId : null
|
|
|
|
},
|
|
|
|
lastServerConnectionConfig() {
|
|
|
|
if (!this.lastServerConnectionConfigId || !this.serverConnectionConfigs.length) return null
|
|
|
|
return this.serverConnectionConfigs.find((s) => s.id == this.lastServerConnectionConfigId)
|
2022-07-01 16:33:39 -05:00
|
|
|
},
|
|
|
|
numCustomHeaders() {
|
|
|
|
if (!this.serverConfig.customHeaders) return 0
|
|
|
|
return Object.keys(this.serverConfig.customHeaders).length
|
2023-09-25 17:30:39 -05:00
|
|
|
},
|
|
|
|
isLocalAuthEnabled() {
|
|
|
|
return this.authMethods.includes('local') || !this.authMethods.length
|
|
|
|
},
|
|
|
|
isOpenIDAuthEnabled() {
|
|
|
|
return this.authMethods.includes('openid')
|
2022-04-03 14:24:17 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
2023-11-01 10:16:31 -05:00
|
|
|
async appUrlOpen(url) {
|
|
|
|
if (!url) return
|
|
|
|
|
|
|
|
// Handle the OAuth callback
|
|
|
|
const urlObj = new URL(url)
|
|
|
|
|
|
|
|
// audiobookshelf://oauth?code...
|
|
|
|
// urlObj.hostname for iOS and urlObj.pathname for android
|
|
|
|
if (url.startsWith('audiobookshelf://oauth')) {
|
|
|
|
// Extract oauth2 code to be exchanged for a token
|
|
|
|
const authCode = urlObj.searchParams.get('code')
|
|
|
|
// Extract the state variable
|
|
|
|
const state = urlObj.searchParams.get('state')
|
|
|
|
|
|
|
|
if (authCode) {
|
|
|
|
await this.oauthExchangeCodeForToken(authCode, state)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.warn(`[ServerConnectForm] appUrlOpen: Unknown url: ${url} - host: ${urlObj.hostname} - path: ${urlObj.pathname}`)
|
|
|
|
}
|
|
|
|
},
|
2023-09-25 17:30:39 -05:00
|
|
|
async clickLoginWithOpenId() {
|
2023-10-08 18:26:29 +02:00
|
|
|
// First request that we want to do oauth/openid and get the URL which a browser window should open
|
|
|
|
const redirectUrl = await this.oauthRequest(this.serverConfig.address)
|
|
|
|
|
|
|
|
// Actually we should be able to use the redirectUrl directly for Browser.open below
|
|
|
|
// However it seems that when directly using it there is a malformation and leads to the error
|
|
|
|
// Unhandled Promise Rejection: DataCloneError: The object can not be cloned.
|
|
|
|
// (On calling Browser.open)
|
|
|
|
// Which is hard to debug
|
|
|
|
// So we simply extract the important elements and build the required URL ourselves
|
|
|
|
// which also has the advantage that we can replace the callbackurl with the app url
|
|
|
|
|
|
|
|
const client_id = redirectUrl.searchParams.get('client_id')
|
|
|
|
const scope = redirectUrl.searchParams.get('scope')
|
|
|
|
const state = redirectUrl.searchParams.get('state')
|
|
|
|
|
|
|
|
if (!client_id || !scope || !state) {
|
|
|
|
console.warn(`[SSO] Invalid OpenID URL - client_id scope or state missing: ${redirectUrl}`)
|
|
|
|
this.$toast.error(`SSO: Invalid answer`)
|
|
|
|
return
|
2023-09-25 17:30:39 -05:00
|
|
|
}
|
2023-10-08 18:26:29 +02:00
|
|
|
|
2023-11-01 09:30:52 -05:00
|
|
|
const host = `${redirectUrl.protocol}//${redirectUrl.host}`
|
|
|
|
const buildUrl = `${host}${redirectUrl.pathname}?response_type=code` + `&client_id=${encodeURIComponent(client_id)}&scope=${encodeURIComponent(scope)}&state=${encodeURIComponent(state)}` + `&redirect_uri=${encodeURIComponent('audiobookshelf://oauth')}`
|
2023-10-08 18:26:29 +02:00
|
|
|
|
|
|
|
// example url for authentik
|
2023-11-01 09:30:52 -05:00
|
|
|
// const authURL = "https://authentik/application/o/authorize/?response_type=code&client_id=41cd96f...&redirect_uri=audiobookshelf%3A%2F%2Foauth&scope=openid%20openid%20email%20profile&state=asdds..."
|
2023-10-08 18:26:29 +02:00
|
|
|
|
|
|
|
// Open the browser. The browser/identity provider in turn will redirect to an in-app link supplementing a code
|
|
|
|
try {
|
2023-11-01 09:30:52 -05:00
|
|
|
await Browser.open({ url: buildUrl })
|
2023-10-08 18:26:29 +02:00
|
|
|
} catch (error) {
|
2023-11-01 09:30:52 -05:00
|
|
|
console.error('Error opening browser', error)
|
2023-10-08 18:26:29 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
async oauthRequest(url) {
|
|
|
|
// set parameter isRest to true, so the backend wont attempt a redirect after we call backend:/callback in exchangeCodeForToken
|
|
|
|
// We dont need the callback parameter strictly speaking, but we must provide something or passport will error out as it seems to always expect it
|
|
|
|
const backendEndpoint = `${url}/auth/openid?callback=${encodeURIComponent('/login')}&isRest=true`
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await CapacitorHttp.get({
|
|
|
|
url: backendEndpoint,
|
|
|
|
disableRedirects: true,
|
|
|
|
webFetchExtra: {
|
2023-11-01 09:30:52 -05:00
|
|
|
redirect: 'manual'
|
|
|
|
}
|
2023-09-25 17:30:39 -05:00
|
|
|
})
|
2023-10-08 18:26:29 +02:00
|
|
|
|
2023-10-13 23:14:52 +02:00
|
|
|
// Depending on iOS or Android, it can be location or Location...
|
2023-11-01 09:30:52 -05:00
|
|
|
const locationHeader = response.headers[Object.keys(response.headers).find((key) => key.toLowerCase() === 'location')]
|
2023-10-08 18:26:29 +02:00
|
|
|
if (locationHeader) {
|
|
|
|
const url = new URL(locationHeader)
|
|
|
|
return url
|
|
|
|
} else {
|
|
|
|
console.log('[SSO] No location header in oauthRequest')
|
|
|
|
this.$toast.error(`SSO: Invalid answer`)
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.log('[SSO] Error in oauthRequest: ' + error)
|
|
|
|
this.$toast.error(`SSO error: ${error}`)
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
},
|
|
|
|
async oauthExchangeCodeForToken(code, state) {
|
|
|
|
// We need to read the url directly from this.serverConfig.address as the callback which is called via the external browser does not pass us that info
|
2023-11-01 09:30:52 -05:00
|
|
|
const backendEndpoint = `${this.serverConfig.address}/auth/openid/callback?state=${encodeURIComponent(state)}&code=${encodeURIComponent(code)}`
|
2023-10-08 18:26:29 +02:00
|
|
|
|
|
|
|
try {
|
2023-10-13 23:14:52 +02:00
|
|
|
// We can close the browser at this point (does not work on Android)
|
2023-11-01 10:16:31 -05:00
|
|
|
if (this.$platform === 'ios' || this.$platform === 'web') {
|
2023-10-13 23:14:52 +02:00
|
|
|
await Browser.close()
|
|
|
|
}
|
2023-10-08 18:26:29 +02:00
|
|
|
|
|
|
|
const response = await CapacitorHttp.get({
|
|
|
|
url: backendEndpoint
|
2023-11-01 09:30:52 -05:00
|
|
|
})
|
2023-10-08 18:26:29 +02:00
|
|
|
|
2023-11-01 10:16:31 -05:00
|
|
|
this.serverConfig.token = response.data.user.token
|
|
|
|
const payload = await this.authenticateToken()
|
2023-10-08 18:26:29 +02:00
|
|
|
|
|
|
|
if (!payload) {
|
2023-11-01 09:30:52 -05:00
|
|
|
console.log('[SSO] Failed getting token: ' + this.error)
|
2023-10-08 18:26:29 +02:00
|
|
|
this.$toast.error(`SSO error: ${this.error}`)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-01 10:16:31 -05:00
|
|
|
this.setUserAndConnection(payload)
|
2023-10-08 18:26:29 +02:00
|
|
|
} catch (error) {
|
2023-11-01 09:30:52 -05:00
|
|
|
console.log('[SSO] Error in exchangeCodeForToken: ' + error)
|
2023-10-08 18:26:29 +02:00
|
|
|
this.$toast.error(`SSO error: ${error}`)
|
2023-11-01 09:30:52 -05:00
|
|
|
return null
|
2023-10-08 18:26:29 +02:00
|
|
|
}
|
2023-09-25 17:30:39 -05:00
|
|
|
},
|
2022-07-01 16:33:39 -05:00
|
|
|
addCustomHeaders() {
|
|
|
|
this.showAddCustomHeaders = true
|
|
|
|
},
|
2022-04-03 14:24:17 -05:00
|
|
|
showServerList() {
|
|
|
|
this.showForm = false
|
|
|
|
this.showAuth = false
|
|
|
|
this.error = null
|
|
|
|
this.serverConfig = {
|
|
|
|
address: null,
|
2022-04-09 12:03:37 -05:00
|
|
|
userId: null,
|
2022-04-03 14:24:17 -05:00
|
|
|
username: null
|
|
|
|
}
|
|
|
|
},
|
|
|
|
async connectToServer(config) {
|
2023-01-08 15:32:15 -06:00
|
|
|
await this.$hapticsImpact()
|
2022-04-17 16:59:49 -05:00
|
|
|
console.log('[ServerConnectForm] connectToServer', config.address)
|
2022-04-03 14:24:17 -05:00
|
|
|
this.processing = true
|
|
|
|
this.serverConfig = {
|
|
|
|
...config
|
|
|
|
}
|
|
|
|
this.showForm = true
|
|
|
|
var success = await this.pingServerAddress(config.address)
|
2022-04-17 16:59:49 -05:00
|
|
|
this.processing = false
|
|
|
|
console.log(`[ServerConnectForm] pingServer result ${success}`)
|
2022-04-03 14:24:17 -05:00
|
|
|
if (!success) {
|
2022-04-17 16:59:49 -05:00
|
|
|
this.showForm = false
|
|
|
|
this.showAuth = false
|
|
|
|
console.log(`[ServerConnectForm] showForm ${this.showForm}`)
|
2022-04-03 14:24:17 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
this.error = null
|
|
|
|
var payload = await this.authenticateToken()
|
|
|
|
|
|
|
|
if (payload) {
|
2022-07-19 18:50:14 -05:00
|
|
|
this.setUserAndConnection(payload)
|
2022-04-03 14:24:17 -05:00
|
|
|
} else {
|
|
|
|
this.showAuth = true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
async removeServerConfigClick() {
|
|
|
|
if (!this.serverConfig.id) return
|
2023-01-08 15:32:15 -06:00
|
|
|
await this.$hapticsImpact()
|
2022-04-03 14:24:17 -05:00
|
|
|
|
|
|
|
const { value } = await Dialog.confirm({
|
|
|
|
title: 'Confirm',
|
|
|
|
message: `Remove this server config?`
|
|
|
|
})
|
|
|
|
if (value) {
|
|
|
|
this.processing = true
|
|
|
|
await this.$db.removeServerConnectionConfig(this.serverConfig.id)
|
2022-07-04 12:15:08 -05:00
|
|
|
const updatedDeviceData = { ...this.deviceData }
|
|
|
|
updatedDeviceData.serverConnectionConfigs = this.deviceData.serverConnectionConfigs.filter((scc) => scc.id != this.serverConfig.id)
|
|
|
|
this.$store.commit('setDeviceData', updatedDeviceData)
|
|
|
|
|
2022-04-03 14:24:17 -05:00
|
|
|
this.serverConfig = {
|
|
|
|
address: null,
|
2022-04-09 12:03:37 -05:00
|
|
|
userId: null,
|
2022-04-03 14:24:17 -05:00
|
|
|
username: null
|
|
|
|
}
|
|
|
|
this.password = null
|
|
|
|
this.processing = false
|
|
|
|
this.showAuth = false
|
|
|
|
this.showForm = !this.serverConnectionConfigs.length
|
|
|
|
}
|
|
|
|
},
|
|
|
|
editServerConfig(serverConfig) {
|
|
|
|
this.serverConfig = {
|
|
|
|
...serverConfig
|
|
|
|
}
|
|
|
|
this.showForm = true
|
|
|
|
this.showAuth = true
|
|
|
|
},
|
2022-12-08 00:28:28 -05:00
|
|
|
async newServerConfigClick() {
|
2023-01-08 15:32:15 -06:00
|
|
|
await this.$hapticsImpact()
|
2022-04-17 16:59:49 -05:00
|
|
|
this.serverConfig = {
|
|
|
|
address: '',
|
|
|
|
userId: '',
|
|
|
|
username: ''
|
|
|
|
}
|
2022-04-03 14:24:17 -05:00
|
|
|
this.showForm = true
|
|
|
|
this.showAuth = false
|
2022-04-17 16:59:49 -05:00
|
|
|
this.error = null
|
2022-04-03 14:24:17 -05:00
|
|
|
},
|
|
|
|
editServerAddress() {
|
|
|
|
this.error = null
|
|
|
|
this.showAuth = false
|
|
|
|
},
|
|
|
|
validateServerUrl(url) {
|
|
|
|
try {
|
|
|
|
var urlObject = new URL(url)
|
|
|
|
var address = `${urlObject.protocol}//${urlObject.hostname}`
|
|
|
|
if (urlObject.port) address += ':' + urlObject.port
|
|
|
|
return address
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Invalid URL', error)
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
},
|
2023-09-15 17:35:44 -05:00
|
|
|
async getRequest(url, headers, connectTimeout = 6000) {
|
|
|
|
const options = {
|
|
|
|
url,
|
|
|
|
headers,
|
|
|
|
connectTimeout
|
|
|
|
}
|
|
|
|
const response = await CapacitorHttp.get(options)
|
|
|
|
console.log('[ServerConnectForm] GET request response', response)
|
|
|
|
if (response.status >= 400) {
|
|
|
|
throw new Error(response.data)
|
|
|
|
} else {
|
|
|
|
return response.data
|
|
|
|
}
|
|
|
|
},
|
|
|
|
async postRequest(url, data, headers, connectTimeout = 6000) {
|
|
|
|
if (!headers) headers = {}
|
|
|
|
if (!headers['Content-Type'] && data) {
|
|
|
|
headers['Content-Type'] = 'application/json'
|
2022-07-01 16:33:39 -05:00
|
|
|
}
|
2023-09-15 17:35:44 -05:00
|
|
|
const options = {
|
|
|
|
url,
|
|
|
|
headers,
|
|
|
|
data,
|
|
|
|
connectTimeout
|
|
|
|
}
|
|
|
|
const response = await CapacitorHttp.post(options)
|
|
|
|
console.log('[ServerConnectForm] POST request response', response)
|
|
|
|
if (response.status >= 400) {
|
|
|
|
throw new Error(response.data)
|
|
|
|
} else {
|
|
|
|
return response.data
|
|
|
|
}
|
|
|
|
},
|
2023-09-25 17:30:39 -05:00
|
|
|
/**
|
|
|
|
* Get request to server /status api endpoint
|
|
|
|
*
|
|
|
|
* @param {string} address
|
|
|
|
* @returns {Promise<{isInit:boolean, language:string, authMethods:string[]}>}
|
|
|
|
*/
|
|
|
|
getServerAddressStatus(address) {
|
|
|
|
return this.getRequest(`${address}/status`).catch((error) => {
|
|
|
|
console.error('Failed to get server status', error)
|
|
|
|
const errorMsg = error.message || error
|
|
|
|
this.error = 'Failed to ping server'
|
|
|
|
if (typeof errorMsg === 'string') {
|
|
|
|
this.error += ` (${errorMsg})`
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
})
|
|
|
|
},
|
2023-09-15 17:35:44 -05:00
|
|
|
pingServerAddress(address, customHeaders) {
|
|
|
|
return this.getRequest(`${address}/ping`, customHeaders)
|
|
|
|
.then((data) => {
|
|
|
|
return data.success
|
|
|
|
})
|
2022-04-03 14:24:17 -05:00
|
|
|
.catch((error) => {
|
2023-09-15 17:35:44 -05:00
|
|
|
console.error('Server ping failed', error)
|
|
|
|
const errorMsg = error.message || error
|
2022-04-03 14:24:17 -05:00
|
|
|
this.error = 'Failed to ping server'
|
2023-09-15 17:35:44 -05:00
|
|
|
if (typeof errorMsg === 'string') {
|
|
|
|
this.error += ` (${errorMsg})`
|
|
|
|
}
|
|
|
|
|
2022-04-03 14:24:17 -05:00
|
|
|
return false
|
|
|
|
})
|
|
|
|
},
|
|
|
|
requestServerLogin() {
|
2023-09-15 17:35:44 -05:00
|
|
|
return this.postRequest(`${this.serverConfig.address}/login`, { username: this.serverConfig.username, password: this.password }, this.serverConfig.customHeaders, 20000)
|
2022-04-03 14:24:17 -05:00
|
|
|
.then((data) => {
|
|
|
|
if (!data.user) {
|
|
|
|
console.error(data.error)
|
|
|
|
this.error = data.error || 'Unknown Error'
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return data
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.error('Server auth failed', error)
|
2023-09-15 17:35:44 -05:00
|
|
|
const errorMsg = error.message || error
|
|
|
|
this.error = 'Failed to login'
|
|
|
|
if (typeof errorMsg === 'string') {
|
|
|
|
this.error += ` (${errorMsg})`
|
|
|
|
}
|
2022-04-03 14:24:17 -05:00
|
|
|
return false
|
|
|
|
})
|
|
|
|
},
|
|
|
|
async submit() {
|
|
|
|
if (!this.networkConnected) return
|
|
|
|
if (!this.serverConfig.address) return
|
|
|
|
if (!this.serverConfig.address.startsWith('http')) {
|
|
|
|
this.serverConfig.address = 'http://' + this.serverConfig.address
|
|
|
|
}
|
|
|
|
var validServerAddress = this.validateServerUrl(this.serverConfig.address)
|
|
|
|
if (!validServerAddress) {
|
|
|
|
this.error = 'Invalid server address'
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
this.serverConfig.address = validServerAddress
|
|
|
|
this.processing = true
|
|
|
|
this.error = null
|
2023-09-25 17:30:39 -05:00
|
|
|
this.authMethods = []
|
2022-04-03 14:24:17 -05:00
|
|
|
|
2023-09-25 17:30:39 -05:00
|
|
|
const statusData = await this.getServerAddressStatus(this.serverConfig.address)
|
2022-04-03 14:24:17 -05:00
|
|
|
this.processing = false
|
2023-09-25 17:30:39 -05:00
|
|
|
if (statusData) {
|
|
|
|
if (!statusData.isInit) {
|
|
|
|
this.error = 'Server is not initialized'
|
|
|
|
} else {
|
|
|
|
this.showAuth = true
|
|
|
|
this.authMethods = statusData.authMethods || []
|
|
|
|
}
|
|
|
|
}
|
2022-04-03 14:24:17 -05:00
|
|
|
},
|
|
|
|
async submitAuth() {
|
|
|
|
if (!this.networkConnected) return
|
|
|
|
if (!this.serverConfig.username) {
|
|
|
|
this.error = 'Invalid username'
|
|
|
|
return
|
|
|
|
}
|
2022-07-19 17:32:49 -05:00
|
|
|
|
|
|
|
const duplicateConfig = this.serverConnectionConfigs.find((scc) => scc.address === this.serverConfig.address && scc.username === this.serverConfig.username && this.serverConfig.id !== scc.id)
|
2022-07-04 12:15:08 -05:00
|
|
|
if (duplicateConfig) {
|
|
|
|
this.error = 'Config already exists for this address and username'
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-03 14:24:17 -05:00
|
|
|
this.error = null
|
|
|
|
this.processing = true
|
|
|
|
|
|
|
|
var payload = await this.requestServerLogin()
|
|
|
|
this.processing = false
|
|
|
|
if (payload) {
|
2022-07-19 16:40:50 -05:00
|
|
|
this.setUserAndConnection(payload)
|
2022-04-03 14:24:17 -05:00
|
|
|
}
|
|
|
|
},
|
2022-07-19 16:40:50 -05:00
|
|
|
async setUserAndConnection({ user, userDefaultLibraryId, serverSettings }) {
|
2022-04-10 20:31:47 -05:00
|
|
|
if (!user) return
|
2022-04-03 14:24:17 -05:00
|
|
|
|
2022-04-10 20:31:47 -05:00
|
|
|
console.log('Successfully logged in', JSON.stringify(user))
|
|
|
|
|
2022-07-19 17:32:49 -05:00
|
|
|
this.$store.commit('setServerSettings', serverSettings)
|
2022-07-19 16:40:50 -05:00
|
|
|
|
2022-04-10 20:31:47 -05:00
|
|
|
// Set library - Use last library if set and available fallback to default user library
|
|
|
|
var lastLibraryId = await this.$localStore.getLastLibraryId()
|
|
|
|
if (lastLibraryId && (!user.librariesAccessible.length || user.librariesAccessible.includes(lastLibraryId))) {
|
|
|
|
this.$store.commit('libraries/setCurrentLibrary', lastLibraryId)
|
|
|
|
} else if (userDefaultLibraryId) {
|
|
|
|
this.$store.commit('libraries/setCurrentLibrary', userDefaultLibraryId)
|
|
|
|
}
|
2022-04-03 14:24:17 -05:00
|
|
|
|
2022-04-10 20:31:47 -05:00
|
|
|
this.serverConfig.userId = user.id
|
|
|
|
this.serverConfig.token = user.token
|
2023-09-25 17:30:39 -05:00
|
|
|
this.serverConfig.username = user.username
|
2022-04-03 14:24:17 -05:00
|
|
|
|
2022-04-10 20:31:47 -05:00
|
|
|
var serverConnectionConfig = await this.$db.setServerConnectionConfig(this.serverConfig)
|
2022-04-03 14:24:17 -05:00
|
|
|
|
2022-04-10 20:31:47 -05:00
|
|
|
this.$store.commit('user/setUser', user)
|
|
|
|
this.$store.commit('user/setServerConnectionConfig', serverConnectionConfig)
|
2022-04-03 14:24:17 -05:00
|
|
|
|
2022-04-10 20:31:47 -05:00
|
|
|
this.$socket.connect(this.serverConfig.address, this.serverConfig.token)
|
|
|
|
this.$router.replace('/bookshelf')
|
2022-04-03 14:24:17 -05:00
|
|
|
},
|
|
|
|
async authenticateToken() {
|
|
|
|
if (!this.networkConnected) return
|
|
|
|
if (!this.serverConfig.token) {
|
|
|
|
this.error = 'No token'
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
this.error = null
|
|
|
|
this.processing = true
|
2023-09-15 17:35:44 -05:00
|
|
|
|
|
|
|
const authRes = await this.postRequest(`${this.serverConfig.address}/api/authorize`, null, { Authorization: `Bearer ${this.serverConfig.token}` }).catch((error) => {
|
|
|
|
console.error('[ServerConnectForm] Server auth failed', error)
|
|
|
|
const errorMsg = error.message || error
|
|
|
|
this.error = 'Failed to authorize'
|
|
|
|
if (typeof errorMsg === 'string') {
|
|
|
|
this.error += ` (${errorMsg})`
|
|
|
|
}
|
2022-04-03 14:24:17 -05:00
|
|
|
return false
|
|
|
|
})
|
2023-09-15 17:35:44 -05:00
|
|
|
|
|
|
|
console.log('[ServerConnectForm] authRes=', authRes)
|
|
|
|
|
2022-04-03 14:24:17 -05:00
|
|
|
this.processing = false
|
|
|
|
return authRes
|
|
|
|
},
|
2023-11-01 10:16:31 -05:00
|
|
|
init() {
|
2022-04-03 14:24:17 -05:00
|
|
|
if (this.lastServerConnectionConfig) {
|
|
|
|
this.connectToServer(this.lastServerConnectionConfig)
|
|
|
|
} else {
|
|
|
|
this.showForm = !this.serverConnectionConfigs.length
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
mounted() {
|
2023-11-01 10:16:31 -05:00
|
|
|
this.$eventBus.$on('url-open', this.appUrlOpen)
|
2022-04-03 14:24:17 -05:00
|
|
|
this.init()
|
2023-11-01 10:16:31 -05:00
|
|
|
},
|
|
|
|
beforeDestroy() {
|
|
|
|
this.$eventBus.$off('url-open', this.appUrlOpen)
|
2022-04-03 14:24:17 -05:00
|
|
|
}
|
|
|
|
}
|
2022-12-08 00:28:28 -05:00
|
|
|
</script>
|