# nestjs-easy-auth-mongo

> MongoDB storage provider for NestJS Easy Auth.

Latest version **0.1.0** (published 2023-10-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install nestjs-easy-auth-mongo
pnpm add nestjs-easy-auth-mongo
yarn add nestjs-easy-auth-mongo
bun add nestjs-easy-auth-mongo
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2023-10-19 |
| First published | 2023-10-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 18 |
| Unpacked size | 309.3 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | bpisano |
| Maintainers | bpisano |

## Links

- npm: https://www.npmjs.com/package/nestjs-easy-auth-mongo
- Repository: https://github.com/bpisano/nestjs-easy-auth-mongo
- Homepage: https://github.com/bpisano/nestjs-easy-auth-mongo#readme
- Issues: https://github.com/bpisano/nestjs-easy-auth-mongo/issues
- npm.io page: https://npm.io/package/nestjs-easy-auth-mongo

## Dependencies (18)

- [bcrypt](https://npm.io/package/bcrypt.md) ^5.1.1
- [date-fns](https://npm.io/package/date-fns.md) ^2.30.0
- [mongoose](https://npm.io/package/mongoose.md) ^7.6.2
- [passport](https://npm.io/package/passport.md) ^0.6.0
- [monkey-db](https://npm.io/package/monkey-db.md) ^0.1.8
- [@nestjs/jwt](https://npm.io/package/@nestjs/jwt.md) ^10.1.1
- [@nestjs/core](https://npm.io/package/@nestjs/core.md) ^10.2.6
- [passport-jwt](https://npm.io/package/passport-jwt.md) ^4.0.1
- [@nestjs/common](https://npm.io/package/@nestjs/common.md) ^10.2.6
- [@nestjs/config](https://npm.io/package/@nestjs/config.md) ^3.1.1
- [passport-local](https://npm.io/package/passport-local.md) ^1.0.0
- [class-validator](https://npm.io/package/class-validator.md) ^0.14.0
- [@nestjs/mongoose](https://npm.io/package/@nestjs/mongoose.md) ^10.0.1
- [@nestjs/passport](https://npm.io/package/@nestjs/passport.md) ^10.0.2
- [model-conversion](https://npm.io/package/model-conversion.md) ^1.0.0
- [nestjs-easy-auth](https://npm.io/package/nestjs-easy-auth.md) ^0.1.0
- [class-transformer](https://npm.io/package/class-transformer.md) ^0.5.1
- [@nestjs/platform-express](https://npm.io/package/@nestjs/platform-express.md) ^10.2.6

## Recent versions

- 0.1.0 (latest) — 2023-10-19

## README

# NestJS Easy Auth Mongo

This package is a storage provider for the [NestJS Easy Auth package](https://github.com/bpisano/nestjs-easy-auth).

# Installation

You can install the package using the following commands:

```bash
$ yarn
yarn add nestjs-easy-auth-mongo

$ npm
npm install nestjs-easy-auth-mongo
```

# Usage

Import the `AuthMongoProviderModule` before importing the `AuthModule` in your `AppModule`:

```typescript
import { AuthMongoModule } from 'nestjs-easy-auth-mongo';

@Module({
  imports: [
    AuthMongoProviderModule.withConfiguration({
      dbName: 'my-database-name',
      uri: 'mongodb://user:password@127.0.0.1:27017',
      schemas: {
        credentials: DBCredentialsSchema,
        user: DBUserSchema
      }
    }),
    AuthModule.withConfiguration({
      // Your auth configuration.
      // For complete documentation, see https://github.com/bpisano/nestjs-easy-auth
    })
  ]
})
export class AppModule {}
```

With this configuration, the NestJS Easy Auth package will use MongoDB as its storage provider.
It will use the provided schemas to store the credentials and the users in the database.

<details>
<summary><strong>Credentials schema example</strong></summary>

```typescript
@Schema({
  collection: 'credentials'
})
export class DBCredentials {
  @Prop()
  public readonly userId: string;

  @Prop()
  public readonly authType: string;

  @Prop()
  public readonly accessToken: string;

  @Prop()
  public readonly refreshToken: string;

  @Prop()
  public readonly accessTokenExpiration: Date;

  @Prop()
  public readonly refreshTokenExpiration: Date;

  public toAppModel: () => Credentials;
}

export type DBCredentialsDocument = HydratedDocument<DBCredentials>;
export const DBCredentialsSchema: MongooseSchema<DBCredentialsDocument> = SchemaFactory.createForClass(DBCredentials);

// This is the method that will be used to convert the database model to the app model.
// For full documentation, see https://github.com/bpisano/nestjs-easy-auth
DBCredentialsSchema.methods.toAppModel = function (): Credentials {
  return new Credentials(
    this.userId,
    this.authType,
    this.accessToken,
    this.refreshToken,
    this.accessTokenExpiration,
    this.refreshTokenExpiration
  );
};
```

</details>

<details>
<summary><strong>User schema example</strong></summary>

```typescript
@Schema({ collection: 'users' })
export class DBUser {
  @Prop()
  public readonly email: string;

  @Prop()
  public readonly hashedPassword?: string;

  public toAppModel: () => User;
}

export type DBUserDocument = HydratedDocument<DBUser>;
export const DBUserSchema: MongooseSchema<DBUserDocument> = SchemaFactory.createForClass(DBUser);

// This is the method that will be used to convert the database model to the app model.
// For full documentation, see https://github.com/bpisano/nestjs-easy-auth
DBUserSchema.methods.toAppModel = function (): User {
  return new User(this._id, this.email, this.hashedPassword);
};
```

</details>

When creating your app models, use the `Document` classes as the database models:

```typsecript
export class Credentials implements CredentialsRepresentation<DBCredentialsDocument, PublicCredentials> {
  // ...
}
```

```typsecript
export class User implements UserRepresentation<DBUserDocument, PublicUser> {
  // ...
}
```

---
_Source: https://npm.io/package/nestjs-easy-auth-mongo · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
