application-amicale/managers/ConnectionManager.js

199 lines
6.2 KiB
JavaScript
Raw Normal View History

2020-03-29 11:47:27 +02:00
// @flow
import * as SecureStore from 'expo-secure-store';
2020-03-29 11:47:27 +02:00
export const ERROR_TYPE = {
BAD_CREDENTIALS: 0,
CONNECTION_ERROR: 1,
SAVE_TOKEN: 2,
NO_TOKEN: 3,
NO_CONSENT: 4,
2020-03-29 11:47:27 +02:00
};
const AUTH_URL = "https://www.amicale-insat.fr/api/password";
export default class ConnectionManager {
static instance: ConnectionManager | null = null;
#email: string;
#token: string | null;
2020-03-29 11:47:27 +02:00
listeners: Array<Function>;
constructor() {
this.#token = null;
this.listeners = [];
}
2020-03-29 11:47:27 +02:00
/**
* Get 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;
}
2020-03-31 18:40:06 +02:00
getToken() {
return this.#token;
}
2020-03-31 14:21:01 +02:00
onLoginStateChange(newState: boolean) {
for (let i = 0; i < this.listeners.length; i++) {
if (this.listeners[i] !== undefined)
this.listeners[i](newState);
}
2020-03-31 14:21:01 +02:00
}
addLoginStateListener(listener: Function) {
this.listeners.push(listener);
2020-03-31 14:21:01 +02:00
}
async recoverLogin() {
return new Promise((resolve, reject) => {
2020-04-04 16:24:26 +02:00
if (this.getToken() !== null)
resolve(this.getToken());
else {
SecureStore.getItemAsync('token')
.then((token) => {
this.#token = token;
2020-03-31 18:40:06 +02:00
if (token !== null) {
this.onLoginStateChange(true);
resolve(token);
} else
reject(false);
})
.catch(error => {
reject(false);
});
}
});
}
isLoggedIn() {
2020-04-04 16:24:26 +02:00
return this.getToken() !== null;
2020-03-31 14:21:01 +02:00
}
async saveLogin(email: string, token: string) {
return new Promise((resolve, reject) => {
SecureStore.setItemAsync('token', token)
.then(() => {
this.#token = token;
this.#email = email;
2020-03-31 14:21:01 +02:00
this.onLoginStateChange(true);
resolve(true);
})
.catch(error => {
reject(false);
});
});
}
2020-03-31 14:21:01 +02:00
async disconnect() {
2020-03-31 18:40:06 +02:00
return new Promise((resolve, reject) => {
SecureStore.deleteItemAsync('token')
.then(() => {
this.#token = null;
2020-03-31 18:40:06 +02:00
this.onLoginStateChange(false);
resolve(true);
})
.catch((error) => {
reject(false);
});
});
2020-03-31 14:21:01 +02:00
}
2020-03-29 11:47:27 +02:00
async connect(email: string, password: string) {
let data = {
email: email,
password: password,
};
return new Promise((resolve, reject) => {
fetch(AUTH_URL, {
method: 'POST',
headers: new Headers({
'Accept': 'application/json',
'Content-Type': 'application/json',
}),
body: JSON.stringify(data)
}).then(async (response) => response.json())
.then((data) => {
if (this.isConnectionResponseValid(data)) {
if (data.state) {
this.saveLogin(email, data.token)
.then(() => {
resolve(true);
})
.catch(() => {
reject(ERROR_TYPE.SAVE_TOKEN);
});
2020-04-04 16:24:26 +02:00
} else if (data.data !== undefined
&& data.data.consent !== undefined
&& !data.data.consent)
reject(ERROR_TYPE.NO_CONSENT);
else
reject(ERROR_TYPE.BAD_CREDENTIALS);
} else
reject(ERROR_TYPE.CONNECTION_ERROR);
2020-03-29 11:47:27 +02:00
})
.catch((error) => {
reject(ERROR_TYPE.CONNECTION_ERROR);
});
});
}
isRequestResponseValid(response: Object) {
let valid = response !== undefined
&& response.state !== undefined
&& typeof response.state === "boolean";
if (valid && response.state)
valid = valid
&& response.data !== undefined
&& typeof response.data === "object";
return valid;
}
isConnectionResponseValid(response: Object) {
let valid = response !== undefined
&& response.state !== undefined
&& typeof response.state === "boolean";
if (valid && response.state)
valid = valid
&& response.token !== undefined
&& response.token !== ''
&& typeof response.token === "string";
return valid;
2020-03-29 11:47:27 +02:00
}
async authenticatedRequest(url: string) {
return new Promise((resolve, reject) => {
2020-04-04 16:24:26 +02:00
if (this.getToken() !== null) {
fetch(url, {
method: 'POST',
headers: new Headers({
'Accept': 'application/json',
'Content-Type': 'application/json',
}),
2020-04-04 16:24:26 +02:00
body: JSON.stringify({token: this.getToken()})
}).then(async (response) => response.json())
.then((data) => {
if (this.isRequestResponseValid(data)) {
if (data.state)
resolve(data.data);
else
reject(ERROR_TYPE.BAD_CREDENTIALS);
} else
reject(ERROR_TYPE.CONNECTION_ERROR);
})
.catch(() => {
reject(ERROR_TYPE.CONNECTION_ERROR);
});
} else
reject(ERROR_TYPE.NO_TOKEN);
});
}
2020-03-29 11:47:27 +02:00
}