From a1302467173181788605ccac33fa479b195647e0 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 6 Jan 2017 11:07:42 +0900 Subject: [PATCH] Refactor: Extract isNativeToken method --- src/api/authenticate.ts | 3 ++- src/api/common/is-native-token.ts | 1 + src/api/streaming.ts | 3 ++- 3 files changed, 5 insertions(+), 2 deletions(-) create mode 100644 src/api/common/is-native-token.ts diff --git a/src/api/authenticate.ts b/src/api/authenticate.ts index 16a7ee1b4..832517379 100644 --- a/src/api/authenticate.ts +++ b/src/api/authenticate.ts @@ -2,6 +2,7 @@ import * as express from 'express'; import App from './models/app'; import User from './models/user'; import Userkey from './models/userkey'; +import isNativeToken from './common/is-native-token'; export interface IAuthContext { /** @@ -27,7 +28,7 @@ export default (req: express.Request) => new Promise(async (resolv return resolve({ app: null, user: null, isSecure: false }); } - if (token[0] == '!') { + if (isNativeToken(token)) { const user = await User .findOne({ token: token }); diff --git a/src/api/common/is-native-token.ts b/src/api/common/is-native-token.ts new file mode 100644 index 000000000..0769a4812 --- /dev/null +++ b/src/api/common/is-native-token.ts @@ -0,0 +1 @@ +export default (token: string) => token[0] == '!'; diff --git a/src/api/streaming.ts b/src/api/streaming.ts index 93d5f217b..84a0f9ddf 100644 --- a/src/api/streaming.ts +++ b/src/api/streaming.ts @@ -3,6 +3,7 @@ import * as websocket from 'websocket'; import * as redis from 'redis'; import User from './models/user'; import Userkey from './models/userkey'; +import isNativeToken from './common/is-native-token'; import homeStream from './stream/home'; import messagingStream from './stream/messaging'; @@ -50,7 +51,7 @@ module.exports = (server: http.Server) => { function authenticate(connection: websocket.connection, token: string): Promise { return new Promise(async (resolve, reject) => { - if (token[0] == '!') { + if (isNativeToken(token)) { // Fetch user // SELECT _id const user = await User