question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Angular 5 Support

See original GitHub issue

Getting the following warning when upgrading to Angular 5.0.0

npm WARN angular2-jwt@0.2.3 requires a peer of @angular/core@^2.0.0||^4.0.0 but none was installed.
npm WARN angular2-jwt@0.2.3 requires a peer of @angular/http@^2.0.0||^4.0.0 but none was installed.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:66
  • Comments:39

github_iconTop GitHub Comments

66reactions
Lakstoncommented, Nov 15, 2017

In my app.module :

import { JwtModule } from '@auth0/angular-jwt'

@NgModule({
  bootstrap: [AppComponent],
  imports: [
    // ...
    HttpClientModule,
    JwtModule.forRoot({
      config: {
        tokenGetter: () => {
          return localStorage.getItem('access_token');
        },
        whitelistedDomains: ['you API url']
      }
    })
  ]
})

In authentication.service.ts

import { JwtHelperService } from '@auth0/angular-jwt'

@Injectable()
export class AuthenticationService {
  constructor(private http: HttpClient, private jwtHelperService: JwtHelperService) {}

  loggedIn() {
    const token: string = this.jwtHelperService.tokenGetter()

    if (!token) {
      return false
    }

    const tokenExpired: boolean = this.jwtHelperService.isTokenExpired(token)

    return !tokenExpired
  }

... other methods such as reset password etc...
}

In the auth-guard.service.ts

import { Injectable } from '@angular/core';
import { CanActivate, Router } from '@angular/router';
import { AuthenticationService } from '../authentication/authentication.service';

@Injectable()
export class AuthGuard implements CanActivate {
  
  constructor(
    private auth: AuthenticationService,
    private router: Router,
  ) {}

  canActivate(): boolean {
    if (this.auth.loggedIn()) {
      return true;
    }  else {
      localStorage.removeItem('token');
      this.router.navigateByUrl('/login');
      return false;
    }
  }
}

and in the app.routing.ts we have a canActivate: [AuthGuard]

Hope that helps 😃

18reactions
rhino5ohcommented, Dec 7, 2017

So, I just came across this. Only place I’ve ever seen it:

capture

Taken from the top of this page: https://auth0.com/blog/introducing-angular2-jwt-a-library-for-angular2-authentication/

apparently angular2-jwt is deprecated and auth0 did a pretty terrible job of letting users know that. If you follow this tutorial: https://auth0.com/blog/real-world-angular-series-part-1/ and look at its source code on github, you can see how they use JWTs without this library. I’m going to take a stab at removing angular2-jwt from my code and see if I can get it working with the new HttpClient

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular versioning and releases
Actively supported versionslink. The following table provides the status for Angular versions under support. Version, Status, Released, Active ends, LTS ends ...
Read more >
Angular - endoflife.date
Release Released Active Support 15 1 month and 1 week ago. (16 Nov 2022) Ends in 4 months and 3 weeks. (18 M... 14 (...
Read more >
Angular 5 Tutorial: Guide to Your First Angular 5 App - Toptal
Build a notes application from scratch for your first Angular 5 app. Learn Angular CLI, use RxJS, implement Firebase as the back-end, and...
Read more >
Angular Version List & History – Angular 2,4,5,6,7,8 - Guru99
Angular 4 is a web application framework for building JavaScript applications. It supports TypeScript, which compiles to JavaScript and displays ...
Read more >
Angular 5 Features and Benefits — All You Need to Know ...
Angular 5.0 comes with Angular Universal State Transfer API and DOM support for sharing code between server and client-side versions of an application....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found