From 3629c5730ab2b5537c6faffd6f0b49f53f45d1b1 Mon Sep 17 00:00:00 2001 From: Arnaud Vergnet Date: Sun, 2 Aug 2020 19:53:05 +0200 Subject: [PATCH] Improve connection manager to match linter --- src/managers/ConnectionManager.js | 297 +++++++++++++++--------------- 1 file changed, 152 insertions(+), 145 deletions(-) diff --git a/src/managers/ConnectionManager.js b/src/managers/ConnectionManager.js index b6d24fa..17fb13a 100644 --- a/src/managers/ConnectionManager.js +++ b/src/managers/ConnectionManager.js @@ -1,7 +1,8 @@ // @flow import * as Keychain from 'react-native-keychain'; -import {apiRequest, ERROR_TYPE} from "../utils/WebData"; +import type {ApiDataLoginType, ApiGenericDataType} from '../utils/WebData'; +import {apiRequest, ERROR_TYPE} from '../utils/WebData'; /** * champ: error @@ -14,161 +15,167 @@ import {apiRequest, ERROR_TYPE} from "../utils/WebData"; * 500 : SERVER_ERROR -> pb coté serveur */ -const SERVER_NAME = "amicale-insat.fr"; -const AUTH_PATH = "password"; +const SERVER_NAME = 'amicale-insat.fr'; +const AUTH_PATH = 'password'; export default class ConnectionManager { - static instance: ConnectionManager | null = null; + static instance: ConnectionManager | null = null; - #email: string; - #token: string | null; + #email: string; - constructor() { - this.#token = null; - } + #token: string | null; - /** - * Gets this class instance or create one if none is found - * - * @returns {ConnectionManager} - */ - static getInstance(): ConnectionManager { - return ConnectionManager.instance === null ? - ConnectionManager.instance = new ConnectionManager() : - ConnectionManager.instance; - } + constructor() { + this.#token = null; + } - /** - * Gets the current token - * - * @returns {string | null} - */ - getToken(): string | null { - return this.#token; - } + /** + * Gets this class instance or create one if none is found + * + * @returns {ConnectionManager} + */ + static getInstance(): ConnectionManager { + if (ConnectionManager.instance == null) + ConnectionManager.instance = new ConnectionManager(); + return ConnectionManager.instance; + } - /** - * Tries to recover login token from the secure keychain - * - * @returns {Promise} - */ - async recoverLogin() { - return new Promise((resolve, reject) => { - if (this.getToken() !== null) - resolve(this.getToken()); - else { - Keychain.getInternetCredentials(SERVER_NAME) - .then((data) => { - if (data) { - this.#token = data.password; - resolve(this.#token); - } else - reject(false); - }) - .catch(() => { - reject(false); - }); - } - }); - } + /** + * Gets the current token + * + * @returns {string | null} + */ + getToken(): string | null { + return this.#token; + } - /** - * Check if the user has a valid token - * - * @returns {boolean} - */ - isLoggedIn() { - return this.getToken() !== null; - } + /** + * Tries to recover login token from the secure keychain + * + * @returns Promise + */ + async recoverLogin(): Promise { + return new Promise( + (resolve: (token: string) => void, reject: () => void) => { + const token = this.getToken(); + if (token != null) resolve(token); + else { + Keychain.getInternetCredentials(SERVER_NAME) + .then((data: Keychain.UserCredentials | false) => { + if ( + data != null && + data.password != null && + typeof data.password === 'string' + ) { + this.#token = data.password; + resolve(this.#token); + } else reject(); + }) + .catch((): void => reject()); + } + }, + ); + } - /** - * Saves the login token in the secure keychain - * - * @param email - * @param token - * @returns {Promise} - */ - async saveLogin(email: string, token: string) { - return new Promise((resolve, reject) => { - Keychain.setInternetCredentials(SERVER_NAME, 'token', token) - .then(() => { - this.#token = token; - this.#email = email; - resolve(true); - }) - .catch(() => { - reject(false); - }); - }); - } + /** + * Check if the user has a valid token + * + * @returns {boolean} + */ + isLoggedIn(): boolean { + return this.getToken() !== null; + } - /** - * Deletes the login token from the keychain - * - * @returns {Promise} - */ - async disconnect() { - return new Promise((resolve, reject) => { - Keychain.resetInternetCredentials(SERVER_NAME) - .then(() => { - this.#token = null; - resolve(true); - }) - .catch(() => { - reject(false); - }); - }); - } + /** + * Saves the login token in the secure keychain + * + * @param email + * @param token + * @returns Promise + */ + async saveLogin(email: string, token: string): Promise { + return new Promise((resolve: () => void, reject: () => void) => { + Keychain.setInternetCredentials(SERVER_NAME, 'token', token) + .then(() => { + this.#token = token; + this.#email = email; + resolve(); + }) + .catch((): void => reject()); + }); + } + /** + * Deletes the login token from the keychain + * + * @returns Promise + */ + async disconnect(): Promise { + return new Promise((resolve: () => void, reject: () => void) => { + Keychain.resetInternetCredentials(SERVER_NAME) + .then(() => { + this.#token = null; + resolve(); + }) + .catch((): void => reject()); + }); + } - /** - * Sends the given login and password to the api. - * If the combination is valid, the login token is received and saved in the secure keychain. - * If not, the promise is rejected with the corresponding error code. - * - * @param email - * @param password - * @returns {Promise} - */ - async connect(email: string, password: string) { - return new Promise((resolve, reject) => { - const data = { - email: email, - password: password, - }; - apiRequest(AUTH_PATH, 'POST', data) - .then((response) => { - this.saveLogin(email, response.token) - .then(() => { - resolve(true); - }) - .catch(() => { - reject(ERROR_TYPE.TOKEN_SAVE); - }); - }) - .catch((error) => reject(error)); - }); - } + /** + * Sends the given login and password to the api. + * If the combination is valid, the login token is received and saved in the secure keychain. + * If not, the promise is rejected with the corresponding error code. + * + * @param email + * @param password + * @returns Promise + */ + async connect(email: string, password: string): Promise { + return new Promise( + (resolve: () => void, reject: (error: number) => void) => { + const data = { + email, + password, + }; + apiRequest(AUTH_PATH, 'POST', data) + .then((response: ApiDataLoginType) => { + if (response.token != null) { + this.saveLogin(email, response.token) + .then((): void => resolve()) + .catch((): void => reject(ERROR_TYPE.TOKEN_SAVE)); + } else reject(ERROR_TYPE.SERVER_ERROR); + }) + .catch((error: number): void => reject(error)); + }, + ); + } - /** - * Sends an authenticated request with the login token to the API - * - * @param path - * @param params - * @returns {Promise} - */ - async authenticatedRequest(path: string, params: Object) { - return new Promise((resolve, reject) => { - if (this.getToken() !== null) { - let data = { - token: this.getToken(), - ...params - }; - apiRequest(path, 'POST', data) - .then((response) => resolve(response)) - .catch((error) => reject(error)); - } else - reject(ERROR_TYPE.TOKEN_RETRIEVE); - }); - } + /** + * Sends an authenticated request with the login token to the API + * + * @param path + * @param params + * @returns Promise + */ + async authenticatedRequest( + path: string, + params: {...}, + ): Promise { + return new Promise( + ( + resolve: (response: ApiGenericDataType) => void, + reject: (error: number) => void, + ) => { + if (this.getToken() !== null) { + const data = { + ...params, + token: this.getToken(), + }; + apiRequest(path, 'POST', data) + .then((response: ApiGenericDataType): void => resolve(response)) + .catch((error: number): void => reject(error)); + } else reject(ERROR_TYPE.TOKEN_RETRIEVE); + }, + ); + } }