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
|
// @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,161 +15,167 @@ 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;
|
|
||||||
|
|
||||||
constructor() {
|
#token: string | null;
|
||||||
this.#token = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
constructor() {
|
||||||
* Gets this class instance or create one if none is found
|
this.#token = null;
|
||||||
*
|
}
|
||||||
* @returns {ConnectionManager}
|
|
||||||
*/
|
|
||||||
static getInstance(): ConnectionManager {
|
|
||||||
return ConnectionManager.instance === null ?
|
|
||||||
ConnectionManager.instance = new ConnectionManager() :
|
|
||||||
ConnectionManager.instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the current token
|
* Gets this class instance or create one if none is found
|
||||||
*
|
*
|
||||||
* @returns {string | null}
|
* @returns {ConnectionManager}
|
||||||
*/
|
*/
|
||||||
getToken(): string | null {
|
static getInstance(): ConnectionManager {
|
||||||
return this.#token;
|
if (ConnectionManager.instance == null)
|
||||||
}
|
ConnectionManager.instance = new ConnectionManager();
|
||||||
|
return ConnectionManager.instance;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tries to recover login token from the secure keychain
|
* Gets the current token
|
||||||
*
|
*
|
||||||
* @returns {Promise<R>}
|
* @returns {string | null}
|
||||||
*/
|
*/
|
||||||
async recoverLogin() {
|
getToken(): string | null {
|
||||||
return new Promise((resolve, reject) => {
|
return this.#token;
|
||||||
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);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if the user has a valid token
|
* Tries to recover login token from the secure keychain
|
||||||
*
|
*
|
||||||
* @returns {boolean}
|
* @returns Promise<string>
|
||||||
*/
|
*/
|
||||||
isLoggedIn() {
|
async recoverLogin(): Promise<string> {
|
||||||
return this.getToken() !== null;
|
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
|
* Check if the user has a valid token
|
||||||
*
|
*
|
||||||
* @param email
|
* @returns {boolean}
|
||||||
* @param token
|
*/
|
||||||
* @returns {Promise<R>}
|
isLoggedIn(): boolean {
|
||||||
*/
|
return this.getToken() !== null;
|
||||||
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);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deletes the login token from the keychain
|
* Saves the login token in the secure keychain
|
||||||
*
|
*
|
||||||
* @returns {Promise<R>}
|
* @param email
|
||||||
*/
|
* @param token
|
||||||
async disconnect() {
|
* @returns Promise<void>
|
||||||
return new Promise((resolve, reject) => {
|
*/
|
||||||
Keychain.resetInternetCredentials(SERVER_NAME)
|
async saveLogin(email: string, token: string): Promise<void> {
|
||||||
.then(() => {
|
return new Promise((resolve: () => void, reject: () => void) => {
|
||||||
this.#token = null;
|
Keychain.setInternetCredentials(SERVER_NAME, 'token', token)
|
||||||
resolve(true);
|
.then(() => {
|
||||||
})
|
this.#token = token;
|
||||||
.catch(() => {
|
this.#email = email;
|
||||||
reject(false);
|
resolve();
|
||||||
});
|
})
|
||||||
});
|
.catch((): void => reject());
|
||||||
}
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes the login token from the keychain
|
||||||
|
*
|
||||||
|
* @returns Promise<void>
|
||||||
|
*/
|
||||||
|
async disconnect(): Promise<void> {
|
||||||
|
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.
|
* 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.
|
||||||
* If not, the promise is rejected with the corresponding error code.
|
* If not, the promise is rejected with the corresponding error code.
|
||||||
*
|
*
|
||||||
* @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(
|
||||||
const data = {
|
(resolve: () => void, reject: (error: number) => void) => {
|
||||||
email: email,
|
const data = {
|
||||||
password: password,
|
email,
|
||||||
};
|
password,
|
||||||
apiRequest(AUTH_PATH, 'POST', data)
|
};
|
||||||
.then((response) => {
|
apiRequest(AUTH_PATH, 'POST', data)
|
||||||
this.saveLogin(email, response.token)
|
.then((response: ApiDataLoginType) => {
|
||||||
.then(() => {
|
if (response.token != null) {
|
||||||
resolve(true);
|
this.saveLogin(email, response.token)
|
||||||
})
|
.then((): void => resolve())
|
||||||
.catch(() => {
|
.catch((): void => reject(ERROR_TYPE.TOKEN_SAVE));
|
||||||
reject(ERROR_TYPE.TOKEN_SAVE);
|
} else reject(ERROR_TYPE.SERVER_ERROR);
|
||||||
});
|
})
|
||||||
})
|
.catch((error: number): void => reject(error));
|
||||||
.catch((error) => reject(error));
|
},
|
||||||
});
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends an authenticated request with the login token to the API
|
* Sends an authenticated request with the login token to the API
|
||||||
*
|
*
|
||||||
* @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,
|
||||||
if (this.getToken() !== null) {
|
params: {...},
|
||||||
let data = {
|
): Promise<ApiGenericDataType> {
|
||||||
token: this.getToken(),
|
return new Promise(
|
||||||
...params
|
(
|
||||||
};
|
resolve: (response: ApiGenericDataType) => void,
|
||||||
apiRequest(path, 'POST', data)
|
reject: (error: number) => void,
|
||||||
.then((response) => resolve(response))
|
) => {
|
||||||
.catch((error) => reject(error));
|
if (this.getToken() !== null) {
|
||||||
} else
|
const data = {
|
||||||
reject(ERROR_TYPE.TOKEN_RETRIEVE);
|
...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);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue