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 🙏

© 2025 – Pkg Stats / Ryan Hefner

angular-authjs

v0.0.18

Published

Uma biblioteca de autenticação Angular

Readme

OBS: UNDER DEVELOPMENT AND TESTS

Angular Authjs

This project was generated using Angular CLI version 20.2.0.

Getting Started

Creating an Angular Project with SSR

angular-authjs Uses a JWT (JSON Web Token) strategy stored in cookies for enhanced security.

To use the angular-authjs library, start by creating a new Angular project with Server-Side Rendering (SSR) support:

  1. Install the Angular CLI globally (if not already installed):

    npm install -g @angular/cli
  2. Create a new Angular project with SSR enabled:

    ng new my-auth-app --ssr
  3. Navigate to the project directory:

    cd my-auth-app
  4. Install package:

    npm install angular-authjs
  5. How to use

Integrate the angular-authjs library into your Angular application with SSR. Below is an example of how to set it up in your server.ts file, including the implementation of GitHub and Google OAuth providers, this is how your server ts should be looked, delete the default writeResponseToNodeResponse function from your server.ts, as createAuthenticationRouter will handle the response for you.

//server.ts
// server.ts
import {
  AngularNodeAppEngine,
  createNodeRequestHandler,
  isMainModule,
} from '@angular/ssr/node';
import { createAuthenticationRouter, getProtectedRoutes, getPublicRoutes, ProviderConfig, Session } from "angular-authjs";
import express from 'express';
import { join } from 'node:path';
import { routes } from './app/app.routes';
import { environment } from './environments/environment';

const browserDistFolder = join(import.meta.dirname, '../browser');

const app = express();
const angularApp = new AngularNodeAppEngine();

app.use(
  express.static(browserDistFolder, {
    maxAge: '1y',
    index: false,
    redirect: false,
  }),
);

app.use(
  createAuthenticationRouter({
    providers: [
      {
        type: "credentials",
        id: "credentials",
        secret: crypto.randomUUID(),
        authorize: async (credentials) => {
          return new Promise<Session>((resolve) => {
            setTimeout(() => {
              resolve({
                user: {
                  id: crypto.randomUUID(),
                  email: credentials.username,
                  name: "Gabriel",
                },
                expires: "",
              });
            }, 250);
          });
        },
      } as ProviderConfig,
      {
        type: "github",
        id: "github",
        clientId: environment["GITHUB_CLIENT_ID"]!,
        clientSecret: environment["GITHUB_CLIENT_SECRET"]!,
        redirectUri: "http://localhost:4200/api/auth/callback/github",
      } as ProviderConfig,
      {
        type: "google",
        id: "google",
        clientId: environment["GOOGLE_CLIENT_ID"]!,
        clientSecret: environment["GOOGLE_CLIENT_SECRET"]!,
        redirectUri: "http://localhost:4200/api/auth/callback/google",
      } as ProviderConfig,
    ],
    secret: environment["AUTH_SECRET"]!,
    protectedRoutes: getProtectedRoutes(routes),
    publicRoutes: getPublicRoutes(routes),
    angularApp,
    bootstrap: angularApp,
  })
);

if (isMainModule(import.meta.url)) {
  const port = process.env['PORT'] || 4000;
  app.listen(port, (error) => {
    if (error) {
      throw error;
    }

    console.log(`Node Express server listening on http://localhost:${port}`);
  });
}
export const reqHandler = createNodeRequestHandler(app);
  1. Defining Protected Routes

The getProtectedRoutes function checks which routes use guards. Create the following components for redirection with callbackUrl:

UnauthorizedComponent → unauthorized route

NotFoundComponent → not-found route


import { Routes } from '@angular/router';
import { RouteGuard } from 'angular-authjs';
import { UnauthorizedComponent } from './components/unauthorized.component';
import { NotFoundComponent } from './components/not-found.component';
import { DashboardComponent } from './components/dashboard.component';

import { LoginComponent } from './components/login.component';

export const routes: Routes = [
    { path: 'unauthorized', component: UnauthorizedComponent },
    { path: 'dashboard', component: DashboardComponent, canActivate: [RouteGuard] },
    { path: 'login', component: LoginComponent },
    { path: '**', component: NotFoundComponent },
];

Login Example


import { Component, OnInit, inject } from '@angular/core';
import { FormBuilder, FormGroup, Validators, ReactiveFormsModule } from '@angular/forms';
import { SessionProvider } from 'angular-authjs';
import { Router } from '@angular/router';
import { CommonModule } from '@angular/common';
import { signal } from '@angular/core';
import { filter } from 'rxjs';

