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.

68 lines
1.5 KiB

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