Class UnologinNextJS

API handlers and utility functions for server-side Next.js.

Hierarchy

  • HttpHandlers
    • UnologinNextJS

Constructors

Properties

authErrorHandler: AuthErrorHandler<ExpressOrNextRequest, ExpressOrNextResponse>

Executed when encountering an authentication error.

See

errors.APIError.isAuthError

Param

req

Param

res

Param

error

Returns

void

client: __module
defaultErrorProps: {
    redirect: {
        destination: string;
        permanent: boolean;
    };
} = ...

ServerSideProps sent on auth error.

See

withUnologin

Type declaration

  • redirect: {
        destination: string;
        permanent: boolean;
    }
    • destination: string
    • permanent: boolean
loginSuccessHandler: null | LoginSuccessHandler<ExpressOrNextRequest, ExpressOrNextResponse>
nextApiHandler: NextApiHandler<any> = ...

Single handler for all unologin related API requests.

Create a file called [unologin].ts or [unologin].js in the directory pages/api/unologin/ in your Next.js project.

In this file, set the default export to nextApiHandler.

Example

Using the quick-setup:


import { nextApiHandler } from '@unologin/next/quick';

export default nextApiHandler;

Example

Or using a custom setup:


import unologinNextJs from 'path/to/your/unologin-instance';

export default unologinNextJs.nextApiHandler;

Param

req

Param

res

Returns

Promise

rest: UnologinRestApi = unologin.rest
routeHandlers: {
    [route: string]: NextApiHandler;
} = ...

Handlers for all unologin Next.js API endpoints.

See

nextApiHandler

Type declaration

  • [route: string]: NextApiHandler

Methods

  • Completes cookie options.

    Returns

    default cookie options

    Parameters

    • opts: CookieOptions

      cookie options

    Returns CookieOptions

  • 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: ExpressOrNextResponse

      res

    Returns null | UserToken

  • Returns

    URL

    Parameters

    • req: ExpressOrNextRequest

      req

    • _: ExpressOrNextResponse

      res (not used)

    Returns URL

  • Returns a types.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: ExpressOrNextRequest

      req

    • res: ExpressOrNextResponse

      res

    Returns null | UserHandle

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

    Requires the user to be logged in.

    The resolved types.UserToken is authenticated and can be trusted.

    See

    getUserTokenOptional for optional authentication.

    Throws

    errors.APIError 403 unauthorized if not logged in.

    Throws

    errors.APIError 403 unauthorized if login token invalid.

    Returns

    Promise

    Parameters

    • req: ExpressOrNextRequest

      req

    • res: ExpressOrNextResponse

      res

    Returns Promise<UserToken>

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

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

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

    Requires a cookie parser.

    See

    getUserToken for required authentication.

    Returns

    Promise

    Parameters

    • req: ExpressOrNextRequest

      req

    • res: ExpressOrNextResponse

      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: ExpressOrNextRequest

      req

    • res: ExpressOrNextResponse

      res

    Returns Promise<{
        url: URL;
    }>

  • Next.js API handler for the login callback.

    Returns

    Promise

    Parameters

    • req: NextApiRequest

      req

    • res: NextApiResponse<any>

      res

    Returns Promise<void>

  • Next.js API handler for logging out.

    Returns

    void

    Parameters

    • req: NextApiRequest

      req

    • res: NextApiResponse<any>

      res

    Returns void

  • Decide what to do on auth error.

    See

    Returns

    void

    Parameters

    • handler: AuthErrorHandler<ExpressOrNextRequest, ExpressOrNextResponse>

      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 types.UserToken passed to handler is authenticated an can be trusted.

    Returns

    void

    Parameters

    • handler: LoginSuccessHandler<ExpressOrNextRequest, ExpressOrNextResponse>

      (req, res, userToken) => unknown

    Returns void

  • Resets/deletes login cookies.

    Returns

    void

    Parameters

    • req: ExpressOrNextRequest

      req

    • res: ExpressOrNextResponse

      res

    Returns void

  • See

    getCachedUserToken

    Returns

    void

    Parameters

    • res: ExpressOrNextResponse

      res

    • userToken: null | UserToken

      token or null

    Returns void

  • Internal

    Returns

    void

    Parameters

    • req: NextApiRequest

      req

    • res: NextApiResponse<any>

      res

    • name: string

      name

    • value: string

      value

    • options: object

      options

    Returns void

  • Internal

    Set the login cookies for a response.

    Returns

    void

    Parameters

    • req: ExpressOrNextRequest

      req

    • res: ExpressOrNextResponse

      rest

    • cookie: LoginCookie

      cookie to set

    • Optional options: CookieOptions

      cookie options

    Returns void

Generated using TypeDoc