All Projects → serhiisol → Ngx Auth

serhiisol / Ngx Auth

Licence: mit
Angular 7+ Authentication Module

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Ngx Auth

Nodejs Auth
Implementation of node.js authentication with social login ✌️, user impersonation 💅, and no passport.js required 💁
Stars: ✭ 201 (-9.46%)
Mutual labels:  authentication
Starter Pack
Combines React (ft. hooks), Redux, Redux-saga and TypeScript with Auth0 as a starting point for modern web apps with solid authentication
Stars: ✭ 209 (-5.86%)
Mutual labels:  authentication
Lock.swift
A Swift & iOS framework to authenticate using Auth0 and with a Native Look & Feel
Stars: ✭ 215 (-3.15%)
Mutual labels:  authentication
Libauth
An ultra-lightweight, zero-dependency JavaScript library for Bitcoin, Bitcoin Cash, and Bitauth applications.
Stars: ✭ 205 (-7.66%)
Mutual labels:  authentication
Go Guardian
Go-Guardian is a golang library that provides a simple, clean, and idiomatic way to create powerful modern API and web authentication.
Stars: ✭ 204 (-8.11%)
Mutual labels:  authentication
Run Aspnetcore Realworld
E-Commerce real world example of run-aspnetcore ASP.NET Core web application. Implemented e-commerce domain with clean architecture for ASP.NET Core reference application, demonstrating a layered application architecture with DDD best practices. Download 100+ page eBook PDF from here ->
Stars: ✭ 208 (-6.31%)
Mutual labels:  authentication
Express Graphql Boilerplate
Express GraphQL API with JWT Authentication and support for sqlite, mysql, and postgresql
Stars: ✭ 201 (-9.46%)
Mutual labels:  authentication
Authentic
Authentication for microservices.
Stars: ✭ 221 (-0.45%)
Mutual labels:  authentication
Qtfirebase
An effort to bring Google's Firebase C++ API to Qt + QML
Stars: ✭ 208 (-6.31%)
Mutual labels:  authentication
Auth0 Vue Samples
Auth0 Integration Samples for Vue.js Applications
Stars: ✭ 215 (-3.15%)
Mutual labels:  authentication
Superb
Pluggable HTTP authentication for Swift.
Stars: ✭ 206 (-7.21%)
Mutual labels:  authentication
Registration Login Spring Hsql
Registration and Login Example with Spring Security, Spring Boot, Spring Data JPA, HSQL, JSP
Stars: ✭ 208 (-6.31%)
Mutual labels:  authentication
Mosquitto Go Auth
Auth plugin for mosquitto.
Stars: ✭ 212 (-4.5%)
Mutual labels:  authentication
Django Graphql Auth
Django registration and authentication with GraphQL.
Stars: ✭ 200 (-9.91%)
Mutual labels:  authentication
Apigatewaydemo
🌱 Simple samples that use Ocelot to build API Gateway.
Stars: ✭ 217 (-2.25%)
Mutual labels:  authentication
Libreauth
LibreAuth is a collection of tools for user authentication.
Stars: ✭ 201 (-9.46%)
Mutual labels:  authentication
Sorcery
Magical authentication for Rails 3 & 4
Stars: ✭ 2,345 (+956.31%)
Mutual labels:  authentication
Stormpath Sdk Java
Official Java SDK for the Stormpath User Management REST API
Stars: ✭ 221 (-0.45%)
Mutual labels:  authentication
Nuxt Basic Auth Module
Provide basic auth your Nuxt.js application
Stars: ✭ 218 (-1.8%)
Mutual labels:  authentication
Passport
Passport module for Nest framework (node.js) 🔑
Stars: ✭ 211 (-4.95%)
Mutual labels:  authentication

Angular 7+ Authentication

This package provides authentication module with interceptor

npm install ngx-auth --save

For older versions of angular see Older Versions section.

Full example

Full example you can find in this repo serhiisol/ngx-auth-example

Authentication module

Authentication modules provides ability to attach authentication token automatically to the headers (through http interceptors), refresh token functionality, guards for protected or public pages and more.

