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

@lcgroup.core/auth

v9.0.0

Published

Authentication module

Downloads

10

Readme

@lcgroup.core/auth

AuthModule allows to add authentication/authorization mechanism (based on security token service) to your project.

Features

  • JWT integration
  • Custom user support
  • Extensibility auth logic (Custom auth service support)
  • Available two ways of token storage - via localstorage (by default) or cookie

Setup

AuthModule works in pair with JwtModule.

Setup example:

    export function jwtOptionsFactory(authTokenService: AuthTokenService) {
        return {
            tokenGetter: () => {
                return authTokenService.getToken();
            },
            allowedDomains: environment.allowedDomains
        };
    }
    // ...
    JwtModule.forRoot({
        jwtOptionsProvider: {
            provide: JWT_OPTIONS,
            useFactory: jwtOptionsFactory,
            deps: [AuthTokenService]
        }
    }),
    AuthModule.forRoot({
        stsParams: {
            client_id: 'your_client_id',
            client_secret: 'some_secret',
            scope: 'your scopes divided with space'
        },
        tokenEndpoint:
            environment.identityServerUrl + AUTH_OPTIONS_DEFAULTS.tokenEndpoint 
    })

Options:

export interface AuthOptions<T> {
    userType?: new() => T; // User class. default: User
    tokenName?: string; // token storage name. default: 'access_token'
    tokenEndpoint?: string; // token endpoint url. default: '/identity/connect/token'
    stsParams?: OAuthParams;
}
export interface OAuthParams {
   scope: string;
   client_id: string;
   client_secret: string;
}

It is possible to use custom user that extends class User:

export class CustomUser extends User {
  public custom_claim: string;
}

To access the claim display mode, you need to pass the enableDevHotkey parameter:

    AuthModule.forRoot({
        ...,
        { enableDevHotkey: true }
    })

To enable the mode, press the key combination Ctrl+F7.

Usage

AuthService

AuthService is the main part of authentication and authorization control.

@Inject(AUTH_SERVICE) private authService: AuthService<User>
login(login: string, password: string): Observable<T> // http call for login and update user
logout(): T // logout (clear store and update user$)
authorize(claim: string | string[] = null) // check user claims
getUser(): T // current user entity from store

user$:  BehaviorSubject<T>; // automatically updating user entity

Authorize directive

Checks user claims and removes inner html if user hasn't got specified claim:

<li routerLinkActive="active" *authorize="'app.debtors'">
    content
</li>

You might want to hide content for anonymous users:

<li routerLinkActive="active" *authorize>
    content
</li>

AuthorizeControl directive

Checks user claims and set 'disabled' attribute for button/input/form, etc.

<button type="button" authorizeControl="users:roles_EDIT">
    Button
</button>

Checks user claims and set 'disabled' attribute and styles (pointer-events, opacity) for span/i

<span authorizeControl="users:list_EDIT">
    X
</span>

You must handle http statuses by your own. It's project specific. Implement HttpInterceptor and provide it.

reason: https://limezaim.atlassian.net/browse/KRNL-22

@Injectable()
export class AuthInterceptor implements HttpInterceptor {
    constructor(
        private injector: Injector // https://github.com/angular/angular/issues/18224
    ) { }

    intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        const authService = this.injector.get(AUTH_SERVICE);
        const router = this.injector.get(Router);
        return next.handle(req)
            .catch(error => {
                if (error && error.status === 401) {
                    authService.logout();
                    router.navigateByUrl('/');
                }
                return throwError(error);
            });
    }
}
{
    provide: HTTP_INTERCEPTORS,
    useClass: AuthInterceptor,
    multi: true
}

Custom AuthService implementation example

class CustomAuthService<T extends User> extends AuthService<T> {

    private logoutStatus = false;

    constructor (
        authApi: AuthApiService,
        authTokenService: AuthTokenService,
        jwtHelperService: JwtHelperService,
        @Inject(AUTH_OPTIONS) options: AuthOptions<T>
      ) {
        super(authApi, authTokenService, jwtHelperService, options);
    }

    get getLogoutStatus(): boolean {
        return this.logoutStatus;
    }

    logout() {
      const logoutResult = super.logout();
      this.logoutStatus = true;
      return logoutResult;
    }
}
    AuthModule.forRoot({ userType: User }, CustomAuthService)

Cookie storage

    npm install ngx-cookie@^4.1.2

Commands

Tests

npm run test npm run test:watch

Build

npm run build

Release

npm run release