Answer the question
In order to leave comments, you need to log in
What is the best way to do authorization in angular2?
The point is, I want to get involved with a third-party api. That is, the data is sent to a third-party site, and if the response is 200, then authorize it on the site. What is the best way to do authorization on angular2?
Answer the question
In order to leave comments, you need to log in
All logic in Angular 2 is done through service classes. It is enough to perform 3 simple steps:
1. For example, in user.service.ts we will add information about the authorized user.
2. Let's create the AuthGuard service, which will be responsible for authorization. It must implement the CanActivate interface
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private userService: UserService) {}
canActivate(route: ActivatedRouteSnapshot, state:RouterStateSnapshot): Promise<boolean>|boolean {
return this.userService.isAuth;
}
}
{
path: 'logout',
component: LogoutComponent,
canActivate: [AuthGuard]
},
{
path: 'login',
component: LoginComponent,
canActivate: [GuestGuard]
},
Built-in http it can. This is more a question for a third-party api.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question