Private accessPrivate accessPrivate apiPrivate apiPrivate clientPrivate fiefPrivate fiefPrivate forbiddenPrivate loginPrivate logoutPrivate logoutPrivate redirectPrivate redirectURIPrivate returnPrivate returnPrivate sessionPrivate userReturn an API middleware to authenticate an API route.
Your API route handler.
Optional parameters to apply when authenticating the request.
An API handler.
import { fiefAuth } from "../../fief"
export default fiefAuth.authenticated(function handler(req, res) {
res.status(200).json(req.user);
});
import { fiefAuth } from "../../fief"
export default fiefAuth.authenticated(function handler(req, res) {
res.status(200).json(req.user);
}, { scope: ['required_scope'] });
import { fiefAuth } from "../../fief"
export default fiefAuth.authenticated(function handler(req, res) {
res.status(200).json(req.user);
}, { acr: FiefACR.LEVEL_ONE });
import { fiefAuth } from "../../fief"
export default fiefAuth.authenticated(function handler(req, res) {
res.status(200).json(req.user);
}, { permissions: ['castles:create'] });
Return an API route to get the FiefUserInfo and FiefAccessTokenInfo of the currently authenticated user.
It's mainly useful to get the user information from the React hooks.
An API route.
import { fiefAuth } from '../../fief';
export default fiefAuth.currentUser();
Return the access token information set in headers by the Fief middleware,
or null if not authenticated.
This function is suitable for server-side rendering in Next.js.
Optional req: IncomingMessageNext.js request object. Required for older versions of Next.js
not supporting the headers() function.
he access token information, or null if not available.
Return the user ID set in headers by the Fief middleware, or null if not authenticated.
This function is suitable for server-side rendering in Next.js.
Optional req: IncomingMessageNext.js request object. Required for older versions of Next.js
not supporting the headers() function.
The user ID, or null if not available.
Fetch the user information object from the Fief API, if access token is available.
This function is suitable for server-side rendering in Next.js.
Optional req: IncomingMessageNext.js request object. Required for older versions of Next.js
not supporting the headers() function.
If true, the user information will be refreshed from the Fief API.
Otherwise, Next.js fetch cache will be used.
The user information, or null if access token is not available.
Return a Next.js middleware to control authentication on the specified paths.
A list of paths matchers with their authentication parameters.
A Next.js middleware function.
import type { NextRequest } from 'next/server'
import { fiefAuth } from './fief'
const authMiddleware = fiefAuth.middleware([
{
matcher: '/private',
parameters: {},
},
{
matcher: '/app/:path*',
parameters: {},
},
{
matcher: '/scope',
parameters: {
scope: ['required_scope'],
},
},
{
matcher: '/acr',
parameters: {
acr: FiefACR.LEVEL_ONE,
},
},
{
matcher: '/permission',
parameters: {
permissions: ['castles:create'],
},
},
]);
export async function middleware(request: NextRequest) {
return authMiddleware(request);
};
Helper class to integrate Fief authentication with Next.js.
Example: Basic