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

naxauth

v1.1.8

Published

Auth manager for nodejs with client

Downloads

172

Readme

Server callbacks


import Auth from 'naxauth/server'

Auth.AuthConfig({
    secret: process.env.APP_SECRET,
    brandName: "Your Brand Name",
    mailConfig: {
        service: 'gmail',
        host: 'smtp.gmail.com',
        auth: {
            user: '',
            pass: ''
        },
        defaultTemplateData: {
            logo: "url"
        }
    },
    async getRequestData(req) {
        return {
            authToken: req.cookies['authorization'],
            userAgent: "",
            body: req.body
        }
    },
    async getUserData(email) {
        return await User.find({email})
    },
    actions: {
        signin: {
            emailNotification: false,
        },
        signup: {
            action: {
                url: "http://localhost:3000",
            },
            async createUser({
                requestData,
                hashPassword
            }) {
                const body = requestData.body
                const created = await User.create({
                    ...body,
                    email: body.email,
                    password: hashPassword,
                    "firstname": body.firstname,
                    "status": "active",
                    "role": "admin"
                })
                return {
                    ...created,
                    name: `${created.firstname} ${created.lastname}`,
                    email: created.email,
                    photo: created.photo
                }
            }
        },
        verify: {
            async updateUser({
                user,
                requestData
            }) {
                await User.update({status: "active"}, {email: user.email})
            },
        },
        forgotPassword: {
            action: {
                url: "http://localhost:3000"
            }
        },
        resetPassword: {
            async updateUser({
                user,
                hashPassword
            }) {
                await User.update({password: hashPassword}, {email: user.email})
            },
        }
    }
})

app.post("/auth", async (req, res)=> {
    const {data, message, status} = await Auth.getAuth(req)
    const {data, message, status} = await Auth.signin(req)
    const {data, message, status} = await Auth.signup(req)
    const {data, message, status} = await Auth.verify(req)
    const {data, message, status} = await Auth.forgotPassword(req)
    const {data, message, status} = await Auth.resetPassword(req)
    const {data, message, status} = await Auth.useAuth(req)

    const type = req.headers['request-type']
    const {data, message, status} = await  Auth.AuthHandler(type, req)

    res.status(status).json({data, message})
})

Server configs reference

import { SignOptions } from "jsonwebtoken";
import { MailOptions } from "nodemailer/lib/json-transport";
import SMTPTransport from "nodemailer/lib/smtp-transport";
import { EmailTemplateData } from "./emailTemplate";

export type TO = { [key: string]: any }
export type UserData<User> = Omit<User, "name" | "email" | "password" | "photo"> & {
    name: string;
    email: string;
    password: string;
    photo?: string;
}
export type RequestData<User> = {
    authToken: string;
    userAgent: string;
    body: Partial<Omit<User, "email" | "password" | "token">> & {
        email?: string;
        password?: string;
        token?: string;
    };
}

type UserAndReqData<User> = { user: User, requestData: RequestData<User> }

import { SignOptions } from "jsonwebtoken";
import { MailOptions } from "nodemailer/lib/json-transport";
import SMTPTransport from "nodemailer/lib/smtp-transport";
import { EmailTemplateData } from "./emailTemplate";

export type TO = { [key: string]: any }
export type UserData<User> = Omit<User, "name" | "email" | "password" | "photo"> & {
    name: string;
    email: string;
    password: string;
    photo?: string;
}
export type RequestData<User> = {
    authToken: string;
    userAgent: string;
    body: Partial<Omit<User, "email" | "password" | "token">> & {
        email?: string;
        password?: string;
        token?: string;
    };
}

type UserAndReqData<User> = { user: User, requestData: RequestData<User> }

export interface NaxAuthConfigProps<User, Req> {
    brandName: string;
    secret?: string;
    jwtConfig?: SignOptions & {
        expiresIn?: number
    };
    mailConfig?: {
        defaultOptions?: MailOptions,
        defaultTemplateData?: EmailTemplateData
    } & SMTPTransport.Options;

    getRequestData: (req: Req) => Promise<RequestData<User>>;
    getUserData: (email: string) => Promise<UserData<User>>;
    getAuthData?: (ctx: UserAndReqData<User>) => Promise<Partial<UserData<User>>>;

