我正在使用 auth gaurd 来防止我的 Angular 应用程序中的 URL 操作并将 URL 操作重定向到登录页面。但这会导致问题,即使在成功登录后,刷新网页时也会将其视为 URL 操作,并通过重定向到登录页面将用户注销。请注意,我没有使用 JWT 或任何类似的身份验证机制. 这是基于后端数据库返回的密码的普通身份验证。到目前为止,我还没有维护任何会话。下面我附上了我已将路径映射到路线的片段:
const appRoutes: Routes = [
{ path: '', component: LoginComponent },
{ path: 'dashboard', canActivate: [AuthGuard], component: DashboardComponent },
{ path: 'file-uploader', canActivate: [AuthGuard], component: FileUploaderComponent },
{ path: 'register', component: RegisterComponent },
{ path: 'login', component: LoginComponent }
];
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private user: UserService, private router: Router) {}
canActivate() {
if (this.user.getUserLoggedIn()) {
return true;
}
else
return false;
}
}
请帮我解决这个问题。在此先感谢。