Implements HttpHandlers for Express.

Hierarchy

Constructors

Properties

authErrorHandler: AuthErrorHandler<Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, Response<any, Record<string, any>>> = ...

Executed when encountering an authentication error.

See

isAuthError

Param

req

Param

res

Param

error

Returns

void

client: main

unologin instance

loginEventHandler: Handler = ...

Express wrapper for handleLoginEvent.

See

handleLoginEvent

Param

req

Param

res

Param

next

Returns

Promise

loginSuccessHandler: null | LoginSuccessHandler<Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, Response<any, Record<string, any>>> = null
requireLogin: Handler = ...

Only executes the next handler if the user is logged in.

Will trigger the AuthErrorHandler otherwise.

Must be preceded by the parseLogin middleware.

See

Param

req

Param

res

Param

next

Returns

Promise

Accessors

  • get cookies(): {
        login: {
            name: string;
            options: CookieOptions;
        };
        loginState: {
            name: string;
            options: CookieOptions;
        };
    }
  • Returns {
        login: {
            name: string;
            options: CookieOptions;
        };
        loginState: {
            name: string;
            options: CookieOptions;
        };
    }

    • login: {
          name: string;
          options: CookieOptions;
      }
      • name: string
      • options: CookieOptions
    • loginState: {
          name: string;
          options: CookieOptions;
      }
      • name: string
      • options: CookieOptions

Methods

  • Internal

    Result of getUserTokenOptional may be stored in with the response object.

    This function acts as a helper to retrieve the cached value.

    This function is meant to be used in conjunction with Express-like frameworks where one middleware function is called after another, passing values using res.locals.

    See

    setCachedUserToken

    Returns

    parsed user token cached in res.locals

    Parameters

    • res: Response<any, Record<string, any>>

      res

    Returns null | UserToken

  • Get login cookie from cookies object.

    Returns

    cookie value

    Parameters

    • cookies: {
          [k: string]: string | undefined;
      }

      cookies

      • [k: string]: string | undefined

    Returns null | string

  • Returns

    URL

    Parameters

    • req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>

      req

    • _: Response<any, Record<string, any>>

      res (not used)

    Returns URL

  • Returns a UserHandle from the current request. Returns null if the request contains no login information.

    IMPORTANT:

    This function is synchronous and the UserHandle can therefore not be trusted to be authenticated.

    The returned UserHandle can however be used in any API call that accepts a UserHandle as a parameter.

    In this case, the authentication happens on the unologin API.

    Use getUserTokenOptional for optional authentication. Use getUserToken for required authentication.

    Returns

    UserHandle | null

    Parameters

    • req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>

      req

    • res: Response<any, Record<string, any>>

      res

    Returns null | UserHandle

  • Authenticates the user and returns a Promise to the UserToken.

    Requires the user to be logged in.

    The resolved UserToken is authenticated and can be trusted.

    See

    getUserTokenOptional for optional authentication.

    Throws

    APIError 403 unauthorized if not logged in.

    Throws

    APIError 403 unauthorized if login token invalid.

    Returns

    Promise

    Parameters

    • req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>

      req

    • res: Response<any, Record<string, any>>

      res

    Returns Promise<UserToken>

  • Authenticates the user and returns a Promise to the UserToken.

    Does not require the user to be logged in. Does nothing if no login cookie is present and returns null.

    The resolved UserToken is authenticated and can be trusted if not null.

    Requires a cookie parser.

    See

    getUserToken for required authentication.

    Returns

    Promise

    Parameters

    • req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>

      req

    • res: Response<any, Record<string, any>>

      res

    Returns Promise<null | UserToken>

  • Internal

    Handles the unologin login event. Returns a URL to redirect the user to.

    Returns

    Promise to redirect the user to

    Parameters

    • req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>

      req

    • res: Response<any, Record<string, any>>

      res

    Returns Promise<{
        url: URL;
    }>

  • Logs out a user and calls next()

    Returns

    Promise

    Parameters

    • req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>

      req

    • res: Response<any, Record<string, any>>

      res

    • Optional next: NextFunction

      next

    Returns Promise<void>

  • Decide what to do on auth error.

    See

    Returns

    void

    Parameters

    • handler: AuthErrorHandler<Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, Response<any, Record<string, any>>>

      Express handler

    Returns void

  • Add a callback that is called after the login event has finished but before the response is sent to the client.

    The UserToken passed to handler is authenticated an can be trusted.

    Returns

    void

    Parameters

    • handler: LoginSuccessHandler<Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, Response<any, Record<string, any>>>

      (req, res, userToken) => unknown

    Returns void

  • Middleware to parse login information.

    Will let any request pass where the user is not logged in.

    See

    for making sure only authenticated requests get past.

    Returns

    Promise<UserToken | null>

    Parameters

    • req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>

      request

    • res: Response<any, Record<string, any>>

      response

    • next: NextFunction

      optional next function

    Returns Promise<void>

  • Resets/deletes login cookies.

    Returns

    void

    Parameters

    • req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>

      req

    • res: Response<any, Record<string, any>>

      res

    Returns void

  • Implements cookie setting for Express.

    Returns

    void

    Parameters

    • _: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>

      req (not used)

    • res: Response<any, Record<string, any>>

      res

    • name: string

      name

    • value: string

      value

    • options: CookieOptions

      options

    Returns void

  • Internal

    Set the login cookies for a response.

    Returns

    void

    Parameters

    • req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>

      req

    • res: Response<any, Record<string, any>>

      rest

    • cookie: LoginCookie

      cookie to set

    • options: CookieOptions = {}

      cookie options

    Returns void

Generated using TypeDoc