    actions?: {
        signin?: {
            emailNotification?: boolean;
            expiresIn?: number;
            messages?: { [type in "wrongEmail" | "wrongPassword" | "success"]: string };
            checkUser?: (ctx: UserAndReqData<User>) => Promise<void>;
            mail?: (ctx: UserAndReqData<User> & { templateData: EmailTemplateData }) => Promise<{
                data?: Partial<EmailTemplateData>,
                options?: MailOptions;
            }>
        },
        signup?: {
            mailType?: "verificaion" | "normal",
            expiresLinkIn?: number;
            messages?: { [type in "exists" | "invalidPassword" | "success"]: string };
            action?: {
                url: string;
                text?: string;
            };
            isVerified?: (ctx: UserAndReqData<User>) => Promise<boolean>;
            createUser: (ctx: { requestData: RequestData<User>, hashPassword: string }) => Promise<UserData<User>>;
            mail?: (ctx: UserAndReqData<User> & { templateData: EmailTemplateData }) => Promise<{
                data?: Partial<EmailTemplateData>,
                options?: MailOptions;
            }>
        },
        update?: {
            emailNotification?: boolean,
            messages?: { [type in "notFound" | "success"]: string };
            updateUser: (ctx: UserAndReqData<User> & { hashPassword: string | null }) => Promise<UserData<User>>;
            mail?: (ctx: UserAndReqData<User> & { templateData: EmailTemplateData }) => Promise<{
                data?: Partial<EmailTemplateData>,
                options?: MailOptions;
            }>
        },
        verify?: {
            emailNotification?: boolean;
            messages?: { [type in "exists" | "invalid" | "success"]: string };
            updateUser: (ctx: { user: User, requestData: RequestData<User> }) => Promise<void>;
            mail?: (ctx: UserAndReqData<User> & { templateData: EmailTemplateData }) => Promise<{
                data?: Partial<EmailTemplateData>,
                options?: MailOptions;
            }>
        },
        forgotPassword?: {
            expiresLinkIn?: number;
            messages?: { [type in "sent" | "invalidUser" | "success"]: string };
            action: {
                url: string;
                text?: string;
            };
            mail?: (ctx: UserAndReqData<User> & { templateData: EmailTemplateData }) => Promise<{
                data?: Partial<EmailTemplateData>,
                options?: MailOptions;
            }>
        },
        resetPassword?: {
            emailNotification?: boolean;
            messages?: { [type in "expired" | "invalid" | "success"]: string };
            updateUser: (ctx: UserAndReqData<User> & { hashPassword: string }) => Promise<void>;
            mail?: (ctx: UserAndReqData<User> & { templateData: EmailTemplateData }) => Promise<{
                data?: Partial<EmailTemplateData>,
                options?: MailOptions;
            }>
        }
    }
}

Client Config

import Auth from 'naxauth/client'

Auth.NaxAuthConfig({
 baseUrl: "http://localhost:3000/api/auth",
  fetch: {
    method: "POST"
  },
  actions: {
    getAuth: {
        path: "/"
    },
    signin: {
        path: "/signin"
    },
    signup: {
    },
    forgotPassword: {
    },
    resetPassword: {
    }
  }
})

Client callbacks

import Auth from 'naxauth/client'

await Auth.signin({
    email: "",
    password: ""
})

const auth = await Auth.getAuth()

await Auth.signout()

await Auth.signup({})

await Auth.forgotPassword(email)
await Auth.resetPassword({token: "", password: ""})

const isLogin = await Auth.getAuthToken()

Client configs reference


export type TO = { [key: string]: any }

export type ActionCallbacks = {
    onRequestStart?: () => string;
    onRequestEnd?: (res: Response) => string;
    onError?: (message: string) => void;
    onSuccess?: (message: string) => void;
}

export type ActionType = ActionCallbacks & {
    path?: string;
    fetch?: RequestInit;
}

export interface AUTH_CONFIG_TYPE {
    baseUrl: string;
    fetch?: RequestInit;
    tokenName?: string;
    tokenPlacement?: "cookie" | "header",

    actions?: {
        getAuth?: ActionType,
        signin?: ActionType,
        signup?: ActionType,
        verify?: ActionType,
        forgotPassword?: ActionType,
        resetPassword?: ActionType
    }
}

Client Event

import Auth from 'naxauth/client'
export type EventNames =
    "getAuth" |
    "signin" |
    "signup" |
    "signout" |
    "verify" |
    "forgotPassword" |
    "resetPassword" |
    "signout" |
    "success" |
    "error" |
    "requestStart" |
    "requestEnd"

Auth.on(eventName, callback)
Auth.off(eventName, callback)
Auth.emit(eventName)

React

import {AuthProvider, useAuth} from 'naxauth/react'


const AuthView = () => {
    const auth = useAuth()

    if(auth.loading) return "loading..."
    return <div>
        {auth.name}
        {auth.email}
        {auth.photo}
    </div>
}

const App = () => {
    return (
        <AuthProvider>
            <AuthView />
        </AuthProvider>
    )
}