Improve connection manager to match linter

This commit is contained in:
Arnaud Vergnet 2020-08-02 19:53:05 +02:00
parent 3d9bfdea4c
commit 3629c5730a

View file

@ -1,7 +1,8 @@
// @flow // @flow
import * as Keychain from 'react-native-keychain'; 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 * champ: error
@ -14,13 +15,14 @@ import {apiRequest, ERROR_TYPE} from "../utils/WebData";
* 500 : SERVER_ERROR -> pb coté serveur * 500 : SERVER_ERROR -> pb coté serveur
*/ */
const SERVER_NAME = "amicale-insat.fr"; const SERVER_NAME = 'amicale-insat.fr';
const AUTH_PATH = "password"; const AUTH_PATH = 'password';
export default class ConnectionManager { export default class ConnectionManager {
static instance: ConnectionManager | null = null; static instance: ConnectionManager | null = null;
#email: string; #email: string;
#token: string | null; #token: string | null;
constructor() { constructor() {
@ -33,9 +35,9 @@ export default class ConnectionManager {
* @returns {ConnectionManager} * @returns {ConnectionManager}
*/ */
static getInstance(): ConnectionManager { static getInstance(): ConnectionManager {
return ConnectionManager.instance === null ? if (ConnectionManager.instance == null)
ConnectionManager.instance = new ConnectionManager() : ConnectionManager.instance = new ConnectionManager();
ConnectionManager.instance; return ConnectionManager.instance;
} }
/** /**
@ -50,26 +52,29 @@ export default class ConnectionManager {
/** /**
* Tries to recover login token from the secure keychain * Tries to recover login token from the secure keychain
* *
* @returns {Promise<R>} * @returns Promise<string>
*/ */
async recoverLogin() { async recoverLogin(): Promise<string> {
return new Promise((resolve, reject) => { return new Promise(
if (this.getToken() !== null) (resolve: (token: string) => void, reject: () => void) => {
resolve(this.getToken()); const token = this.getToken();
if (token != null) resolve(token);
else { else {
Keychain.getInternetCredentials(SERVER_NAME) Keychain.getInternetCredentials(SERVER_NAME)
.then((data) => { .then((data: Keychain.UserCredentials | false) => {
if (data) { if (
data != null &&
data.password != null &&
typeof data.password === 'string'
) {
this.#token = data.password; this.#token = data.password;
resolve(this.#token); resolve(this.#token);
} else } else reject();
reject(false);
}) })
.catch(() => { .catch((): void => reject());
reject(false);
});
} }
}); },
);
} }
/** /**
@ -77,7 +82,7 @@ export default class ConnectionManager {
* *
* @returns {boolean} * @returns {boolean}
*/ */
isLoggedIn() { isLoggedIn(): boolean {
return this.getToken() !== null; return this.getToken() !== null;
} }
@ -86,41 +91,36 @@ export default class ConnectionManager {
* *
* @param email * @param email
* @param token * @param token
* @returns {Promise<R>} * @returns Promise<void>
*/ */
async saveLogin(email: string, token: string) { async saveLogin(email: string, token: string): Promise<void> {
return new Promise((resolve, reject) => { return new Promise((resolve: () => void, reject: () => void) => {
Keychain.setInternetCredentials(SERVER_NAME, 'token', token) Keychain.setInternetCredentials(SERVER_NAME, 'token', token)
.then(() => { .then(() => {
this.#token = token; this.#token = token;
this.#email = email; this.#email = email;
resolve(true); resolve();
}) })
.catch(() => { .catch((): void => reject());
reject(false);
});
}); });
} }
/** /**
* Deletes the login token from the keychain * Deletes the login token from the keychain
* *
* @returns {Promise<R>} * @returns Promise<void>
*/ */
async disconnect() { async disconnect(): Promise<void> {
return new Promise((resolve, reject) => { return new Promise((resolve: () => void, reject: () => void) => {
Keychain.resetInternetCredentials(SERVER_NAME) Keychain.resetInternetCredentials(SERVER_NAME)
.then(() => { .then(() => {
this.#token = null; this.#token = null;
resolve(true); resolve();
}) })
.catch(() => { .catch((): void => reject());
reject(false);
});
}); });
} }
/** /**
* Sends the given login and password to the api. * 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 the combination is valid, the login token is received and saved in the secure keychain.
@ -128,26 +128,26 @@ export default class ConnectionManager {
* *
* @param email * @param email
* @param password * @param password
* @returns {Promise<R>} * @returns Promise<void>
*/ */
async connect(email: string, password: string) { async connect(email: string, password: string): Promise<void> {
return new Promise((resolve, reject) => { return new Promise(
(resolve: () => void, reject: (error: number) => void) => {
const data = { const data = {
email: email, email,
password: password, password,
}; };
apiRequest(AUTH_PATH, 'POST', data) apiRequest(AUTH_PATH, 'POST', data)
.then((response) => { .then((response: ApiDataLoginType) => {
if (response.token != null) {
this.saveLogin(email, response.token) this.saveLogin(email, response.token)
.then(() => { .then((): void => resolve())
resolve(true); .catch((): void => reject(ERROR_TYPE.TOKEN_SAVE));
} else reject(ERROR_TYPE.SERVER_ERROR);
}) })
.catch(() => { .catch((error: number): void => reject(error));
reject(ERROR_TYPE.TOKEN_SAVE); },
}); );
})
.catch((error) => reject(error));
});
} }
/** /**
@ -155,20 +155,27 @@ export default class ConnectionManager {
* *
* @param path * @param path
* @param params * @param params
* @returns {Promise<R>} * @returns Promise<ApiGenericDataType>
*/ */
async authenticatedRequest(path: string, params: Object) { async authenticatedRequest(
return new Promise((resolve, reject) => { path: string,
params: {...},
): Promise<ApiGenericDataType> {
return new Promise(
(
resolve: (response: ApiGenericDataType) => void,
reject: (error: number) => void,
) => {
if (this.getToken() !== null) { if (this.getToken() !== null) {
let data = { const data = {
...params,
token: this.getToken(), token: this.getToken(),
...params
}; };
apiRequest(path, 'POST', data) apiRequest(path, 'POST', data)
.then((response) => resolve(response)) .then((response: ApiGenericDataType): void => resolve(response))
.catch((error) => reject(error)); .catch((error: number): void => reject(error));
} else } else reject(ERROR_TYPE.TOKEN_RETRIEVE);
reject(ERROR_TYPE.TOKEN_RETRIEVE); },
}); );
} }
} }