fb.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import firebase from "firebase/app"
  2. import "firebase/firestore"
  3. import "firebase/auth"
  4. // Initialize Firebase, copied from cloud console
  5. const firebaseConfig = {
  6. apiKey: process.env.API_KEY || "AIzaSyCMsFreESs58-hRxTtiqQrIcimh4i1wbsM",
  7. authDomain: process.env.AUTH_DOMAIN || "postwoman-api.firebaseapp.com",
  8. databaseURL: process.env.DATABASE_URL || "https://postwoman-api.firebaseio.com",
  9. projectId: process.env.PROJECT_ID || "postwoman-api",
  10. storageBucket: process.env.STORAGE_BUCKET || "postwoman-api.appspot.com",
  11. messagingSenderId: process.env.MESSAGING_SENDER_ID || "421993993223",
  12. appId: process.env.APP_ID || "1:421993993223:web:ec0baa8ee8c02ffa1fc6a2",
  13. measurementId: process.env.MEASUREMENT_ID || "G-ERJ6025CEB",
  14. }
  15. firebase.initializeApp(firebaseConfig)
  16. // a reference to the users collection
  17. const usersCollection = firebase.firestore().collection("users")
  18. // the shared state object that any vue component
  19. // can get access to
  20. export const fb = {
  21. currentUser: null,
  22. currentFeeds: [],
  23. currentSettings: [],
  24. currentHistory: [],
  25. currentCollections: [],
  26. currentEnvironments: [],
  27. writeFeeds: async (message, label) => {
  28. const dt = {
  29. createdOn: new Date(),
  30. author: fb.currentUser.uid,
  31. author_name: fb.currentUser.displayName,
  32. author_image: fb.currentUser.photoURL,
  33. message,
  34. label,
  35. }
  36. usersCollection
  37. .doc(fb.currentUser.uid)
  38. .collection("feeds")
  39. .add(dt)
  40. .catch((e) => console.error("error inserting", dt, e))
  41. },
  42. deleteFeed: (id) => {
  43. usersCollection
  44. .doc(fb.currentUser.uid)
  45. .collection("feeds")
  46. .doc(id)
  47. .delete()
  48. .catch((e) => console.error("error deleting", id, e))
  49. },
  50. writeSettings: async (setting, value) => {
  51. const st = {
  52. updatedOn: new Date(),
  53. author: fb.currentUser.uid,
  54. author_name: fb.currentUser.displayName,
  55. author_image: fb.currentUser.photoURL,
  56. name: setting,
  57. value,
  58. }
  59. usersCollection
  60. .doc(fb.currentUser.uid)
  61. .collection("settings")
  62. .doc(setting)
  63. .set(st)
  64. .catch((e) => console.error("error updating", st, e))
  65. },
  66. writeHistory: async (entry) => {
  67. const hs = entry
  68. usersCollection
  69. .doc(fb.currentUser.uid)
  70. .collection("history")
  71. .add(hs)
  72. .catch((e) => console.error("error inserting", hs, e))
  73. },
  74. deleteHistory: (entry) => {
  75. usersCollection
  76. .doc(fb.currentUser.uid)
  77. .collection("history")
  78. .doc(entry.id)
  79. .delete()
  80. .catch((e) => console.error("error deleting", entry, e))
  81. },
  82. clearHistory: () => {
  83. usersCollection
  84. .doc(fb.currentUser.uid)
  85. .collection("history")
  86. .get()
  87. .then(({ docs }) => {
  88. docs.forEach((e) => fb.deleteHistory(e))
  89. })
  90. },
  91. toggleStar: (entry, value) => {
  92. usersCollection
  93. .doc(fb.currentUser.uid)
  94. .collection("history")
  95. .doc(entry.id)
  96. .update({ star: value })
  97. .catch((e) => console.error("error deleting", entry, e))
  98. },
  99. writeCollections: async (collection) => {
  100. const cl = {
  101. updatedOn: new Date(),
  102. author: fb.currentUser.uid,
  103. author_name: fb.currentUser.displayName,
  104. author_image: fb.currentUser.photoURL,
  105. collection,
  106. }
  107. usersCollection
  108. .doc(fb.currentUser.uid)
  109. .collection("collections")
  110. .doc("sync")
  111. .set(cl)
  112. .catch((e) => console.error("error updating", cl, e))
  113. },
  114. writeEnvironments: async (environment) => {
  115. const ev = {
  116. updatedOn: new Date(),
  117. author: fb.currentUser.uid,
  118. author_name: fb.currentUser.displayName,
  119. author_image: fb.currentUser.photoURL,
  120. environment,
  121. }
  122. usersCollection
  123. .doc(fb.currentUser.uid)
  124. .collection("environments")
  125. .doc("sync")
  126. .set(ev)
  127. .catch((e) => console.error("error updating", ev, e))
  128. },
  129. }
  130. // When a user logs in or out, save that in the store
  131. firebase.auth().onAuthStateChanged((user) => {
  132. if (user) {
  133. fb.currentUser = user
  134. fb.currentUser.providerData.forEach((profile) => {
  135. let us = {
  136. updatedOn: new Date(),
  137. provider: profile.providerId,
  138. name: profile.displayName,
  139. email: profile.email,
  140. photoUrl: profile.photoURL,
  141. uid: profile.uid,
  142. }
  143. usersCollection
  144. .doc(fb.currentUser.uid)
  145. .set(us)
  146. .catch((e) => console.error("error updating", us, e))
  147. })
  148. usersCollection
  149. .doc(fb.currentUser.uid)
  150. .collection("feeds")
  151. .orderBy("createdOn", "desc")
  152. .onSnapshot((feedsRef) => {
  153. const feeds = []
  154. feedsRef.forEach((doc) => {
  155. const feed = doc.data()
  156. feed.id = doc.id
  157. feeds.push(feed)
  158. })
  159. fb.currentFeeds = feeds
  160. })
  161. usersCollection
  162. .doc(fb.currentUser.uid)
  163. .collection("settings")
  164. .onSnapshot((settingsRef) => {
  165. const settings = []
  166. settingsRef.forEach((doc) => {
  167. const setting = doc.data()
  168. setting.id = doc.id
  169. settings.push(setting)
  170. })
  171. fb.currentSettings = settings
  172. })
  173. usersCollection
  174. .doc(fb.currentUser.uid)
  175. .collection("history")
  176. .onSnapshot((historyRef) => {
  177. const history = []
  178. historyRef.forEach((doc) => {
  179. const entry = doc.data()
  180. entry.id = doc.id
  181. history.push(entry)
  182. })
  183. fb.currentHistory = history
  184. })
  185. usersCollection
  186. .doc(fb.currentUser.uid)
  187. .collection("collections")
  188. .onSnapshot((collectionsRef) => {
  189. const collections = []
  190. collectionsRef.forEach((doc) => {
  191. const collection = doc.data()
  192. collection.id = doc.id
  193. collections.push(collection)
  194. })
  195. if (collections.length > 0) {
  196. fb.currentCollections = collections[0].collection
  197. }
  198. })
  199. usersCollection
  200. .doc(fb.currentUser.uid)
  201. .collection("environments")
  202. .onSnapshot((environmentsRef) => {
  203. const environments = []
  204. environmentsRef.forEach((doc) => {
  205. const environment = doc.data()
  206. environment.id = doc.id
  207. environments.push(environment)
  208. })
  209. if (environments.length > 0) {
  210. fb.currentEnvironments = environments[0].environment
  211. }
  212. })
  213. } else {
  214. fb.currentUser = null
  215. }
  216. })