0.65.2 • Published 6 days ago

@dzangolab/fastify-user v0.65.2

Weekly downloads
-
License
MIT
Repository
github
Last release
6 days ago

@dzangolab/fastify-user

A Fastify plugin that provides an easy integration of user model (service, controller, resolver) in a fastify API.

Requirements

  • @dzangolab/fastify-config
  • @dzangolab/fastify-slonik
  • @dzangolab/fastify-mercurius
  • slonik
  • mercurius
  • supertokens-node

Installation

In a simple repo:

npm install @dzangolab/fastify-user

If using in a monorepo with pnpm:

pnpm add --filter "myrepo" @dzangolab/fastify-user

Usage

Register the user route with your Fastify instance:

import configPlugin from "@dzangolab/fastify-config";
import userRoute from "@dzangolab/fastify-user";
import fastify from "fastify";

import config from "./config";

import type { ApiConfig } from "@dzangolab/fastify-config";
import type { FastifyInstance } from "fastify";

// Create fastify instance
const fastify = Fastify({
  logger: config.logger,
});

// Register fastify-config plugin
fastify.register(configPlugin, { config });

// Register fastify-user route
fastify.register(userRoute);

await fastify.listen({
  port: config.port,
  host: "0.0.0.0",
});

Add resolver in your apps resolver collection

import { usersResolver, userRoutes } from "@dzangolab/fastify-user";

import type { IResolvers } from "mercurius";

const resolvers: IResolvers = {
  Mutation: {
    ...usersResolver.Mutation,
  },
  Query: {
    ...userResolver.Query,
  },
};

export default resolvers;

Example schema for the package

import { gql } from "mercurius-codegen";

const schema = gql`
  directive @auth on OBJECT | FIELD_DEFINITION

  input Filters {
    AND: [Filters]
    OR: [Filters]
    not: Boolean
    key: String
    operator: String
    value: String
  }

  enum SortDirection {
    ASC
    DESC
  }

  input SortInput {
    key: String
    direction: SortDirection
  }

  type Query {
    user(id: String): User @auth
    users(limit: Int, offset: Int): [User]! @auth
  }

  type User {
    givenName: String
    id: String
    middleNames: String
    surname: String
  }
`;

Configuration

To add custom email and password validations:

const config: ApiConfig = {
  // ...
  user: {
    //...
    email: {
      host_whitelist: ["..."]
    },
    password: {
      minLength: 8,
      minLowercase: 1,
      minUppercase: 0,
      minNumbers: 1,
      minSymbols: 0,
    }
  }
};

To overwrite ThirdPartyEmailPassword recipes from config:

const config: ApiConfig = {
  // ...
  user: {
    //...
    recipes: {
      thirdPartyEmailPassword: {
        override: {
          apis: {
            appleRedirectHandlerPOST,
            authorisationUrlGET,
            emailPasswordEmailExistsGET,
            emailPasswordSignInPOST,
            emailPasswordSignUpPOST,
            generatePasswordResetTokenPOST,
            passwordResetPOST,
            thirdPartySignInUpPOST,
          },
          functions: {
            createResetPasswordToken,
            emailPasswordSignIn,
            emailPasswordSignUp,
            getUserById,
            getUserByThirdPartyInfo,
            getUsersByEmail,
            resetPasswordUsingToken,
            thirdPartySignInUp,
            updateEmailOrPassword,
          },
        sendEmail,
        signUpFeature: {
          formFields: [
            {
              id: "password",
              validate: async (password) => {
                // if password invalid return invalid message
              },
            },
            //...
          ],
        },
      },
    },
  },
};

NOTE: Each above overridden elements is a wrapper function. For example to override emailPasswordSignUpPOST see emailPasswordSignUpPOST.

Context

0.65.2

6 days ago

0.65.1

9 days ago

0.65.0

14 days ago

0.64.2

1 month ago

0.64.1

1 month ago

0.64.0

2 months ago

0.63.0

2 months ago

0.62.4

2 months ago

0.62.3

2 months ago

0.62.2

2 months ago

0.62.1

3 months ago

0.62.0

3 months ago

0.61.1

3 months ago

0.61.0

3 months ago

0.60.0

3 months ago

0.59.0

4 months ago

0.58.0

4 months ago

0.57.1

4 months ago

0.57.0

4 months ago

0.56.0

5 months ago

0.55.2

5 months ago

0.55.1

5 months ago

0.55.0

5 months ago

0.54.0

5 months ago

0.53.4

5 months ago

0.53.3

5 months ago

0.43.0

8 months ago

0.41.0

9 months ago

0.38.0

9 months ago

0.36.2

10 months ago

0.36.1

10 months ago

0.36.0

10 months ago

0.53.2

5 months ago

0.34.0

10 months ago

0.53.0

6 months ago

0.53.1

6 months ago

0.51.0

6 months ago

0.51.1

6 months ago

0.48.0

8 months ago

0.48.1

8 months ago

0.46.0

8 months ago

0.44.0

8 months ago

0.42.0

8 months ago

0.40.2

9 months ago

0.40.0

9 months ago

0.40.1

9 months ago

0.39.1

9 months ago

0.39.0

9 months ago

0.37.1

9 months ago

0.37.0

9 months ago

0.35.0

10 months ago

0.52.1

6 months ago

0.50.1

6 months ago

0.52.0

6 months ago

0.50.0

7 months ago

0.49.0

7 months ago

0.47.0

8 months ago

0.45.0

8 months ago

0.33.0

11 months ago

0.32.10

11 months ago

0.32.9

11 months ago

0.32.8

11 months ago

0.32.7

11 months ago

0.32.6

11 months ago

0.32.5

12 months ago

0.32.4

12 months ago

0.32.3

12 months ago

0.32.2

12 months ago

0.32.1

12 months ago

0.32.0

1 year ago

0.31.3

1 year ago

0.31.2

1 year ago

0.31.1

1 year ago

0.31.0

1 year ago

0.30.0

1 year ago

0.29.0

1 year ago

0.28.0

1 year ago

0.27.1

1 year ago

0.27.0

1 year ago

0.26.3

1 year ago

0.26.2

1 year ago

0.26.1

1 year ago

0.26.0

1 year ago

0.25.3

1 year ago

0.25.2

1 year ago

0.25.1

1 year ago

0.25.0

1 year ago

0.24.0

1 year ago

0.23.0

1 year ago

0.22.1

1 year ago

0.22.0

1 year ago

0.21.0

1 year ago

0.20.0

1 year ago

0.19.0

1 year ago

0.18.3

1 year ago

0.18.2

1 year ago

0.18.1

1 year ago

0.18.0

1 year ago