diff --git a/src/App.vue b/src/App.vue
index a7ee551..147bb03 100644
--- a/src/App.vue
+++ b/src/App.vue
@@ -42,7 +42,6 @@
@click="hideMenu"
>
-
{
- const result = await couchService.remote.allDocs()
- return result.rows
- }
-}
-
-export default new AdminService()
diff --git a/src/services/CouchService.ts b/src/services/CouchService.ts
index 0ea76a4..60fa625 100644
--- a/src/services/CouchService.ts
+++ b/src/services/CouchService.ts
@@ -28,11 +28,18 @@ const remoteConfig = {
const LOCALE_DB: string = 'LOCALE_DB'
const REMOTE: string = 'https://juliencalixte.ddns.net/database'
+const initRemote = () => {
+ return new Promise((resolve, reject) => {
+ try {
+ resolve(new PouchDb(`${REMOTE}/vaquant`, remoteConfig))
+ } catch (error) {
+ reject(error)
+ }
+ })
+}
+
class CouchService {
- public remote: PouchDB.Database = new PouchDb(
- `${REMOTE}/vaquant`,
- remoteConfig
- )
+ public remote: PouchDB.Database | null = null
public db: PouchDB.Database | null = new PouchDb(LOCALE_DB)
public remoteUserDb: PouchDB.Database | null = null
public userDb: PouchDB.Database | null = null
@@ -53,6 +60,13 @@ class CouchService {
this.cancelSync()
})
}
+ this.initRemote()
+ }
+
+ public async initRemote() {
+ if (!this.remote) {
+ this.remote = await initRemote()
+ }
}
public async initLive(): Promise {
@@ -68,11 +82,19 @@ class CouchService {
})
.on('change', (result: PouchDB.Replication.SyncResult) => {
if (result.direction === 'pull') {
- emit(SYNC, result.change.docs.map((doc: any) => doc._id))
+ emit(
+ SYNC,
+ result.change.docs.map((doc: any) => doc._id)
+ )
}
})
}
+ if (!this.remote) {
+ await this.initRemote()
+ return false
+ }
+
this.sync = this.db
.sync(this.remote, {
live: true,
diff --git a/src/services/UserService.ts b/src/services/UserService.ts
index 8a5acc8..d78d01e 100644
--- a/src/services/UserService.ts
+++ b/src/services/UserService.ts
@@ -4,6 +4,12 @@ import IResponse from '@/models/IResponse'
class UserService {
public async signup(user: IUser, password: string): Promise {
+ if (!couchService.remote) {
+ return {
+ ok: false,
+ message: 'no remote'
+ }
+ }
try {
const response = await couchService.remote.signUp(user.userId, password, {
metadata: user
@@ -24,6 +30,12 @@ class UserService {
}
public async login(userId: string, password: string): Promise {
+ if (!couchService.remote) {
+ return {
+ ok: false,
+ message: 'no remote'
+ }
+ }
try {
const response = await couchService.remote.logIn(userId, password)
return {
@@ -45,6 +57,12 @@ class UserService {
}
public async logout(): Promise {
+ if (!couchService.remote) {
+ return {
+ ok: false,
+ message: 'no remote'
+ }
+ }
try {
const { ok } = await couchService.remote.logOut()
return {
@@ -59,6 +77,12 @@ class UserService {
}
public async deleteUser(userId: string): Promise {
+ if (!couchService.remote) {
+ return {
+ ok: false,
+ message: 'no remote'
+ }
+ }
try {
const { ok } = await couchService.remote.deleteUser(userId)
return {
@@ -72,6 +96,9 @@ class UserService {
}
public async getUser(current: IUser | null): Promise {
+ if (!couchService.remote) {
+ return null
+ }
try {
const session = await couchService.remote.getSession()
if (session.ok && session.userCtx && session.userCtx.name) {
@@ -91,6 +118,9 @@ class UserService {
}
public async updateUser(user: IUser): Promise {
+ if (!couchService.remote) {
+ return false
+ }
try {
const result = await couchService.remote.putUser(user.userId, {
metadata: user