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

@will2hew/nestjs-auth

v1.1.3

Published

<h1 align="center"> nestjs-auth </h1>

Downloads

3

Readme

Installation

$ npm i --save @will2hew/nestjs-auth

Usage

Import the User and Session entities, and register the AuthModule

import { AuthModule, User, Session } from "@will2hew/nestjs-auth";

@Module({
  imports: [
    TypeOrmModule.forRoot({
      // connection options
      entities: [User, Session],
    }),
    AuthModule.register({
      prefix: "/auth",
      cookie: {
        name: "sid",
        secret: "super-secret",
        secure: false, // set to true in production
      },
      sessionMaximumAge: 60 * 60 * 24, // 24 hours
    }),
  ],
})
export class AppModule {}

Create a new user

const user = new User();

user.email = "[email protected]";
user.password = "password";

user.firstName = "John";
user.lastName = "Smith";

await this.userRepository.save(user);

Sign in as the user

POST /auth/sign-in
Content-Type: application/json

{
    "email": "[email protected]",
    "password": "password"
}

Protecting endpoints

nestjs-auth provides a guard to protect backend routes.

import { AuthGuard } from "@will2hew/nestjs-auth";

@Controller()
@UseGuards(AuthGuard)
export class AppController {
  @Get()
  getData() {
    return "Hello, World!";
  }
}

You can also require the user has the correct role

import { AuthGuard, Roles } from "@will2hew/nestjs-auth";

@Controller()
@UseGuards(AuthGuard)
export class AppController {
  @Roles("admin")
  @Get("admin")
  getAdminData() {
    return "Top secret!";
  }
}

Accessing the signed in user

You will typically want to access the signed in user to only respond with data relevant to them. nestjs-auth provides a decorator for this situation.

import { AuthGuard, CurrentUser, User } from "@will2hew/nestjs-auth";

@Controller()
@UseGuards(AuthGuard)
export class AppController {
  @Get("me")
  getMe(@CurrentUser() user: User) {
    return user;
  }
}

Extending the User

The default nestjs-auth user offers a set of commonly used user profile fields, but if you'd like to go beyond these you can extend the BaseUser class.

@Entity()
export class OrganizationUser {
  @PrimaryGeneratedColumn("uuid")
  id: string;

  @Column()
  organizationId: string;
}

And provide it during registration

@Module({
  imports: [
    TypeOrmModule.forRoot({
      // connection options
      entities: [OrganizationUser, Session],
    }),
    AuthModule.register({
      userEntity: OrganizationUser,
      // rest of your configuration
    }),
  ],
})
export class AppModule {}

User API

Fields

| Field | Type | Required | Description | | ----------------- | ------------------- | -------- | ------------------------------------------------------------- | | id | string \| number | ✅ | The primary identifier for the user. | | email | string | ✅ | The users email. | | password | string | ✅ | The users password. Automatically hashed when set or updated. | | firstName | string | × | The users first name. | | lastName | string | × | The users last name. | | roles | string[] | ✅ | A string array of the users role(s). | | emailVerifiedAt | Date | × | The date and time the users email was marked verified. |

Methods

verifyEmail()

Sets emailVerifiedAt to the current date and time.

Example:

await user.verifyEmail();