Skip to content

Commit

Permalink
nextjs: implement SSR functions to retrieve user and access token info
Browse files Browse the repository at this point in the history
  • Loading branch information
frankie567 committed Nov 22, 2023
1 parent 94d515b commit fb9d7db
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
7 changes: 7 additions & 0 deletions nextjs/server/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "@fief/fief/nextjs/server",
"private": true,
"main": "../../build/cjs/nextjs/server.js",
"module": "../../build/esm/nextjs/server.js",
"types": "../../build/esm/nextjs/server.d.ts"
}
3 changes: 3 additions & 0 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const external = [
'crypto',
'jose',
'next',
'next/headers',
'next/server',
'path-to-regexp',
'react',
Expand Down Expand Up @@ -44,6 +45,7 @@ export default [
'src/express/index.ts',
'src/nextjs/index.ts',
'src/nextjs/react.tsx',
'src/nextjs/server.ts',
],
plugins: [
typescript({
Expand Down Expand Up @@ -71,6 +73,7 @@ export default [
'src/express/index.ts',
'src/nextjs/index.ts',
'src/nextjs/react.tsx',
'src/nextjs/server.ts',
],
plugins: [
typescript({
Expand Down
27 changes: 27 additions & 0 deletions src/nextjs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,26 @@ export interface FiefAuthParameters {
*/
userIdHeaderName?: string;

/**
* Name of the request header where user information is made available by middleware.
*
* Defaults to `X-FiefAuth-User-Info`.
*/
userInfoHeaderName?: string;

/**
* Name of the request header where access token is made available by middleware.
*
* Defaults to `X-FiefAuth-Access-Token`.
*/
accessTokenHeaderName?: string;

/**
* Name of the request header where access token information is made available by middleware.
*
* Defaults to `X-FiefAuth-Access-Token-Info`.
*/
accessTokenInfoHeaderName?: string;
}

export interface PathConfig {
Expand Down Expand Up @@ -224,8 +238,12 @@ class FiefAuth {

private userIdHeaderName: string;

private userInfoHeaderName: string;

private accessTokenHeaderName: string;

private accessTokenInfoHeaderName: string;

constructor(parameters: FiefAuthParameters) {
this.client = parameters.client;

Expand Down Expand Up @@ -264,7 +282,9 @@ class FiefAuth {
;

this.userIdHeaderName = parameters.userIdHeaderName ? parameters.userIdHeaderName : 'X-FiefAuth-User-Id';
this.userInfoHeaderName = parameters.userInfoHeaderName ? parameters.userInfoHeaderName : 'X-FiefAuth-User-Info';
this.accessTokenHeaderName = parameters.accessTokenHeaderName ? parameters.accessTokenHeaderName : 'X-FiefAuth-Access-Token';
this.accessTokenInfoHeaderName = parameters.accessTokenInfoHeaderName ? parameters.accessTokenInfoHeaderName : 'X-FiefAuth-Access-Token-Info';
}

/**
Expand Down Expand Up @@ -383,6 +403,13 @@ class FiefAuth {
if (result.accessTokenInfo) {
requestHeaders.set(this.userIdHeaderName, result.accessTokenInfo.id);
requestHeaders.set(this.accessTokenHeaderName, result.accessTokenInfo.access_token);
requestHeaders.set(
this.accessTokenInfoHeaderName,
JSON.stringify(result.accessTokenInfo),
);
}
if (result.user) {
requestHeaders.set(this.userInfoHeaderName, JSON.stringify(result.user));

Check warning on line 412 in src/nextjs/index.ts

View check run for this annotation

Codecov / codecov/patch

src/nextjs/index.ts#L412

Added line #L412 was not covered by tests
}
return NextResponse.next({ request: { headers: requestHeaders } });
} catch (err) {
Expand Down
60 changes: 60 additions & 0 deletions src/nextjs/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { headers } from 'next/headers';

import { FiefAccessTokenInfo, FiefUserInfo } from '../client';

/**
* 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.
*
* @param headerName - Name of the request header. Defaults to `X-FiefAuth-User-Id`.
* @returns The user ID, or null if not available.
*/
export const fiefUserId = (headerName: string = 'X-FiefAuth-User-Id'): string | null => {
const headersList = headers();
return headersList.get(headerName);
};

/**
* Return the user information object set in headers by the Fief middleware,
* or `null` if not authenticated.
*
* This function is suitable for server-side rendering in Next.js.
*
* @param headerName - Name of the request header. Defaults to `X-FiefAuth-User-Info`.
* @returns The user information, or null if not available.
*/
export const fiefUserInfo = (headerName: string = 'X-FiefAuth-User-Info'): FiefUserInfo | null => {
const headersList = headers();
const rawUserInfo = headersList.get(headerName);
return rawUserInfo ? JSON.parse(rawUserInfo) : null;
};

/**
* Return the access token set in headers by the Fief middleware,
* or `null` if not authenticated.
*
* This function is suitable for server-side rendering in Next.js.
*
* @param headerName - Name of the request header. Defaults to `X-FiefAuth-Access-Token`.
* @returns The access token, or null if not available.
*/
export const fiefAccessToken = (headerName: string = 'X-FiefAuth-Access-Token'): string | null => {
const headersList = headers();
return headersList.get(headerName);
};

/**
* 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.
*
* @param headerName - Name of the request header. Defaults to `X-FiefAuth-Access-Token-Info`.
* @returns The access token information, or null if not available.
*/
export const fiefAccessTokenInfo = (headerName: string = 'X-FiefAuth-Access-Token-Info'): FiefAccessTokenInfo | null => {
const headersList = headers();
const rawAccessTokenInfo = headersList.get(headerName);
return rawAccessTokenInfo ? JSON.parse(rawAccessTokenInfo) : null;
};

0 comments on commit fb9d7db

Please sign in to comment.