Class Auth

The main class that handles sessions and authentication. Each application should have one of these.

Example

const auth = new Auth();
// cookie parser is needed and needs to be before you .use this
app.use(cookieParser());
app.use(auth.middleware)

Hierarchy

  • Auth

Constructors

Properties

Methods

Constructors

  • Creates an instance of the Auth class.

    Parameters

    • options: AuthOptions = ...

      The options to use. Defaults to an in memory session store. You should use a real session store in production, because in memory stores will not scale across processes and log everybody out when the server restarts.

    Returns Auth

Properties

auths: { [key: string]: AuthHandler } = {}

Type declaration

options: AuthOptions

Methods

  • Register an authentication method.

    Parameters

    • name: string

      The name of the authentication method.

    • auth: AuthHandler

      The authentication method.

    Returns void

  • Middleware to sign in a user.

    Example

    app.post("/signin", express.urlencoded({ extended: false }), authentication.authenticate("local"));
    

    Returns

    Parameters

    • type: string

      The authenticator to use. Has to be registered using addAuth.

    • manual: boolean = false

      Whether you want to handle the response yourself. Otherwise this function will send JSON back.

    Returns ((req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, res: Response<any, Record<string, any>>, next: NextFunction) => void)

      • (req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, res: Response<any, Record<string, any>>, next: NextFunction): void
      • Parameters

        • req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>
        • res: Response<any, Record<string, any>>
        • next: NextFunction

        Returns void

  • app.use this.

    Example

    // cookie parser is needed and needs to be before you .use this
    app.use(cookieParser());
    app.use(auth.middleware())

    See

    https://www.npmjs.com/package/cookie-parser needs to be used before this is used.

    Returns ((req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, res: Response<any, Record<string, any>>, next: NextFunction) => Promise<void>)

      • (req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, res: Response<any, Record<string, any>>, next: NextFunction): Promise<void>
      • Parameters

        • req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>
        • res: Response<any, Record<string, any>>
        • next: NextFunction

        Returns Promise<void>

Generated using TypeDoc