Usage

  1. Import AuthService interface to implement it with your custom Authentication service, e.g.:
import { AuthService } from 'ngx-auth';

@Injectable()
export class AuthenticationService implements AuthService {

  private interruptedUrl: string;

  constructor(private http: Http) {}

  public isAuthorized(): Observable<boolean> {
    const isAuthorized: boolean = !!sessionStorage.getItem('accessToken');

    return Observable.of(isAuthorized);
  }

  public getAccessToken(): Observable<string> {
    const accessToken: string = sessionStorage.getItem('accessToken');

    return Observable.of(accessToken);
  }

  public refreshToken(): Observable<any> {
    const refreshToken: string = sessionStorage.getItem('refreshToken');

    return this.http
      .post('http://localhost:3001/refresh-token', { refreshToken })
      .catch(() => this.logout())
  }

  public refreshShouldHappen(response: HttpErrorResponse): boolean {
    return response.status === 401;
  }

  public verifyRefreshToken(req: HttpRequest<any>): boolean {
    return req.url.endsWith('refresh-token');
  }

  public skipRequest(req: HttpRequest<any>): boolean {
    return req.url.endsWith('third-party-request');
  }

  public getInterruptedUrl(): string {
    return this.interruptedUrl;
  }

  public setInterruptedUrl(url: string): void {
    this.interruptedUrl = url;
  }

}
  1. Specify PublicGuard for public routes and ProtectedGuard for protected respectively, e.g.:
const publicRoutes: Routes = [
  {
    path: '',
    component: LoginComponent,
    canActivate: [ PublicGuard ]
  }
];
const protectedRoutes: Routes = [
  {
    path: '',
    component: ProtectedComponent,
    canActivate: [ ProtectedGuard ],
    children: [
      { path: 'dashboard', loadChildren: './dashboard/dashboard.module#DashboardModule' }
    ]
  }
];
  1. Create additional AuthenticationModule and provide important providers and imports, e.g.:
import { NgModule } from '@angular/core';
import { AuthModule, AUTH_SERVICE, PUBLIC_FALLBACK_PAGE_URI, PROTECTED_FALLBACK_PAGE_URI } from 'ngx-auth';

import { AuthenticationService } from './authentication.service';

@NgModule({
  imports: [ AuthModule ],
  providers: [
    { provide: PROTECTED_FALLBACK_PAGE_URI, useValue: '/' },
    { provide: PUBLIC_FALLBACK_PAGE_URI, useValue: '/login' },
    { provide: AUTH_SERVICE, useClass: AuthenticationService }
  ]
})
export class AuthenticationModule {

}

where,

  • PROTECTED_FALLBACK_PAGE_URI - main protected page to be redirected to, in case if user will reach public route, that is protected by PublicGuard and will be authenticated

  • PUBLIC_FALLBACK_PAGE_URI - main public page to be redirected to, in case if user will reach protected route, that is protected by ProtectedGuard and won't be authenticated

  • AUTH_SERVICE - Authentication service token providers

  1. Provide your AuthenticationModule in your AppModule

Customizing authentication headers

By default, requests are intercepted and a { Authorization: 'Bearer ${token}'} header is injected. To customize this behavior, implement the getHeaders method on your AuthenticationService

After login redirect to the interrupted URL

The AuthService has an optional method setInterruptedUrl which saves the URL that was requested before the user is redirected to the login page. This property can be used in order to redirect the user to the originally requested page after he logs in. E.g. in your login.component.ts (check also AuthService implementation above):

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html'
})
export class LoginComponent {

  constructor(
    private router: Router,
    private authService: AuthenticationService,
  ) { }

  public login() {
    this.authService
      .login()
      .subscribe(() =>
        this.router.navigateByUrl(this.authService.getInterruptedUrl())
      );
  }
}

Older Versions

For angular 6, use version 4.1.0

npm install [email protected] --save

For angular 5, use version 3.1.0

npm install [email protected] --save

For angular 4, use version 2.2.0

npm install [email protected] --save
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].