forked from vergnet/application-amicale
Improve connection manager to match linter
This commit is contained in:
parent
3d9bfdea4c
commit
3629c5730a
1 changed files with 152 additions and 145 deletions
|
@ -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,13 +15,14 @@ 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;
|
||||
|
||||
#email: string;
|
||||
|
||||
#token: string | null;
|
||||
|
||||
constructor() {
|
||||
|
@ -33,9 +35,9 @@ export default class ConnectionManager {
|
|||
* @returns {ConnectionManager}
|
||||
*/
|
||||
static getInstance(): ConnectionManager {
|
||||
return ConnectionManager.instance === null ?
|
||||
ConnectionManager.instance = new ConnectionManager() :
|
||||
ConnectionManager.instance;
|
||||
if (ConnectionManager.instance == null)
|
||||
ConnectionManager.instance = new ConnectionManager();
|
||||
return ConnectionManager.instance;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -50,26 +52,29 @@ export default class ConnectionManager {
|
|||
/**
|
||||
* Tries to recover login token from the secure keychain
|
||||
*
|
||||
* @returns {Promise<R>}
|
||||
* @returns Promise<string>
|
||||
*/
|
||||
async recoverLogin() {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (this.getToken() !== null)
|
||||
resolve(this.getToken());
|
||||
async recoverLogin(): Promise<string> {
|
||||
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) => {
|
||||
if (data) {
|
||||
.then((data: Keychain.UserCredentials | false) => {
|
||||
if (
|
||||
data != null &&
|
||||
data.password != null &&
|
||||
typeof data.password === 'string'
|
||||
) {
|
||||
this.#token = data.password;
|
||||
resolve(this.#token);
|
||||
} else
|
||||
reject(false);
|
||||
} else reject();
|
||||
})
|
||||
.catch(() => {
|
||||
reject(false);
|
||||
});
|
||||
.catch((): void => reject());
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -77,7 +82,7 @@ export default class ConnectionManager {
|
|||
*
|
||||
* @returns {boolean}
|
||||
*/
|
||||
isLoggedIn() {
|
||||
isLoggedIn(): boolean {
|
||||
return this.getToken() !== null;
|
||||
}
|
||||
|
||||
|
@ -86,41 +91,36 @@ export default class ConnectionManager {
|
|||
*
|
||||
* @param email
|
||||
* @param token
|
||||
* @returns {Promise<R>}
|
||||
* @returns Promise<void>
|
||||
*/
|
||||
async saveLogin(email: string, token: string) {
|
||||
return new Promise((resolve, reject) => {
|
||||
async saveLogin(email: string, token: string): Promise<void> {
|
||||
return new Promise((resolve: () => void, reject: () => void) => {
|
||||
Keychain.setInternetCredentials(SERVER_NAME, 'token', token)
|
||||
.then(() => {
|
||||
this.#token = token;
|
||||
this.#email = email;
|
||||
resolve(true);
|
||||
resolve();
|
||||
})
|
||||
.catch(() => {
|
||||
reject(false);
|
||||
});
|
||||
.catch((): void => reject());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the login token from the keychain
|
||||
*
|
||||
* @returns {Promise<R>}
|
||||
* @returns Promise<void>
|
||||
*/
|
||||
async disconnect() {
|
||||
return new Promise((resolve, reject) => {
|
||||
async disconnect(): Promise<void> {
|
||||
return new Promise((resolve: () => void, reject: () => void) => {
|
||||
Keychain.resetInternetCredentials(SERVER_NAME)
|
||||
.then(() => {
|
||||
this.#token = null;
|
||||
resolve(true);
|
||||
resolve();
|
||||
})
|
||||
.catch(() => {
|
||||
reject(false);
|
||||
});
|
||||
.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.
|
||||
|
@ -128,26 +128,26 @@ export default class ConnectionManager {
|
|||
*
|
||||
* @param email
|
||||
* @param password
|
||||
* @returns {Promise<R>}
|
||||
* @returns Promise<void>
|
||||
*/
|
||||
async connect(email: string, password: string) {
|
||||
return new Promise((resolve, reject) => {
|
||||
async connect(email: string, password: string): Promise<void> {
|
||||
return new Promise(
|
||||
(resolve: () => void, reject: (error: number) => void) => {
|
||||
const data = {
|
||||
email: email,
|
||||
password: password,
|
||||
email,
|
||||
password,
|
||||
};
|
||||
apiRequest(AUTH_PATH, 'POST', data)
|
||||
.then((response) => {
|
||||
.then((response: ApiDataLoginType) => {
|
||||
if (response.token != null) {
|
||||
this.saveLogin(email, response.token)
|
||||
.then(() => {
|
||||
resolve(true);
|
||||
.then((): void => resolve())
|
||||
.catch((): void => reject(ERROR_TYPE.TOKEN_SAVE));
|
||||
} else reject(ERROR_TYPE.SERVER_ERROR);
|
||||
})
|
||||
.catch(() => {
|
||||
reject(ERROR_TYPE.TOKEN_SAVE);
|
||||
});
|
||||
})
|
||||
.catch((error) => reject(error));
|
||||
});
|
||||
.catch((error: number): void => reject(error));
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -155,20 +155,27 @@ export default class ConnectionManager {
|
|||
*
|
||||
* @param path
|
||||
* @param params
|
||||
* @returns {Promise<R>}
|
||||
* @returns Promise<ApiGenericDataType>
|
||||
*/
|
||||
async authenticatedRequest(path: string, params: Object) {
|
||||
return new Promise((resolve, reject) => {
|
||||
async authenticatedRequest(
|
||||
path: string,
|
||||
params: {...},
|
||||
): Promise<ApiGenericDataType> {
|
||||
return new Promise(
|
||||
(
|
||||
resolve: (response: ApiGenericDataType) => void,
|
||||
reject: (error: number) => void,
|
||||
) => {
|
||||
if (this.getToken() !== null) {
|
||||
let data = {
|
||||
const data = {
|
||||
...params,
|
||||
token: this.getToken(),
|
||||
...params
|
||||
};
|
||||
apiRequest(path, 'POST', data)
|
||||
.then((response) => resolve(response))
|
||||
.catch((error) => reject(error));
|
||||
} else
|
||||
reject(ERROR_TYPE.TOKEN_RETRIEVE);
|
||||
});
|
||||
.then((response: ApiGenericDataType): void => resolve(response))
|
||||
.catch((error: number): void => reject(error));
|
||||
} else reject(ERROR_TYPE.TOKEN_RETRIEVE);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue