Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
boco-frontend
Manage
Activity
Members
Labels
Plan
Issues
21
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
idatt2106_2022_02
boco-frontend
Commits
9771a0ab
Commit
9771a0ab
authored
2 years ago
by
Zara Mudassar
Browse files
Options
Downloads
Patches
Plain Diff
Function for guarding navigation routes added.
parent
92e36b15
No related branches found
Branches containing commit
No related tags found
1 merge request
!22
Vue route guard
Pipeline
#176395
passed with stages
Stage:
Stage:
in 1 minute and 31 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/router/index.js
+32
-8
32 additions, 8 deletions
src/router/index.js
with
32 additions
and
8 deletions
src/router/index.js
+
32
−
8
View file @
9771a0ab
...
...
@@ -4,33 +4,53 @@ import HomeView from "../views/HomeView.vue";
import
LoginView
from
"
../views/LoginView.vue
"
;
import
NewPasswordView
from
"
../views/NewPasswordView
"
;
/**
* Guards routes. If token is null, no user is logged in and only the
* login page and the register page will be accessible.
*
* Legg denne metoden i login component for å nullstille token, har ikke logg ut metode ennå
* så må nullstilles på denne måten.
* beforeMount() {
console.log("Token: " + this.$store.state.user.token);
this.$store.commit("logout");
console.log("Token etter: " + this.$store.state.user.token);
}
*
*/
function
guardRoute
(
to
,
from
,
next
)
{
var
isAuthenticated
=
false
;
if
(
store
.
state
.
user
.
token
!=
null
)
isAuthenticated
=
true
;
else
isAuthenticated
=
false
;
if
(
isAuthenticated
)
{
next
();
// allow to enter route
}
else
{
next
(
"
/login
"
);
// go to '/login';
}
}
const
routes
=
[
{
path
:
"
/
"
,
//Endre før push
name
:
"
home
"
,
beforeEnter
:
guardRoute
,
component
:
HomeView
,
},
{
path
:
"
/about
"
,
name
:
"
about
"
,
beforeEnter
:
guardRoute
,
component
:
()
=>
import
(
"
../views/AboutView.vue
"
),
},
{
path
:
"
/profile/:id
"
,
name
:
"
profile
"
,
component
:
()
=>
import
(
"
../views/ProfileView.vue
"
),
beforeEnter
:
()
=>
{
if
(
store
.
state
.
user
.
token
==
null
)
router
.
push
(
"
login
"
);
},
beforeEnter
:
guardRoute
,
},
{
path
:
"
/register
"
,
name
:
"
register
"
,
// route level code-splitting
// this generates a separate chunk (register.[hash].js) for this route
// which is lazy-loaded when the route is visited.
component
:
()
=>
import
(
/* webpackChunkName: "register" */
"
../views/RegisterView.vue
"
),
component
:
()
=>
import
(
"
../views/RegisterView.vue
"
),
},
{
path
:
"
/login
"
,
...
...
@@ -41,21 +61,25 @@ const routes = [
path
:
"
/newPassword
"
,
name
:
"
newPassword
"
,
component
:
NewPasswordView
,
beforeEnter
:
guardRoute
,
},
{
path
:
"
/searchItemList
"
,
name
:
"
searchItemList
"
,
component
:
()
=>
import
(
"
../views/SearchItemListView.vue
"
),
beforeEnter
:
guardRoute
,
},
{
path
:
"
/createNewGroup
"
,
name
:
"
createNewGroup
"
,
component
:
()
=>
import
(
"
../views/CreateNewGroupView.vue
"
),
beforeEnter
:
guardRoute
,
},
{
path
:
"
/addNewItem
"
,
name
:
"
addNewItem
"
,
component
:
()
=>
import
(
"
../views/AddNewItemView.vue
"
),
beforeEnter
:
guardRoute
,
},
];
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment