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.

71 lines
1.6 KiB

5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
2 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.$repositories.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 fetchSocialLink() {
  48. return await this.$repositories.auth.socialLink()
  49. },
  50. async initAuth({ commit }) {
  51. try {
  52. const user = await this.$repositories.user.getProfile()
  53. commit('setAuthenticated', true)
  54. commit('setUsername', user.username)
  55. commit('setUserId', user.id)
  56. commit('setIsStaff', user.isStaff)
  57. } catch {
  58. commit('setAuthenticated', false)
  59. commit('setIsStaff', false)
  60. }
  61. },
  62. async logout({ commit }) {
  63. await this.$repositories.auth.logout()
  64. commit('setAuthenticated', false)
  65. commit('setIsStaff', false)
  66. commit('clearUsername')
  67. }
  68. }