diff --git a/.gitignore b/.gitignore index 47ccf0f..56814c7 100644 --- a/.gitignore +++ b/.gitignore @@ -2,8 +2,6 @@ node_modules/ coverage/ dist/ scratch/ -mobile/android/.idea/ -mobile/ios/**/xcuserdata/ .DS_Store npm-debug.log* yarn-debug.log* diff --git a/mobile/.gitignore b/mobile/.gitignore index 0374824..914ff82 100644 --- a/mobile/.gitignore +++ b/mobile/.gitignore @@ -1,6 +1,9 @@ +local.js ios/build/ ios/Pods/ ios/DerivedData/ +ios/**/xcuserdata/ +android/.idea/ android/**/build/** android/**/.gradle/** **/.idea/** diff --git a/mobile/ios/DeightonAR/Info.plist b/mobile/ios/DeightonAR/Info.plist index 890b502..c42738b 100644 --- a/mobile/ios/DeightonAR/Info.plist +++ b/mobile/ios/DeightonAR/Info.plist @@ -21,7 +21,7 @@ CFBundleSignature ???? CFBundleVersion - 20180410.0 + 20180413.0 LSRequiresIPhoneOS NSAppTransportSecurity diff --git a/mobile/src/API.js b/mobile/src/API.js index 156a0c0..a5724f6 100644 --- a/mobile/src/API.js +++ b/mobile/src/API.js @@ -1,15 +1,11 @@ import EventEmitter from "eventemitter3" import io from "socket.io-client" import { AsyncStorage } from "react-native" +import autobind from "autobind-decorator" +import { local } from "./local" const authTokenKeyName = "AuthToken" -const backendKeyName = "BackendName" - -// if (__DEV__) { -// const localIPAddr = process.env.LOCAL_IP_ADDR -// baseURL = `http://${localIPAddr || "localhost"}:3001` -// apiPath = "" -// } +const backendKeyName = "Backend" class NetworkError extends Error { constructor(message) { @@ -36,50 +32,71 @@ class APIError extends Error { } } +@autobind class API extends EventEmitter { + static apiPath = "/api" + constructor() { super() this.user = { pending: true } - // AsyncStorage.getItem(backendKeyName) - // .then((backend) => { - // this.backend = backend - // }) - // .catch((err) => { - // this.backend = "normal" - // AsyncStorage.setItem(backendKeyName, this.backend) - // }) + const baseURLs = { + normal: "https://dar.kss.us.com", + test: "https://dar-test.kss.us.com", + development: `http://${local.ipAddr}:3001`, + } + this.baseURL = null - this.backendName = "normal" - this.baseURL = "https://dar.kss.us.com" - this.apiPath = "/api" + const checkForToken = () => { + console.log(`${this.backendName} - ${this.baseURL}`) + AsyncStorage.getItem(authTokenKeyName) + .then((token) => { + if (!token) { + return Promise.reject() + } - AsyncStorage.getItem(authTokenKeyName) - .then((token) => { - if (!token) { + this.token = token + return this.who() + }) + .then((user) => { + console.log("token good") + this.user = user + this.connectSocket() + this.emit("login") + }) + .catch(() => { + console.log("token not present") + AsyncStorage.removeItem(authTokenKeyName) + this.token = null + this.user = {} + this.socket = null + this.emit("logout") + }) + } + + AsyncStorage.getItem(backendKeyName) + .then((backendName) => { + this.backendName = backendName + this.baseURL = baseURLs[backendName] + + if (!this.baseURL) { return Promise.reject() } - - this.token = token - return this.who() + console.log("setting backend") + checkForToken() }) - .then((user) => { - this.user = user - this.connectSocket() - this.emit("login") - }) - .catch((err) => { - AsyncStorage.removeItem(authTokenKeyName) - this.token = null - this.user = {} - this.socket = null - this.emit("logout") + .catch(() => { + this.backendName = "normal" + this.baseURL = baseURLS[this.backendName] + AsyncStorage.setItem(backendKeyName, this.backendName) + console.log("setting default backend") + checkForToken() }) } connectSocket() { this.socket = io(this.baseURL, { - path: this.apiPath + "/socketio", + path: API.apiPath + "/socketio", query: { auth_token: this.token, }, @@ -117,11 +134,21 @@ class API extends EventEmitter { return this.backendName } + set backend(value) { + if (this.backendName !== value) { + this.backendName = value + AsyncStorage.setItem(backendKeyName, this.backendName).then( + this.logout, + this.logout + ) + } + } + makeImageUrl(id, size) { if (id) { - return this.apiPath + "/assets/" + id + "?access_token=" + this.token + return API.apiPath + "/assets/" + id + "?access_token=" + this.token } else if (size && size.width && size.height) { - return `${this.apiPath}/placeholders/${size.width}x${ + return `${API.apiPath}/placeholders/${size.width}x${ size.height }?access_token=${this.token}` } else { @@ -131,7 +158,7 @@ class API extends EventEmitter { makeAssetUrl(id) { return id - ? this.apiPath + "/assets/" + id + "?access_token=" + this.token + ? API.apiPath + "/assets/" + id + "?access_token=" + this.token : null } @@ -168,7 +195,16 @@ class API extends EventEmitter { } } fetchOptions.headers = headers - fetch(this.baseURL + this.apiPath + path, fetchOptions) + + if (!this.baseURL) { + tconsole.error("no baseURL!") + return reject(new Error("API layer not ready")) + } + + console.log( + `${fetchOptions.method} - ${this.baseURL + API.apiPath + path}` + ) + fetch(this.baseURL + API.apiPath + path, fetchOptions) .then((res) => { return Promise.all([ Promise.resolve(res), diff --git a/mobile/src/version.js b/mobile/src/version.js index e9911c7..5ba431a 100644 --- a/mobile/src/version.js +++ b/mobile/src/version.js @@ -1,6 +1,6 @@ export const versionInfo = { version: '1.0.0', - fullVersion: '1.0.0-20180410.0', + fullVersion: '1.0.0-20180413.0', title: 'Deighton AR System', copyright: '© 2018, Kingston Software Solutions.', supportEmail: 'support@kss.us.com', diff --git a/server/src/api/routes/AuthRoutes.js b/server/src/api/routes/AuthRoutes.js index 3365347..5b84598 100644 --- a/server/src/api/routes/AuthRoutes.js +++ b/server/src/api/routes/AuthRoutes.js @@ -79,7 +79,7 @@ export class AuthRoutes { let User = this.db.User if (!email || !password) { - createError.BadRequest("Must supply user name and password") + throw createError.BadRequest("Must supply user name and password") } // Lookup the user diff --git a/server/src/api/routes/index.js b/server/src/api/routes/index.js index 3ff7008..cb3741a 100644 --- a/server/src/api/routes/index.js +++ b/server/src/api/routes/index.js @@ -7,9 +7,9 @@ export { TeamRoutes } from "./TeamRoutes" import createError from "http-errors" export function catchAll(routeHandler) { - return (req, res, next) => { + return async (req, res, next) => { try { - routeHandler(req, res, next) + await routeHandler(req, res, next) } catch (err) { if (err instanceof createError.HttpError) { next(err) diff --git a/version.json5 b/version.json5 index 7e21746..96d7505 100644 --- a/version.json5 +++ b/version.json5 @@ -15,7 +15,7 @@ major: 1, minor: 0, patch: 0, - build: 20180410, + build: 20180413, revision: 0, sequence: 1, tz: "America/Los_Angeles", diff --git a/website/src/version.js b/website/src/version.js index e9911c7..5ba431a 100644 --- a/website/src/version.js +++ b/website/src/version.js @@ -1,6 +1,6 @@ export const versionInfo = { version: '1.0.0', - fullVersion: '1.0.0-20180410.0', + fullVersion: '1.0.0-20180413.0', title: 'Deighton AR System', copyright: '© 2018, Kingston Software Solutions.', supportEmail: 'support@kss.us.com',