@Component({
  selector: 'app-login',
  standalone: true,
  imports: [CommonModule, ReactiveFormsModule],
  template: `
    <h2>Login</h2>
    <form [formGroup]="loginForm" (ngSubmit)="onSubmit()">
      <label for="username">Usuário</label>
      <input id="username" formControlName="username" required />
      <div *ngIf="loginForm.get('username')?.invalid && loginForm.get('username')?.touched" style="color:red">Usuário é obrigatório</div>
      <label for="password">Senha</label>
      <input id="password" type="password" formControlName="password" required />
      <div *ngIf="loginForm.get('password')?.invalid && loginForm.get('password')?.touched" style="color:red">Senha é obrigatória</div>
      <button type="submit" [disabled]="loginForm.invalid">Entrar</button>
    </form>
    <button (click)="signInWithProvider('github')">Entrar com GitHub</button>
    <button (click)="signInWithProvider('google')">Entrar com Google</button>
    <div *ngIf="error()" style="color:red">{{ error() }}</div>
  `,
})
export class LoginComponent implements OnInit {
  loginForm: FormGroup;
  error = signal('');

  private sessionProvider = inject(SessionProvider);
  private router = inject(Router);
  private fb = inject(FormBuilder);

  constructor() {
    this.loginForm = this.fb.group({
      username: ['', Validators.required],
      password: ['', Validators.required],
    });
  }

  ngOnInit(): void {
    this.sessionProvider.isAuthenticated$
      .pipe(filter(isAuth => isAuth))
      .subscribe(() => {
        this.router.navigateByUrl('/dashboard');
      });
  }

  onSubmit() {
    if (this.loginForm.invalid) return;
    const { username, password } = this.loginForm.value;
    this.sessionProvider.signIn('credentials', {
      username,
      password,
      callbackUrl: '/dashboard',
    }).subscribe({
      next: (res) => {
        this.router.navigateByUrl(res.redirectTo);
      },
      error: () => {
        this.error.set('Usuário ou senha inválidos');
      }
    });
  }

  signInWithProvider(provider: 'github' | 'google') {
    this.sessionProvider.signIn(provider, {
      callbackUrl: '/dashboard',
    }).subscribe({
      next: (res) => {
        window.location.href = res.redirectTo;
      },
      error: () => {
        this.error.set(`Erro ao autenticar com ${provider}`);
      }
    });
  }
}
  1. Using the SessionProvider Service

The SessionProvider service provides methods to manage authentication sessions, including sign-in with GitHub and Google. Below is the service implementation and how to use it:

// app.component.ts
import { Component, OnInit } from "@angular/core";
import { SessionProvider } from "./session-provider.service";
import { Router } from "@angular/router";

@Component({
  selector: "app-root",
  template: `
    <div *ngIf="isAuthenticated; else login">
      <p>Welcome, {{ session()?.user.name }}!</p>
      <button (click)="signOut()">Sign Out</button>
    </div>
    <ng-template #login>
      <button (click)="signInWithGitHub()">Sign In with GitHub</button>
      <button (click)="signInWithGoogle()">Sign In with Google</button>
    </ng-template>
  `,
})
export class AppComponent implements OnInit {
  session = this.sessionProvider.session.asObservable();
  isAuthenticated = false;

  constructor(private sessionProvider: SessionProvider, private router: Router) {}

  ngOnInit() {
    this.sessionProvider.getSession().subscribe({
      next: (session) => this.sessionProvider.session.set(session),
      error: () => this.sessionProvider.session.set(null),
    });
    this.isAuthenticated = this.sessionProvider.isAuthenticated();
  }

  signInWithGitHub() {
    this.sessionProvider.signIn("github", { callbackUrl: "/dashboard" }).subscribe({
      next: (response) => {
        window.location.href = response.redirectTo; // Redirect to GitHub auth URL
      },
      error: (error) => console.error("GitHub sign-in failed", error),
    });
  }

  signInWithGoogle() {
    this.sessionProvider.signIn("google", { callbackUrl: "/dashboard" }).subscribe({
      next: (response) => {
        window.location.href = response.redirectTo; // Redirect to Google auth URL
      },
      error: (error) => console.error("Google sign-in failed", error),
    });
  }

  signOut() {
    this.sessionProvider.signOut("/").subscribe({
      next: () => {
        this.sessionProvider.session.set(null);
        this.router.navigate(["/"]);
      },
      error: (error) => console.error("Sign-out failed", error),
    });
  }
}