auth-sync.js 739 B

12345678910111213141516171819202122232425262728
  1. import misago from "misago/index"
  2. import { patch } from "misago/reducers/auth"
  3. import ajax from "misago/services/ajax"
  4. import snackbar from "misago/services/snackbar"
  5. import store from "misago/services/store"
  6. const AUTH_SYNC_RATE = 45 // sync user with backend every 45 seconds
  7. export default function initializer(context) {
  8. if (context.get("isAuthenticated")) {
  9. window.setInterval(function() {
  10. ajax.get(context.get("AUTH_API")).then(
  11. function(data) {
  12. store.dispatch(patch(data))
  13. },
  14. function(rejection) {
  15. snackbar.apiError(rejection)
  16. }
  17. )
  18. }, AUTH_SYNC_RATE * 1000)
  19. }
  20. }
  21. misago.addInitializer({
  22. name: "auth-sync",
  23. initializer: initializer,
  24. after: "auth"
  25. })