P
P
Pogran2016-11-14 16:43:18
Angular
Pogran, 2016-11-14 16:43:18

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

3 answer(s)
V
Vitaly, 2016-11-14
@vitali1995

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;
  }
}

3. In the description of the paths, we indicate the class responsible for checking access:
{
    path: 'logout',
    component: LogoutComponent,
    canActivate: [AuthGuard]
  },
  {
    path: 'login',
    component: LoginComponent,
    canActivate: [GuestGuard]
  },

D
Dmitry Klusevich, 2016-11-14
@dimka-dooz

Built-in http it can. This is more a question for a third-party api.

P
Pogran, 2016-11-15
@Pogran

By the way, I found this manual jasonwatmore.com/post/2016/08/16/angular-2-jwt-aut...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question