-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
nextjs: implement SSR functions to retrieve user and access token info
- Loading branch information
1 parent
94d515b
commit fb9d7db
Showing
4 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |