Browse Source

Feature: Mobile - Redirect to back_url, if not logged in

Vladimir Sheremet 2 years ago
parent
commit
24dae98e4a

+ 8 - 2
app/frontend/apps/mobile/modules/login/views/Login.vue

@@ -1,7 +1,7 @@
 <!-- Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/ -->
 
 <script setup lang="ts">
-import { useRouter } from 'vue-router'
+import { useRoute, useRouter } from 'vue-router'
 import {
   useNotifications,
   NotificationTypes,
@@ -34,6 +34,7 @@ if (props.invalidatedSession === '1') {
 const authentication = useAuthenticationStore()
 
 const router = useRouter()
+const route = useRoute()
 
 const application = useApplicationLoadedStore()
 
@@ -94,7 +95,12 @@ const login = (formData: FormData<LoginFormData>) => {
   return authentication
     .login(formData.login as string, formData.password as string)
     .then(() => {
-      router.replace('/')
+      const { redirect: redirectUrl } = route.query
+      if (typeof redirectUrl === 'string') {
+        router.replace(redirectUrl)
+      } else {
+        router.replace('/')
+      }
     })
     .catch((errors: UserError) => {
       const { notify } = useNotifications()

+ 7 - 1
app/frontend/shared/router/guards/before/__tests__/authentication.spec.ts

@@ -20,6 +20,7 @@ describe('authenticationGuard', () => {
     const to = {
       name: 'TicketOverview',
       path: '/tickets',
+      fullPath: '/tickets',
       meta: {
         requiresAuth: true,
       },
@@ -28,7 +29,12 @@ describe('authenticationGuard', () => {
 
     authenticationGuard(to, from, next)
 
-    expect(next).toHaveBeenCalledWith('login')
+    expect(next).toHaveBeenCalledWith({
+      path: '/login',
+      query: {
+        redirect: '/tickets',
+      },
+    })
   })
 
   it('should give access to route for authenticated user', () => {

+ 1 - 1
app/frontend/shared/router/guards/before/authentication.ts

@@ -20,7 +20,7 @@ const checkAuthenticated = (
     log.debug(
       `Route guard for '${to.path}': authentication - forbidden - unauthenticated.`,
     )
-    next('login')
+    next({ path: '/login', query: { redirect: to.fullPath } })
   } else if (to.name === 'Login' && authenticated) {
     // Use the default route here.
     log.debug(