client
Protected
authExecuted when encountering an authentication error.
errors.APIError.isAuthError
req
res
error
void
Readonly
clientProtected
loginSingle 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.
Using the quick-setup:
import { nextApiHandler } from '@unologin/next/quick';
export default nextApiHandler;
Or using a custom setup:
import unologinNextJs from 'path/to/your/unologin-instance';
export default unologinNextJs.nextApiHandler;
req
res
Promise
Readonly
restReadonly
routeProtected
getInternal
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
.
parsed user token cached in res.locals
res
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.
UserHandle | null
req
res
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.
getUserTokenOptional for optional authentication.
errors.APIError 403 unauthorized if not logged in.
errors.APIError 403 unauthorized if login token invalid.
Promise
req
res
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.
getUserToken for required authentication.
Promise
req
res
Internal
Handles the unologin login event. Returns a URL to redirect the user to.
Promise
req
res
Private
loginPrivate
logoutAdd 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.
void
(req, res, userToken) => unknown
Protected
resetProtected
setProtected
setProtected
setInternal
Set the login cookies for a response.
void
req
rest
cookie to set
Optional
options: CookieOptionscookie options
unologin with context UnologinNextJSWithContext
context from GetServerSideProps
Wraps getServerSideProps and attaches unologin
key to context
.
Promise
function to execute with context
error handler
Generated using TypeDoc
API handlers and utility functions for server-side Next.js.