Giter Club home page Giter Club logo

ng-jwt-login's Introduction

Angular 6 jwt login

Built with
  1. A service for http communiction (connector.service.ts)
  2. Routes and Routing Module
  3. Route Guard
  4. Http Interceptor
  5. JWT package: @auth0/angular-jwt
Setup steps

Create 2 components : login and profile

ng g c login --spec false
ng g c profile --spec false

Create a service for http connection with backend

ng g s connector --spec false

Add @auth0/angular-jwt package to play with jwt

npm i --save @auth0/angular-jwt

Create a Routing Module

ng g m app-routing --flat --module=app --spec false

Modify the routing module as following

import { NgModule } from '@angular/core';
import { Routes, RouterModule, Router } from '@angular/router';

import { LoginComponent } from './login/login.component';
import { ProfileComponent } from './profile/profile.component';

const routes: Routes = [
  { path: '', redirectTo: '/login', pathMatch: 'full'},
  { path: 'login', component: LoginComponent },
  { path: 'profile', component: ProfileComponent }
]

@NgModule({
  imports: [ RouterModule.forRoot(routes) ],
  exports: [ RouterModule ]
})
export class AppRoutingModule { }

Put the <router-outlet></router-outlet> in app.component.html

1     Use http interceptor

1.1     Create a service to intercept token in each requests

ng g s token-interceptor --spec false
import { Injectable } from '@angular/core';
import { HttpInterceptor } from '@angular/common/http'

@Injectable({
  providedIn: 'root'
})
export class TokenInterceptorService implements HttpInterceptor {

  constructor() { }

  intercept(req, next) {
    
    if (this.getToken()) {
      let tokenizedReq = req.clone({
        setHeaders : {
          Authorization : "Bearer "
        }
      })
  
      return next.handle(tokenizedReq);
    } else {
      return next.handle(req);
    }

  }

  private getToken() {
    if (!!localStorage.getItem("token")) {
      return localStorage.getItem("token")
    } else {
      return false;
    }
  }
}

1.2     Register the interceptor service in the module

...
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { TokenInterceptorService } from './token-interceptor.service';
...

@NgModule({
  ...
  providers: [
    ...,
    TokenInterceptorService, 
    {
      provide: HTTP_INTERCEPTORS,
      useClass: TokenInterceptorService,
      multi: true
    }
  ]
})
export class AppRoutingModule { }

2     Create a service to handle all http requests

We will create a seperate service that will handle all http requests(get + post) and will also take care of authorization token

2.1     Create the service

ng g s connector --spec false

Content of connector.service.ts

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http'
import { Observable } from 'rxjs';
@Injectable({
  providedIn: 'root'
})
export class ConnectorService {

  constructor(
    private http: HttpClient
  ) { }

  public getRequest(params) {
    let res = this.http.get(
      "http://dp.localhost.com/index.php",
      {
        params : params
      }
    ).subscribe(res => {
      //  Update the token in localstorage if available
      this.updateToken(res);
      // Return the response to the invoking method
      return res;
    });
  }

  public postRequest(params) {

  }

  public updateToken(response) {
    if (response["token"] !== undefined) {
      localStorage.setItem("token", response["token"]);
    }
  }

}

ng-jwt-login's People

Contributors

debojyoti avatar debojyoticn avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.