UNPKG

@krypton-org/krypton-auth

Version:

Express authentication middleware, using GraphQL and JSON Web Tokens.

141 lines (140 loc) 5.89 kB
/// <reference types="qs" /> import { NextFunction, Request, Response } from 'express'; /** * Notification type. */ interface Notification { type: string; message: string; } /** * Returns the user data of the logged in user. * @throws {UnauthorizedError} User does not exist * @param {Request} req * @param {Response} res * @returns {Promise<{ user: any }>} Promise to the user data */ export declare const getUser: (req: Request<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs>, res: Response<any>) => Promise<{ user: any; }>; /** * Returns true email address not already taken by another user. * @param {string} email * @returns {Promise<boolean>} Promise returning true if`isAvailable` */ export declare const checkEmailAvailable: (email: string) => Promise<boolean>; /** * Verifying link clicked by users in the account verification email. * @throws {UnauthorizedError} User does not exist * @param {Request} req * @param {Response} res * @param {NextFunction} next * @renders notification page informing users of the operation success. */ export declare const confirmEmail: (req: Request<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs>, res: Response<any>, next: NextFunction) => Promise<void>; /** * Create a new user. * @throws {UsernameAlreadyExistsError} * @throws {EmailAlreadyExistsError} * @throws {UserValidationError} * @param {any} user * @param {Request} req * @returns {Promise<boolean>} Promise returning true on request success */ export declare const createUser: (user: any, req: Request<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs>) => Promise<boolean>; /** * Resend an account verification email to logged in user. * @throws {UnauthorizedError} * @throws {EmailAlreadyConfirmedError} * @param {Request} req * @returns {Promise<boolean>} Promise returning true on request success */ export declare const resendConfirmationEmail: (req: Request<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs>) => Promise<boolean>; /** * Updating user password from the password recovery form. * @throws {UnauthorizedError} * @throws {UpdatePasswordTooLateError} * @param {string} password - new password * @param {string} passwordRecoveryToken - token guaranteeing user identity * @returns {Promise<{ notifications: Notification[] }>} Promise to the notifications of success or failure */ export declare const recoverPassword: (password: string, passwordRecoveryToken: string) => Promise<{ notifications: Notification[]; }>; /** * Update the different user fields of logged in user. * @throws {UnauthorizedError} * @throws {TokenEncryptionError} * @throws {WrongPasswordError} * @throws {UsernameAlreadyExistsError} * @throws {EmailAlreadyExistsError} * @throws {UserValidationError} * @param {any} userUpdates - fields to update * @param {Request} req * @param {Response} res * @returns {Promise<{ user: any; notifications: Notification[] }>} Promise to the new user data and the notifications of success or failure */ export declare const updateUser: (userUpdates: any, req: Request<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs>, res: Response<any>) => Promise<{ expiryDate: Date; token: string; user: any; }>; /** * Delete logged in user. * @throws {UnauthorizedError} * @throws {WrongPasswordError} * @param {string} password * @param {Request} req * @returns {Promise<boolean>} */ export declare const deleteUser: (password: string, req: Request<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs>) => Promise<boolean>; /** * User log-out. * @throws {UnauthorizedError} * @returns {Promise<boolean>} */ export declare const logout: (req: Request<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs>) => Promise<boolean>; /** * User log-in. * @throws {UserNotFoundError} * @throws {TokenEncryptionError} * @param {string} email * @param {string} password * @param {Request} req * @param {Response} res * @returns {Promise<{ token: string; user: any }>} Promise to the user token and user data. */ export declare const login: (email: string, password: string, req: Request<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs>, res: Response<any>) => Promise<{ expiryDate: Date; token: string; user: any; }>; /** * Send password recovery email, when a user has lost his password. * @throws {AlreadyLoggedInError} * @param {string} email * @param {Request} req * @returns {Promise<boolean} Returns true */ export declare const sendPasswordRecoveryEmail: (email: string, req: Request<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs>) => Promise<boolean>; /** * Send an HTML page with the password reset form. * @param {Request} req * @param {Response} res * @param {NextFunction} next * @returns an HTML page with the password reset form */ export declare const resetPasswordForm: (req: Request<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs>, res: Response<any>, next: NextFunction) => void; /** * Refresh the auth token and the refresh token. This last one is set in an httpOnly cookie. * @throws {UnauthorizedError} * @throws {TokenEncryptionError} * @param {Request} req * @param {Response} res * @returns {Promise<{ token: string; expiryDate: Date }>} Promise to the new authentication token and its expiry date. */ export declare const refreshTokens: (req: Request<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs>, res: Response<any>) => Promise<{ token: string; expiryDate: Date; }>; export {};