feat: redirect the user to the last page they were on before logging in after login

This commit is contained in:
kolaente 2021-10-13 21:53:39 +02:00
parent 97dd55d946
commit 9a2f95ecc6
Signed by untrusted user: konrad
GPG Key ID: F40E70337AB24C9B
5 changed files with 44 additions and 5 deletions

View File

@ -19,6 +19,7 @@
import {mapState} from 'vuex' import {mapState} from 'vuex'
import logoUrl from '@/assets/logo-full.svg' import logoUrl from '@/assets/logo-full.svg'
import { saveLastVisited } from '@/helpers/saveLastVisited'
export default { export default {
name: 'contentNoAuth', name: 'contentNoAuth',
@ -46,6 +47,7 @@ export default {
localStorage.getItem('passwordResetToken') === null && localStorage.getItem('passwordResetToken') === null &&
localStorage.getItem('emailConfirmToken') === null localStorage.getItem('emailConfirmToken') === null
) { ) {
saveLastVisited(this.$route.name, this.$route.params)
this.$router.push({name: 'user.login'}) this.$router.push({name: 'user.login'})
} }
}, },

View File

@ -0,0 +1,18 @@
const LAST_VISITED_KEY = 'lastVisited'
export const saveLastVisited = (name: string, params: object) => {
localStorage.setItem(LAST_VISITED_KEY, JSON.stringify({name, params}))
}
export const getLastVisited = () => {
const lastVisited = localStorage.getItem(LAST_VISITED_KEY)
if (lastVisited === null) {
return null
}
return JSON.parse(lastVisited)
}
export const clearLastVisited = () => {
return localStorage.removeItem(LAST_VISITED_KEY)
}

View File

@ -104,13 +104,13 @@
<script> <script>
import {mapState} from 'vuex' import {mapState} from 'vuex'
import router from '../../router'
import {HTTPFactory} from '@/http-common' import {HTTPFactory} from '@/http-common'
import {ERROR_MESSAGE, LOADING} from '@/store/mutation-types' import {ERROR_MESSAGE, LOADING} from '@/store/mutation-types'
import legal from '../../components/misc/legal' import legal from '../../components/misc/legal'
import ApiConfig from '@/components/misc/api-config.vue' import ApiConfig from '@/components/misc/api-config.vue'
import {getErrorText} from '@/message' import {getErrorText} from '@/message'
import {redirectToProvider} from '../../helpers/redirectToProvider' import {redirectToProvider} from '../../helpers/redirectToProvider'
import {getLastVisited, clearLastVisited} from '../../helpers/saveLastVisited'
export default { export default {
components: { components: {
@ -142,9 +142,18 @@ export default {
}) })
} }
// Check if the user is already logged in, if so, redirect him to the homepage // Check if the user is already logged in, if so, redirect them to the homepage
if (this.authenticated) { if (this.authenticated) {
router.push({name: 'home'}) const last = getLastVisited()
if (last !== null) {
this.$router.push({
name: last.name,
params: last.params,
})
clearLastVisited()
} else {
this.$router.push({name: 'home'})
}
} }
}, },
created() { created() {

View File

@ -14,6 +14,7 @@ import {mapState} from 'vuex'
import {ERROR_MESSAGE, LOADING} from '@/store/mutation-types' import {ERROR_MESSAGE, LOADING} from '@/store/mutation-types'
import {getErrorText} from '@/message' import {getErrorText} from '@/message'
import {clearLastVisited, getLastVisited} from '../../helpers/saveLastVisited'
export default { export default {
name: 'Auth', name: 'Auth',
@ -63,7 +64,16 @@ export default {
code: this.$route.query.code, code: this.$route.query.code,
}) })
.then(() => { .then(() => {
this.$router.push({name: 'home'}) const last = getLastVisited()
if (last !== null) {
this.$router.push({
name: last.name,
params: last.params,
})
clearLastVisited()
} else {
this.$router.push({name: 'home'})
}
}) })
.catch(e => { .catch(e => {
const err = getErrorText(e, p => this.$t(p)) const err = getErrorText(e, p => this.$t(p))

View File

@ -116,7 +116,7 @@ export default {
} }
}, },
beforeMount() { beforeMount() {
// Check if the user is already logged in, if so, redirect him to the homepage // Check if the user is already logged in, if so, redirect them to the homepage
if (this.authenticated) { if (this.authenticated) {
router.push({name: 'home'}) router.push({name: 'home'})
} }