로그인하지 않은 사용자, 예를 들어 "/ items"authguard가 false를 반환하고 "/ login"을 탐색하지 않는 보안 루트로 이동하려는 경우.Angular2 Authguard 로그인 경로
export const APP_ROUTES: Routes = [
{path: '', component: PublicComponent, children: PUBLIC_ROUTES},
{
path: '',
component: SecureComponent,
resolve: {user: UserResolver},
canActivate: [AuthGuard],
children: SECURE_ROUTES
},
{path: '', redirectTo: '/login', pathMatch: 'full'}
];
AuthGuard 번호 :
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private userService: UserService) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot){
let url: string = state.url;
return this.checkLogin(url);
}
checkLogin(url: string): Observable<boolean> {
this.userService.redirectUrl = url;
return this.userService.getPrincipal()
.map(user => {
if(user)
return true;
return false;
});
}
}
문제는 여기에 무엇입니까 :? –
사용자가 로그인하지 않은 경우 로그인 페이지를 탐색하고 싶습니까? :) – vangoo
@vangoo 아래 코드를 사용해 보셨습니까? –