ng-jwt
Provides an Angular (2-5) auth module to handle authentication based on JWT.
tnx angular-jwt
Feature status:
Feature | Status | Docs |
---|---|---|
AuthenticationService | Available | README |
TokenService | Available | README |
AuthHttp | Available | README |
Auth | Available | README |
LoggedInAuth | Available | README |
LoggedOutAuth | Available | README |
Installation
ng-jwt is available on NPM
$ npm install ng-jwt --save
Setup & Usage
Once the module has been installed, you need to include NgJwtModule
into your root module:
;...
In the forRoot
function you can specify a custom config as well.
Feature status:
Feature | Desc | Default |
---|---|---|
loginEndPoint * | Endpoint url for login with AuthenticationService | [null / Require] |
loginTokenName | Token object name for reading from result | access_token |
loginParams | additional params for sending login request | [null / optional] |
headerName | set Authorization header name for AuthHttp Requests |
Authorization |
headerPrefix | Authorization value for AuthHttp Requests |
Bearer |
guards | Logged[in/out] guard redirect router name | [null] |
Login && Logout
AuthenticationService
service comes withlogout
and login
function built-in:
;;
Manually Login & Logout
In case of of needing a customized Login/Logout functionality, you may use TokenService
.
;;;
Sending Requests
ng-jwt
uses HttpInterceptor
to modify HttpClient
headers for Authentication. So while using HttpClientModule
, ng-jwt
would send the Authentication headers alongside the request.
for angular < 4.3:
If you want to send a request with the Authorization
header set with the JWT token you can use the AuthHttp
class.
It will set the authentication headers on the request on the fly.
;...
Login Validation
Auth
class provides another helper method for authentication validation. By using Auth.loggedIn
function
you can check if the client is logged in. This method returns a Boolean
.
;;
Default Auth Guards
If you want to loggedIn in router:
for authModule
config:
;...
for route config:
const routes: Routes = [
{path: 'family', component: FamilyListComponent, canActivate: [LoggedInAuth]},
{path: 'unauthorized', component: UnAuthorizedComponent}
];
License
ng-jwt is released under MIT license.