ns-angular-jwt
by slejnej | v1.0.0
JSON Web Token helper library for NS Angular
npm i --save ns-angular-jwt

Nativescript - Angular - JWT

  • Angular v11+ and RxJS v6+ -> v1.0

The library provides an HttpInterceptor which automatically attaches a JSON Web Token to HttpClient requests.

Installation

# installation with tns
tns install slejnej/ns-angular-jwt

Usage

  1. make sure you have credentials from your any OAuth2 service
  2. create your injectable AuthGuard service in app root as follows
// file - auth-guard.service
import all_needed_used_and_injected!!!

@Injectable()
export class AuthGuard implements CanActivate {

subscription: Subscription;

constructor(private routerExtensions: RouterExtensions, protected authService: AuthService,
protected userService: UserService, private storage: SecureStorageService) { }

canActivate() {
if (this.authService.isAuthenticated()) {
console.log('guard OK');
return true;
}
else {
console.log('guard FAIL');
// logout user and redirect
return false;
}
}
  1. in your app-routing.module.ts check that you have defined auth providers as:
import { AuthGuard } from "~/auth-guard.service";
export const authProviders = [
AuthGuard
];
  1. create new token.interceptor.ts