Javascript 只有确认用户才能加载到页面的地方?角度9
我需要创建一个保护,只有在其中isConfirmed==true的用户才能加载到页面。 我已经为canLoad和canActivate提供了一个保护,但是在哪里或如何实现以及这个逻辑。我已经准备好让你知道我在做什么了Javascript 只有确认用户才能加载到页面的地方?角度9,javascript,angular,routes,Javascript,Angular,Routes,我需要创建一个保护,只有在其中isConfirmed==true的用户才能加载到页面。 我已经为canLoad和canActivate提供了一个保护,但是在哪里或如何实现以及这个逻辑。我已经准备好让你知道我在做什么了 canActivate() { const userData = JSON.parse(localStorage.getItem("userData")); const authData = JSON.parse(localStorage.g
canActivate() {
const userData = JSON.parse(localStorage.getItem("userData"));
const authData = JSON.parse(localStorage.getItem("authData"));
const route = this.location.path();
if (!userData && !authData) {
return true;
} else {
if (route == '/login' || route === '/register' || route === '/forgot-password') {
this.location.back();
return false;
} else {
return true;
}
}
}
canLoad() {
const userData = JSON.parse(localStorage.getItem("userData"));
const authData = JSON.parse(localStorage.getItem("authData"));
// console.log('userData' , userData.isConfirmed)
if (userData && authData) {
// logged in so return true
return true;
}
// not logged in so redirect to login page with the return url
this.userService.logout();
this.router.navigate(["/login"]);
return false;
}
及任何路线:
{
path: "questionnaire",
loadChildren: () =>
import("./questionnaire/questionnaire.module").then(
(m) => m.QuestionnaireModule
),
canLoad: [AuthGuard]
},
{
path: "login",
loadChildren: () =>
import("./login/login.module").then((m) => m.LoginModule),
canActivate: [AuthGuard]
},
{
path: "my-clients",
loadChildren: () =>
import("./my-clients/my-clients.module").then((m) => m.MyClientsModule),
canLoad: [AuthGuard]
}
好的,这很好,但是如果userData.isConfirmed只能进入页面,在哪里或如何设置?
我正在尝试这样做:
canLoad() {
const userData = JSON.parse(localStorage.getItem("userData"));
const authData = JSON.parse(localStorage.getItem("authData"));
// console.log('userData' , userData.isConfirmed)
if (userData && authData && userData.isConfirmed) {
// logged in so return true
return true;
}
// not logged in so redirect to login page with the return url
this.userService.logout();
this.router.navigate(["/login"]);
return false;
}
但这不是工作,因为我有用户必须填写的任何问题,并且无法加载到问卷路由中,因为userData.isconfired不是真的。在问卷调查后,userData.isconfirm为true。只需从问卷路由中移除防护即可。为什么要在登录页面上设置防护,登录页面应该是每个人都可以访问的。仅在每个人都无法访问或只有在登录后才能访问的路线上设置警卫。