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.

пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
пре 8 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. import Auth from './api/auth';
  4. import HTTP from './api/http';
  5. import Storage from './api/storage';
  6. import Password from './domain/password';
  7. Vue.use(Vuex);
  8. const storage = new Storage();
  9. const auth = new Auth(storage);
  10. const PasswordsAPI = new HTTP('passwords', storage);
  11. const defaultPassword = {
  12. id: '',
  13. site: '',
  14. login: '',
  15. uppercase: true,
  16. lowercase: true,
  17. numbers: true,
  18. symbols: true,
  19. length: 12,
  20. counter: 1
  21. };
  22. function getDefaultPasswordProfile(version, passwordProfile = {}) {
  23. if (version === 1) {
  24. return Object.assign({}, defaultPassword, passwordProfile, {version: 1, length: 12});
  25. }
  26. if (version === 2) {
  27. return Object.assign({}, defaultPassword, passwordProfile, {version: 2, length: 16});
  28. }
  29. }
  30. const versionLoadedByDefault = storage.json().version || 1;
  31. const state = {
  32. authenticated: auth.isAuthenticated(),
  33. email: '',
  34. passwordStatus: 'CLEAN',
  35. passwords: [],
  36. baseURL: 'https://lesspass.com',
  37. password: getDefaultPasswordProfile(versionLoadedByDefault),
  38. version: versionLoadedByDefault
  39. };
  40. const mutations = {
  41. LOGOUT(state){
  42. state.authenticated = false;
  43. },
  44. USER_AUTHENTICATED(state, user){
  45. state.authenticated = true;
  46. state.email = user.email;
  47. },
  48. SET_PASSWORDS(state, passwords){
  49. state.passwords = passwords;
  50. },
  51. SET_PASSWORD(state, {password}){
  52. state.password = password;
  53. },
  54. DELETE_PASSWORD(state, {id}){
  55. var passwords = state.passwords;
  56. state.passwords = passwords.filter(password => {
  57. return password.id !== id;
  58. });
  59. if (state.password.id === id) {
  60. state.password = state.defaultPassword;
  61. }
  62. },
  63. PASSWORD_CLEAN(state){
  64. setTimeout(() => {
  65. state.passwordStatus = 'CLEAN';
  66. }, 5000);
  67. },
  68. CHANGE_PASSWORD_STATUS(state, status){
  69. state.passwordStatus = status;
  70. },
  71. SET_DEFAULT_PASSWORD(state){
  72. state.password = Object.assign({}, defaultPassword)
  73. },
  74. UPDATE_SITE(state, {site}){
  75. state.password.site = site
  76. },
  77. UPDATE_BASE_URL(state, {baseURL}){
  78. state.baseURL = baseURL
  79. },
  80. UPDATE_EMAIL(state, {email}){
  81. state.email = email
  82. },
  83. CHANGE_VERSION(state, {version}){
  84. state.password = getDefaultPasswordProfile(version, state.password);
  85. state.version = version;
  86. },
  87. SAVE_DEFAULT_OPTIONS: (state) => {
  88. const password = new Password(state.password);
  89. const jsonPassword = password.json();
  90. storage.save({password: jsonPassword, version: jsonPassword.version});
  91. }
  92. };
  93. const actions = {
  94. USER_AUTHENTICATED: ({commit}, user) => commit('USER_AUTHENTICATED', user),
  95. LOGOUT: ({commit}) => {
  96. auth.logout();
  97. commit('LOGOUT');
  98. },
  99. SAVE_OR_UPDATE_PASSWORD: ({commit, state, dispatch}) => {
  100. const password = new Password(state.password);
  101. if (password.isNewPassword(state.passwords)) {
  102. PasswordsAPI.create(password.json()).then(() => {
  103. commit('CHANGE_PASSWORD_STATUS', 'CREATED');
  104. commit('PASSWORD_CLEAN');
  105. dispatch('FETCH_PASSWORDS');
  106. })
  107. } else {
  108. PasswordsAPI.update(password.json()).then(() => {
  109. commit('CHANGE_PASSWORD_STATUS', 'UPDATED');
  110. commit('PASSWORD_CLEAN');
  111. dispatch('FETCH_PASSWORDS');
  112. })
  113. }
  114. },
  115. REFRESH_TOKEN: ({commit}) => {
  116. if (auth.isAuthenticated()) {
  117. auth.refreshToken().catch(() => {
  118. commit('LOGOUT');
  119. });
  120. }
  121. },
  122. PASSWORD_CHANGE({commit}, {password}){
  123. commit('SET_PASSWORD', {password});
  124. },
  125. PASSWORD_GENERATED: ({commit}) => {
  126. commit('CHANGE_PASSWORD_STATUS', 'DIRTY');
  127. },
  128. FETCH_PASSWORDS: ({commit}) => {
  129. if (auth.isAuthenticated()) {
  130. PasswordsAPI.all().then(response => commit('SET_PASSWORDS', response.data.results));
  131. }
  132. },
  133. FETCH_PASSWORD: ({commit}, {id}) => {
  134. PasswordsAPI.get({id}).then(response => commit('SET_PASSWORD', {password: response.data}));
  135. },
  136. DELETE_PASSWORD: ({commit}, {id}) => {
  137. PasswordsAPI.remove({id}).then(() => {
  138. commit('DELETE_PASSWORD', {id});
  139. });
  140. },
  141. LOAD_DEFAULT_PASSWORD: ({commit}) => {
  142. commit('SET_DEFAULT_PASSWORD');
  143. }
  144. };
  145. const getters = {
  146. passwords: state => state.passwords,
  147. password: state => state.password,
  148. isAuthenticated: state => state.authenticated,
  149. isGuest: state => !state.authenticated,
  150. passwordStatus: state => state.passwordStatus,
  151. email: state => state.email,
  152. version: state => state.version,
  153. };
  154. export default new Vuex.Store({
  155. state: Object.assign(state, storage.json()),
  156. getters,
  157. actions,
  158. mutations
  159. });