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.

50 lines
1.1 KiB

5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. export const state = () => ({
  2. username: null,
  3. isAuthenticated: false
  4. })
  5. export const mutations = {
  6. setUsername(state, username) {
  7. state.username = username
  8. },
  9. clearUsername(state) {
  10. state.username = null
  11. },
  12. setAuthenticated(state, isAuthenticated) {
  13. state.isAuthenticated = isAuthenticated
  14. }
  15. }
  16. export const getters = {
  17. isAuthenticated(state) {
  18. return state.isAuthenticated
  19. },
  20. getUsername(state) {
  21. return state.username
  22. }
  23. }
  24. export const actions = {
  25. async authenticateUser({ commit }, authData) {
  26. try {
  27. await this.$services.auth.login(authData.username, authData.password)
  28. commit('setAuthenticated', true)
  29. } catch(error) {
  30. throw new Error('The credential is invalid')
  31. }
  32. },
  33. async initAuth({ commit }) {
  34. try {
  35. const user = await this.$services.user.getMyProfile()
  36. commit('setAuthenticated', true)
  37. commit('setUsername', user.username)
  38. } catch {
  39. commit('setAuthenticated', false)
  40. }
  41. },
  42. async logout({ commit }) {
  43. await this.$services.auth.logout()
  44. commit('setAuthenticated', false)
  45. commit('clearUsername')
  46. }
  47. }