Application Android et IOS pour l'amicale des élèves
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConnectionManager.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // @flow
  2. import * as Keychain from 'react-native-keychain';
  3. import {apiRequest, ERROR_TYPE} from "../utils/WebData";
  4. /**
  5. * champ: error
  6. *
  7. * 0 : SUCCESS -> pas d'erreurs
  8. * 1 : BAD_CREDENTIALS -> email ou mdp invalide
  9. * 2 : BAD_TOKEN -> session expirée
  10. * 3 : NO_CONSENT
  11. * 403 : FORBIDDEN -> accès a la ressource interdit
  12. * 500 : SERVER_ERROR -> pb coté serveur
  13. */
  14. const SERVER_NAME = "amicale-insat.fr";
  15. const AUTH_PATH = "password";
  16. export default class ConnectionManager {
  17. static instance: ConnectionManager | null = null;
  18. #email: string;
  19. #token: string | null;
  20. constructor() {
  21. this.#token = null;
  22. }
  23. /**
  24. * Gets this class instance or create one if none is found
  25. *
  26. * @returns {ConnectionManager}
  27. */
  28. static getInstance(): ConnectionManager {
  29. return ConnectionManager.instance === null ?
  30. ConnectionManager.instance = new ConnectionManager() :
  31. ConnectionManager.instance;
  32. }
  33. /**
  34. * Gets the current token
  35. *
  36. * @returns {string | null}
  37. */
  38. getToken(): string | null {
  39. return this.#token;
  40. }
  41. /**
  42. * Tries to recover login token from the secure keychain
  43. *
  44. * @returns {Promise<R>}
  45. */
  46. async recoverLogin() {
  47. return new Promise((resolve, reject) => {
  48. if (this.getToken() !== null)
  49. resolve(this.getToken());
  50. else {
  51. Keychain.getInternetCredentials(SERVER_NAME)
  52. .then((data) => {
  53. if (data) {
  54. this.#token = data.password;
  55. resolve(this.#token);
  56. } else
  57. reject(false);
  58. })
  59. .catch(() => {
  60. reject(false);
  61. });
  62. }
  63. });
  64. }
  65. /**
  66. * Check if the user has a valid token
  67. *
  68. * @returns {boolean}
  69. */
  70. isLoggedIn() {
  71. return this.getToken() !== null;
  72. }
  73. /**
  74. * Saves the login token in the secure keychain
  75. *
  76. * @param email
  77. * @param token
  78. * @returns {Promise<R>}
  79. */
  80. async saveLogin(email: string, token: string) {
  81. return new Promise((resolve, reject) => {
  82. Keychain.setInternetCredentials(SERVER_NAME, 'token', token)
  83. .then(() => {
  84. this.#token = token;
  85. this.#email = email;
  86. resolve(true);
  87. })
  88. .catch(() => {
  89. reject(false);
  90. });
  91. });
  92. }
  93. /**
  94. * Deletes the login token from the keychain
  95. *
  96. * @returns {Promise<R>}
  97. */
  98. async disconnect() {
  99. return new Promise((resolve, reject) => {
  100. Keychain.resetInternetCredentials(SERVER_NAME)
  101. .then(() => {
  102. this.#token = null;
  103. resolve(true);
  104. })
  105. .catch(() => {
  106. reject(false);
  107. });
  108. });
  109. }
  110. /**
  111. * Sends the given login and password to the api.
  112. * If the combination is valid, the login token is received and saved in the secure keychain.
  113. * If not, the promise is rejected with the corresponding error code.
  114. *
  115. * @param email
  116. * @param password
  117. * @returns {Promise<R>}
  118. */
  119. async connect(email: string, password: string) {
  120. return new Promise((resolve, reject) => {
  121. const data = {
  122. email: email,
  123. password: password,
  124. };
  125. apiRequest(AUTH_PATH, 'POST', data)
  126. .then((response) => {
  127. this.saveLogin(email, response.token)
  128. .then(() => {
  129. resolve(true);
  130. })
  131. .catch(() => {
  132. reject(ERROR_TYPE.TOKEN_SAVE);
  133. });
  134. })
  135. .catch((error) => reject(error));
  136. });
  137. }
  138. /**
  139. * Sends an authenticated request with the login token to the API
  140. *
  141. * @param path
  142. * @param params
  143. * @returns {Promise<R>}
  144. */
  145. async authenticatedRequest(path: string, params: Object) {
  146. return new Promise((resolve, reject) => {
  147. if (this.getToken() !== null) {
  148. let data = {
  149. token: this.getToken(),
  150. ...params
  151. };
  152. apiRequest(path, 'POST', data)
  153. .then((response) => resolve(response))
  154. .catch((error) => reject(error));
  155. } else
  156. reject(ERROR_TYPE.TOKEN_RETRIEVE);
  157. });
  158. }
  159. }