feat: use store getters to check auth (#731)

Co-authored-by: Dominik Pschenitschni <mail@celement.de>
Reviewed-on: https://kolaente.dev/vikunja/frontend/pulls/731
Reviewed-by: konrad <k@knt.li>
Co-authored-by: dpschen <dpschen@noreply.kolaente.de>
Co-committed-by: dpschen <dpschen@noreply.kolaente.de>
This commit is contained in:
dpschen 2021-09-10 13:04:00 +00:00 committed by konrad
parent b5df941e39
commit 0295113f50
4 changed files with 29 additions and 15 deletions

View file

@ -23,11 +23,9 @@
</template> </template>
<script> <script>
import {mapState} from 'vuex' import {mapState, mapGetters} from 'vuex'
import isTouchDevice from 'is-touch-device' import isTouchDevice from 'is-touch-device'
import authTypes from './models/authTypes'
import Notification from './components/misc/notification' import Notification from './components/misc/notification'
import {KEYBOARD_SHORTCUTS_ACTIVE, ONLINE} from './store/mutation-types' import {KEYBOARD_SHORTCUTS_ACTIVE, ONLINE} from './store/mutation-types'
import KeyboardShortcuts from './components/misc/keyboard-shortcuts' import KeyboardShortcuts from './components/misc/keyboard-shortcuts'
@ -74,11 +72,13 @@ export default {
return isTouchDevice() return isTouchDevice()
}, },
...mapState({ ...mapState({
authUser: state => state.auth.authenticated && (state.auth.info && state.auth.info.type === authTypes.USER),
authLinkShare: state => state.auth.authenticated && (state.auth.info && state.auth.info.type === authTypes.LINK_SHARE),
online: ONLINE, online: ONLINE,
keyboardShortcutsActive: KEYBOARD_SHORTCUTS_ACTIVE, keyboardShortcutsActive: KEYBOARD_SHORTCUTS_ACTIVE,
}), }),
...mapGetters('auth', [
'authUser',
'authLinkShare',
]),
}, },
methods: { methods: {
setupOnlineStatus() { setupOnlineStatus() {

View file

@ -1,5 +0,0 @@
{
"UNKNOWN": 0,
"USER": 1,
"LINK_SHARE": 2
}

View file

@ -3,6 +3,12 @@ import {ERROR_MESSAGE, LOADING} from '../mutation-types'
import UserModel from '../../models/user' import UserModel from '../../models/user'
import {getToken, refreshToken, removeToken, saveToken} from '@/helpers/auth' import {getToken, refreshToken, removeToken, saveToken} from '@/helpers/auth'
const AUTH_TYPES = {
'UNKNOWN': 0,
'USER': 1,
'LINK_SHARE': 2,
}
const defaultSettings = settings => { const defaultSettings = settings => {
if (typeof settings.weekStart === 'undefined' || settings.weekStart === '') { if (typeof settings.weekStart === 'undefined' || settings.weekStart === '') {
settings.weekStart = 0 settings.weekStart = 0
@ -21,6 +27,20 @@ export default {
lastUserInfoRefresh: null, lastUserInfoRefresh: null,
settings: {}, settings: {},
}), }),
getters: {
authUser(state) {
return state.authenticated && (
state.info &&
state.info.type === AUTH_TYPES.USER
)
},
authLinkShare(state) {
return state.authenticated && (
state.info &&
state.info.type === AUTH_TYPES.LINK_SHARE
)
},
},
mutations: { mutations: {
info(state, info) { info(state, info) {
state.info = info state.info = info

View file

@ -33,8 +33,7 @@
</template> </template>
<script> <script>
import {mapState} from 'vuex' import {mapGetters} from 'vuex'
import authTypes from '@/models/authTypes.json'
export default { export default {
name: 'LinkSharingAuth', name: 'LinkSharingAuth',
@ -54,9 +53,9 @@ export default {
mounted() { mounted() {
this.setTitle(this.$t('sharing.authenticating')) this.setTitle(this.$t('sharing.authenticating'))
}, },
computed: mapState({ computed: mapGetters('auth', [
authLinkShare: state => state.auth.authenticated && (state.auth.info && state.auth.info.type === authTypes.LINK_SHARE), 'authLinkShare',
}), ]),
methods: { methods: {
auth() { auth() {
this.errorMessage = '' this.errorMessage = ''