admin-fe/src/store/modules/user.js

135 lines
3.7 KiB
JavaScript
Raw Normal View History

2019-03-05 22:00:48 +00:00
import { loginByUsername, getUserInfo } from '@/api/login'
import { getNodeInfo } from '@/api/nodeInfo'
2019-03-23 14:09:48 +00:00
import { getToken, setToken, removeToken, getAuthHost, setAuthHost, removeAuthHost } from '@/utils/auth'
2017-04-18 07:09:13 +00:00
const user = {
state: {
user: '',
2019-03-03 20:01:00 +00:00
id: '',
2017-04-18 07:09:13 +00:00
status: '',
code: '',
2017-07-20 05:26:09 +00:00
token: getToken(),
2019-03-23 14:09:48 +00:00
authHost: getAuthHost(),
2017-04-18 07:09:13 +00:00
name: '',
avatar: '',
introduction: '',
roles: [],
setting: {
articlePlatform: []
},
nodeInfo: {}
2017-04-18 07:09:13 +00:00
},
mutations: {
SET_CODE: (state, code) => {
2017-08-22 07:43:34 +00:00
state.code = code
2017-04-18 07:09:13 +00:00
},
SET_TOKEN: (state, token) => {
2017-08-22 07:43:34 +00:00
state.token = token
2017-04-18 07:09:13 +00:00
},
SET_INTRODUCTION: (state, introduction) => {
2017-08-22 07:43:34 +00:00
state.introduction = introduction
2017-04-18 07:09:13 +00:00
},
SET_SETTING: (state, setting) => {
2017-08-22 07:43:34 +00:00
state.setting = setting
2017-04-18 07:09:13 +00:00
},
SET_STATUS: (state, status) => {
2017-08-22 07:43:34 +00:00
state.status = status
2017-04-18 07:09:13 +00:00
},
SET_NAME: (state, name) => {
2017-08-22 07:43:34 +00:00
state.name = name
2017-04-18 07:09:13 +00:00
},
SET_AVATAR: (state, avatar) => {
2017-08-22 07:43:34 +00:00
state.avatar = avatar
2017-04-18 07:09:13 +00:00
},
SET_ROLES: (state, roles) => {
2017-08-22 07:43:34 +00:00
state.roles = roles
2019-03-03 20:01:00 +00:00
},
SET_ID: (state, id) => {
state.id = id
2019-03-23 14:09:48 +00:00
},
SET_AUTH_HOST: (state, authHost) => {
state.authHost = authHost
},
SET_NODE_INFO: (state, nodeInfo) => {
state.nodeInfo = nodeInfo
2017-04-18 07:09:13 +00:00
}
},
actions: {
2019-03-23 14:09:48 +00:00
LoginByUsername({ commit, dispatch }, { username, authHost, password }) {
2017-04-18 07:09:13 +00:00
return new Promise((resolve, reject) => {
2019-03-23 14:09:48 +00:00
loginByUsername(username, password, authHost).then(response => {
2017-08-22 07:43:34 +00:00
const data = response.data
2019-02-22 19:38:56 +00:00
commit('SET_TOKEN', data.access_token)
2019-03-23 14:09:48 +00:00
commit('SET_AUTH_HOST', authHost)
2019-03-22 20:58:58 +00:00
setToken(data.access_token)
2019-03-23 14:09:48 +00:00
setAuthHost(authHost)
2017-08-22 07:43:34 +00:00
resolve()
2017-04-24 06:15:42 +00:00
}).catch(error => {
2019-03-23 14:09:48 +00:00
dispatch('addErrorLog', { message: error.message })
2017-08-22 07:43:34 +00:00
reject(error)
})
})
2017-04-18 07:09:13 +00:00
},
2020-04-05 00:25:01 +00:00
async GetNodeInfo({ commit, dispatch, state }) {
const nodeInfo = await getNodeInfo(state.authHost)
2017-04-24 06:15:42 +00:00
commit('SET_NODE_INFO', nodeInfo.data)
2020-04-05 00:25:01 +00:00
dispatch('SetInvitesEnabled', nodeInfo.data.metadata.invitesEnabled)
},
2017-08-23 03:39:16 +00:00
GetUserInfo({ commit, state }) {
2017-04-24 06:15:42 +00:00
return new Promise((resolve, reject) => {
2019-03-23 14:09:48 +00:00
getUserInfo(state.token, state.authHost).then(response => {
2019-02-23 21:40:26 +00:00
const data = response.data
2020-03-29 19:52:32 +00:00
const message = '<span>This user doesn\`t have admin rights. Try another credentials or see the </span>' +
'<u><a target="_blank" href="https://docs.pleroma.social/backend/administration/CLI_tasks/user/#set-the-value-of-the-given-users-settings">docs</a></u>' +
'<span> to find out how to make this user an admin</span>'
2019-02-23 21:40:26 +00:00
if (!data) {
2019-01-09 08:04:24 +00:00
reject('Verification failed, please login again.')
2017-10-25 09:12:36 +00:00
}
2019-05-21 19:24:48 +00:00
if (data.pleroma && data.pleroma.is_admin) {
2019-02-23 21:40:26 +00:00
commit('SET_ROLES', ['admin'])
} else {
2020-03-29 19:52:32 +00:00
reject(message)
}
2019-05-21 19:24:48 +00:00
commit('SET_NAME', data.username)
2019-03-03 20:01:00 +00:00
commit('SET_ID', data.id)
2019-05-21 19:24:48 +00:00
commit('SET_AVATAR', data.avatar)
2019-02-22 19:38:56 +00:00
commit('SET_INTRODUCTION', '')
2017-08-22 07:43:34 +00:00
resolve(response)
2017-04-24 06:15:42 +00:00
}).catch(error => {
2017-08-22 07:43:34 +00:00
reject(error)
})
})
2017-04-24 06:15:42 +00:00
},
2019-03-05 22:00:48 +00:00
LogOut({ commit }) {
commit('SET_TOKEN', '')
commit('SET_ROLES', [])
removeToken()
2019-03-23 14:09:48 +00:00
removeAuthHost()
2017-04-18 07:09:13 +00:00
},
FedLogOut({ commit }) {
return new Promise(resolve => {
2017-08-22 07:43:34 +00:00
commit('SET_TOKEN', '')
removeToken()
2019-03-23 14:09:48 +00:00
removeAuthHost()
2017-08-22 07:43:34 +00:00
resolve()
})
2019-09-12 22:55:50 +00:00
},
async LoginByPleromaFE({ commit, dispatch }, { token }) {
commit('SET_TOKEN', token)
setToken(token)
commit('SET_AUTH_HOST', window.location.host)
setAuthHost(window.location.host)
dispatch('GetUserInfo')
2019-03-22 20:58:58 +00:00
}
2017-04-18 07:09:13 +00:00
}
2017-08-22 07:43:34 +00:00
}
2017-04-18 07:09:13 +00:00
2017-08-22 07:43:34 +00:00
export default user