npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ctng/auth

v5.0.2

Published

This package provides an authentication module including public/protected guards and interceptors.

Downloads

7

Readme

Auth

This package provides an authentication module including public/protected guards and interceptors.

Usage

  1. Provide an AuthConfig configuration
const authConfig: AuthConfig = {
  protectedDefaultUri: {
    uri: '/auth/protected',
  },
  publicDefaultUri: {
    uri: '/auth/public',
  },
  toProtectedUriAfterLogin: true,
  refreshTokenIntervalMinutes: 60,
};
  • protectedDefaultUri: The external or internal default uri for the protected area. Is used as redirection if user is authorized but on a route protected by PublicGuard.
  • publicDefaultUri: The external or internal default uri for the publi area. Is used as redirection if user is not authorized but on a route protected by ProtectedGuard.
  • toProtecteUriAft erLogin: Indicates if auth should redirect to protected default after login was successful.
  • refreshTokenIntervalMinutes: Interval in minutes to check for token refresh.
  1. Create a custom auth provider by implementing AuthProvider
@Injectable()
export class MyAuthProvider implements AuthProvider {
  public isAuthorized(): boolean {
    return !!sessionStorage.getItem('accessToken');
  }

  public getAccessToken(): string {
    return sessionStorage.getItem('accessToken');
  }

  public checkRefreshToken?(): boolean {
    // Optional: check if token should be refreshed (can be enabled/disabled in config)
    return sessionStorage.getItem('accessToken').expired === true;
  }

  public refreshToken?(): Observable<any> {
    // Optional: refresh token (can be enabled/disabled in config)
    const refreshToken: string = sessionStorage.getItem('refreshToken');

    return this.http.post('http://localhost:8080/refresh-token', { refreshToken });
  }

  public logout?(): void {
    // Optional: React on logout
    sessionStorage.clearItem('accessToken');
  }

  public login?(user: string, password: string): Observable<any> {
    // Optional: Login if available
  }

  public skipRequest?(request: HttpRequest<any>): boolean {
    // Optional: Skip intercepting specific requests
    return req.url.endsWith('req-to-skip');
  }

  public getHeaders?(token: string): { [name: string]: string | string[] } {
    // Optional: custom headers for interceptors
    return {
      customBearer: token,
    };
  }
  public setInterruptedUrl?(url: string): void {
    // Optional: save the interrupted url when logged out of the system, e.g. by 401
    this.interruptedUrl = url;
  }
}
  1. Import AuthModule and provide config and providers
{
  imports: [
    AuthModule,
  ]
  providers: [
    {
      provide: AUTH_CONFIG,
      useValue: authConfig,
    },
    {
      provide: AuthProvider,
      useClass: MyAuthProvider,
    },
  ],
};
  1. Use ProtectedGuard to protect protected routes and use PublicGuard to protect public routes.
const protectedRoute: Routes = [
  {
    path: '',
    component: SomeComponent,
    canActivate: [ProtectedGuard],
  },
];

In this example the user can only access SomeComponent if the MyAuthProvider returns true for isAuthorized. If not, the user is redirected to publicDefaultUri from AuthConfig. Same happens for the PublicGuard, just the other way around.