-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(next): add server actions support (#562)
* feat(next): add server actions support * refactor: apply suggestions from code review --------- Co-authored-by: Gao Sun <gao@silverhand.io>
- Loading branch information
Showing
26 changed files
with
1,514 additions
and
166 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,5 @@ | ||
--- | ||
"@logto/next": minor | ||
--- | ||
|
||
Add Next.js Server Actions support |
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 |
---|---|---|
|
@@ -30,3 +30,4 @@ cache | |
*.pem | ||
.history | ||
.vercel | ||
.next |
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,3 @@ | ||
{ | ||
"extends": "next/core-web-vitals" | ||
} |
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,21 @@ | ||
# Next Sample (server actions) | ||
|
||
This is a sample project for Logto's Next.js SDK (server actions). | ||
|
||
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Flogto-io%2Fjs%2Ftree%2Fmaster%2Fpackages%2Fnext-sample&env=APP_ID,APP_SECRET,ENDPOINT,BASE_URL,COOKIE_SECRET,RESOURCES,SCOPES&envDescription=Configuration%20needed%20to%20init%20Logto%20client&envLink=https%3A%2F%2Fgithub.com%2Flogto-io%2Fjs%2Ftree%2Fmaster%2Fpackages%2Fnext-server-actions-sample%2FREADME.md&project-name=logto-js&repository-name=logto-js) | ||
|
||
## Configuration | ||
|
||
You can configure the sample project by modifying the `libraries/config.js` file, or by setting the following environment variables: | ||
|
||
| key | description | example | | ||
| ------------- | ------------------------------------------------------- | ------------------------------------------------ | | ||
| APP_ID | The app ID of your application | `my-app` | | ||
| APP_SECRET | The app secret of your application | `my-secret` | | ||
| ENDPOINT | The endpoint of your Logto server | `http://localhost:3001` | | ||
| BASE_URL | The base URL of this application | `http://localhost:3000` | | ||
| COOKIE_SECRET | The secret for cookie encryption | `my-cookie-secret` | | ||
| RESOURCES | Optional, the API resource identifier, split with comma | `http://localhost:3003/,http://localhost:3004/]` | | ||
| SCOPES | Optional, the scopes to grant, split with comma | `read:users,write:users` | | ||
|
||
Learn more about resource and scopes in the [Logto RBAC Documentation](https://docs.logto.io/docs/recipes/rbac/protect-resource#configure-client-sdk). |
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,21 @@ | ||
"use client"; | ||
|
||
import { useRouter } from "next/navigation"; | ||
import { handleSignIn } from "../../libraries/logto"; | ||
import { useEffect } from "react"; | ||
|
||
type Props = { | ||
searchParams: Record<string, string>; | ||
}; | ||
|
||
export default function Callback({ searchParams }: Props) { | ||
const router = useRouter(); | ||
|
||
useEffect(() => { | ||
handleSignIn(searchParams).then(() => { | ||
router.push("/"); | ||
}); | ||
}, [router, searchParams]); | ||
|
||
return <div>Signing in...</div>; | ||
} |
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,19 @@ | ||
import type { Metadata } from "next"; | ||
|
||
export const metadata: Metadata = { | ||
title: "Hello Logto", | ||
description: | ||
"Example project for integrating Logto with Next.js Server Actions", | ||
}; | ||
|
||
export default function RootLayout({ | ||
children, | ||
}: { | ||
children: React.ReactNode; | ||
}) { | ||
return ( | ||
<html lang="en"> | ||
<body>{children}</body> | ||
</html> | ||
); | ||
} |
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,34 @@ | ||
import { getLogtoContext } from "../libraries/logto"; | ||
import SignIn from "./sign-in"; | ||
import SignOut from "./sign-out"; | ||
|
||
export default async function Home() { | ||
const { isAuthenticated, claims } = await getLogtoContext(); | ||
return ( | ||
<main> | ||
<h1>Hello Logto.</h1> | ||
<div>{isAuthenticated ? <SignOut /> : <SignIn />}</div> | ||
{claims && ( | ||
<div> | ||
<h2>Claims:</h2> | ||
<table> | ||
<thead> | ||
<tr> | ||
<th>Name</th> | ||
<th>Value</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{Object.entries(claims).map(([key, value]) => ( | ||
<tr key={key}> | ||
<td>{key}</td> | ||
<td>{value}</td> | ||
</tr> | ||
))} | ||
</tbody> | ||
</table> | ||
</div> | ||
)} | ||
</main> | ||
); | ||
} |
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,18 @@ | ||
"use client"; | ||
|
||
import { useRouter } from "next/navigation"; | ||
import { signIn } from "../libraries/logto"; | ||
|
||
const SignIn = () => { | ||
const router = useRouter(); | ||
|
||
const handleClick = async () => { | ||
const redirectUrl = await signIn(); | ||
|
||
router.push(redirectUrl); | ||
}; | ||
|
||
return <button onClick={handleClick}>Sign In</button>; | ||
}; | ||
|
||
export default SignIn; |
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,18 @@ | ||
"use client"; | ||
|
||
import { useRouter } from "next/navigation"; | ||
import { signOut } from "../libraries/logto"; | ||
|
||
const SignOut = () => { | ||
const router = useRouter(); | ||
|
||
const handleClick = async () => { | ||
const redirectUrl = await signOut(); | ||
|
||
router.push(redirectUrl); | ||
}; | ||
|
||
return <button onClick={handleClick}>Sign Out</button>; | ||
}; | ||
|
||
export default SignOut; |
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,74 @@ | ||
"use server"; | ||
|
||
import LogtoClient from "@logto/next/server-actions"; | ||
import { cookies } from "next/headers"; | ||
|
||
const config = { | ||
appId: process.env.APP_ID ?? "<app-id>", | ||
appSecret: process.env.APP_SECRET ?? "<app-secret>", | ||
endpoint: process.env.ENDPOINT ?? "http://localhost:3001", | ||
baseUrl: process.env.BASE_URL ?? "http://localhost:3000", | ||
cookieSecret: | ||
process.env.COOKIE_SECRET ?? "complex_password_at_least_32_characters_long", | ||
cookieSecure: process.env.NODE_ENV === "production", | ||
// Optional fields for RBAC | ||
resources: process.env.RESOURCES?.split(","), | ||
scopes: process.env.SCOPES?.split(","), | ||
}; | ||
|
||
const logtoClient = new LogtoClient(config); | ||
|
||
const cookieName = `logto:${config.appId}`; | ||
|
||
const setCookies = (value?: string) => { | ||
if (value === undefined) { | ||
return; | ||
} | ||
|
||
cookies().set(cookieName, value, { | ||
maxAge: 14 * 3600 * 24, | ||
secure: config.cookieSecure, | ||
}); | ||
}; | ||
|
||
const getCookie = () => { | ||
return cookies().get(cookieName)?.value ?? ""; | ||
}; | ||
|
||
export const signIn = async () => { | ||
const { url, newCookie } = await logtoClient.handleSignIn( | ||
getCookie(), | ||
`${config.baseUrl}/callback` | ||
); | ||
|
||
setCookies(newCookie); | ||
|
||
return url; | ||
}; | ||
|
||
export const handleSignIn = async (searchParams: Record<string, string>) => { | ||
// Convert searchParams object into a query string. | ||
const search = new URLSearchParams(searchParams).toString(); | ||
|
||
const newCookie = await logtoClient.handleSignInCallback( | ||
getCookie(), | ||
`${config.baseUrl}/callback?${search}` | ||
); | ||
|
||
setCookies(newCookie); | ||
}; | ||
|
||
export const signOut = async () => { | ||
const url = await logtoClient.handleSignOut( | ||
getCookie(), | ||
`${config.baseUrl}/callback` | ||
); | ||
|
||
setCookies(''); | ||
|
||
return url; | ||
}; | ||
|
||
export const getLogtoContext = async () => { | ||
return await logtoClient.getLogtoContext(getCookie()); | ||
}; |
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,5 @@ | ||
/// <reference types="next" /> | ||
/// <reference types="next/image-types/global" /> | ||
|
||
// NOTE: This file should not be edited | ||
// see https://nextjs.org/docs/basic-features/typescript for more information. |
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,8 @@ | ||
/** @type {import('next').NextConfig} */ | ||
const nextConfig = { | ||
experimental: { | ||
serverActions: true, | ||
}, | ||
}; | ||
|
||
module.exports = nextConfig; |
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,36 @@ | ||
{ | ||
"name": "next-server-actions-sample", | ||
"version": "2.1.0", | ||
"license": "MIT", | ||
"private": true, | ||
"scripts": { | ||
"dev": "next dev", | ||
"build": "next build", | ||
"start": "next start" | ||
}, | ||
"dependencies": { | ||
"@logto/next": "workspace:^", | ||
"next": "^13.5.3", | ||
"react": "^18.2.0", | ||
"react-dom": "^18.2.0" | ||
}, | ||
"devDependencies": { | ||
"@silverhand/ts-config": "^4.0.0", | ||
"@silverhand/ts-config-react": "^4.0.0", | ||
"@types/node": "latest", | ||
"@types/react": "latest", | ||
"@types/react-dom": "latest", | ||
"eslint": "^8.50.0", | ||
"eslint-config-next": "latest", | ||
"lint-staged": "^14.0.0", | ||
"postcss": "^8.4.6", | ||
"postcss-modules": "^6.0.0", | ||
"prettier": "^3.0.0", | ||
"stylelint": "^15.0.0", | ||
"typescript": "latest" | ||
}, | ||
"stylelint": { | ||
"extends": "@silverhand/eslint-config-react/.stylelintrc" | ||
}, | ||
"prettier": "@silverhand/eslint-config/.prettierrc" | ||
} |
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,22 @@ | ||
{ | ||
"extends": "@silverhand/ts-config-react/tsconfig.base", | ||
"compilerOptions": { | ||
"jsx": "preserve", | ||
"incremental": true, | ||
"plugins": [ | ||
{ | ||
"name": "next" | ||
} | ||
], | ||
"allowJs": true // added by next cli | ||
}, | ||
"include": [ | ||
"next-env.d.ts", | ||
"**/*.ts", | ||
"**/*.tsx", | ||
".next/types/**/*.ts" | ||
], | ||
"exclude": [ | ||
"node_modules" | ||
] | ||
} |
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
Oops, something went wrong.