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
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
5 changed files with 44 additions and 5 deletions

View file

@ -19,6 +19,7 @@
import {mapState} from 'vuex'
import logoUrl from '@/assets/logo-full.svg'
import { saveLastVisited } from '@/helpers/saveLastVisited'
export default {
name: 'contentNoAuth',
@ -46,6 +47,7 @@ export default {
localStorage.getItem('passwordResetToken') === null &&
localStorage.getItem('emailConfirmToken') === null
) {
saveLastVisited(this.$route.name, this.$route.params)
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>
import {mapState} from 'vuex'
import router from '../../router'
import {HTTPFactory} from '@/http-common'
import {ERROR_MESSAGE, LOADING} from '@/store/mutation-types'
import legal from '../../components/misc/legal'
import ApiConfig from '@/components/misc/api-config.vue'
import {getErrorText} from '@/message'
import {redirectToProvider} from '../../helpers/redirectToProvider'
import {getLastVisited, clearLastVisited} from '../../helpers/saveLastVisited'
export default {
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) {
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() {

View file

@ -14,6 +14,7 @@ import {mapState} from 'vuex'
import {ERROR_MESSAGE, LOADING} from '@/store/mutation-types'
import {getErrorText} from '@/message'
import {clearLastVisited, getLastVisited} from '../../helpers/saveLastVisited'
export default {
name: 'Auth',
@ -63,7 +64,16 @@ export default {
code: this.$route.query.code,
})
.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 => {
const err = getErrorText(e, p => this.$t(p))

View file

@ -116,7 +116,7 @@ export default {
}
},
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) {
router.push({name: 'home'})
}