From 3aeb43f4e2047e0ac1fd77ed0fdd53cf37bc6461 Mon Sep 17 00:00:00 2001 From: Xiao Yijun Date: Fri, 28 Jun 2024 14:34:54 +0800 Subject: [PATCH 1/2] refactor(console): update golang guide --- .../src/assets/docs/guides/web-go/README.mdx | 114 +++++++++--------- 1 file changed, 55 insertions(+), 59 deletions(-) diff --git a/packages/console/src/assets/docs/guides/web-go/README.mdx b/packages/console/src/assets/docs/guides/web-go/README.mdx index 3524a657356..634f4a1d64b 100644 --- a/packages/console/src/assets/docs/guides/web-go/README.mdx +++ b/packages/console/src/assets/docs/guides/web-go/README.mdx @@ -2,6 +2,8 @@ import UriInputField from '@/mdx-components/UriInputField'; import Steps from '@/mdx-components/Steps'; import Step from '@/mdx-components/Step'; import InlineNotification from '@/ds-components/InlineNotification'; +import RedirectUrisWeb from '../../fragments/_redirect-uris-web.mdx'; +import Checkpoint from '../../fragments/_checkpoint.md'; @@ -12,7 +14,7 @@ import InlineNotification from '@/ds-components/InlineNotification'; The following demonstration is built upon the Gin Web Framework. You may also integrate Logto into other frameworks by taking the same steps. - We assume your app is running on http://localhost:8080 in this guide. + We assume your app is running on http://localhost:3000 in this guide. Execute in the project root directory: @@ -23,8 +25,7 @@ go get github.com/logto-io/go Add the `github.com/logto-io/go/client` package to your application code: -```go -// main.go +```go title="main.go" package main import ( @@ -38,15 +39,13 @@ func main() { router.GET("/", func(c *gin.Context) { c.String(200, "Hello Logto!") }) - router.Run(":8080") + router.Run(":3000") } ``` - + In traditional web applications, the user authentication information will be stored in the user session. @@ -60,8 +59,7 @@ Logto SDK provides a `Storage` interface, you can implement a `Storage` adapter The `Storage` type in the Logto SDK is as follows: -```go -// github.com/logto-io/client/storage.go +```go title="github.com/logto-io/client/storage.go" package client type Storage interface { @@ -74,7 +72,7 @@ We use [github.com/gin-contrib/sessions](https://github.com/gin-contrib/sessions Apply the middleware to the application, so that we can get the user session by the user request context in the route handler: -```go +```go title="main.go" package main import ( @@ -97,14 +95,13 @@ func main() { // ... ctx.String(200, "Hello Logto!") }) - router.Run(":8080") + router.Run(":3000") } ``` Create a `session_storage.go` file, define a `SessionStorage` and implement the Logto SDK's `Storage` interfaces: -```go -// session_storage.go +```go title="session_storage.go" package main import ( @@ -138,15 +135,12 @@ sessionStorage := &SessionStorage{session: session} - + First, create a Logto config: - - {`// main.go -func main() { + +{`func main() { // ... logtoConfig := &client.LogtoConfig{ @@ -161,8 +155,7 @@ func main() { Then, you can create a `LogtoClient` for each user request with the Logto config above: -```go -// main.go +```go title="main.go" func main() { // ... @@ -181,7 +174,7 @@ func main() { authState = "You are logged in to this website! :)" } - homePage := `

Hello Logto

` + + homePage := "

Hello Logto

" + "
" + authState + "
" ctx.Data(http.StatusOK, "text/html; charset=utf-8", []byte(homePage)) @@ -193,23 +186,18 @@ func main() {
- - -Before you start implementing the sign-in flow, you need to add a redirect URI in the Admin Console for your application. + -This allows Logto to redirect the user to the redirect URI after signing in. + -For example, if you add `http://localhost:8080/sign-in-callback` to your Redirect URI, Logto will redirect the user to the `/sign-in-callback` route of your application after signing in. + - + After the redirect URI is configured, we add a `sign-in` route to handle the sign-in request and also add an sign-in link on the home page: - - {`//main.go -func main() { + + {`func main() { // ... // Add a link to perform a sign-in request on the home page @@ -247,25 +235,22 @@ func main() { }`} -Now, when your user visit `http://localhost:8080/sign-in`, the user will be redirected to the Logto sign-in page. +Now, when your user visit `http://localhost:3000/sign-in`, the user will be redirected to the Logto sign-in page. - + When the user signs in successfully on the Logto sign-in page, Logto will redirect the user to the Redirect URI. -Assuming your Redirect URI is `http://localhost:8080/sign-in-callback`, then we will add the `/sign-in-callback` route to handle the callback after signing in. +Assuming your Redirect URI is `http://localhost:3000/callback`, then we will add the `/callback` route to handle the callback after signing in. -```go -// main.go +```go title="main.go" func main() { // ... // Add a route for handling sign-in callback requests - router.GET("/sign-in-callback", func(ctx *gin.Context) { + router.GET("/callback", func(ctx *gin.Context) { session := sessions.Default(ctx) logtoClient := client.NewLogtoClient( logtoConfig, @@ -290,21 +275,14 @@ func main() { - + Similar to the sign-in flow, when the user signs out, Logto will redirect the user to the post sign-out redirect URI. -Assuming that you add `http://localhost:8080` to the Post Sign-out Redirect URI field, Logto will redirect the user to the home page after signing out. - - - Now, let's add the `sign-out` route to handle the sign-out request and also add a sign-out link on the home page: - - {`//main.go -func main() { + + {`func main() { // ... // Add a link to perform a sign-out request on the home page @@ -347,16 +325,34 @@ After the user makes a signing-out request, Logto will clear all user authentica - + + + + + + + -Now, you can test your application: +To display the user's information, you can use the `client.GetIdTokenClaims` method. For example, add a route: -1. Visit `http://localhost:8080`, you will see "You are not logged in to this website." message on the home page. -2. Click the "Sign In" link, you will be redirected to the Logto sign-in page. -3. Sign in with your Logto account, you will be redirected to the home page and see "You are logged in to this website!" message. -4. Click the "Sign Out" link, and your user authentication information will be cleared, and the home page will display "You are not logged in to this website." message again. +```go title="main.go" +func main() { + //... + + router.GET("/user-id-token-claims", func(ctx *gin.Context) { + session := sessions.Default(ctx) + logtoClient := client.NewLogtoClient(logtoConfig, &SessionStorage{session: session}) + + idTokenClaims, err := logtoClient.GetIdTokenClaims() + + if err != nil { + ctx.String(http.StatusOK, err.Error()) + } + + ctx.JSON(http.StatusOK, idTokenClaims) + }) +} +``` From f1c80663e56d06dece9b950b66744ec05d3f301f Mon Sep 17 00:00:00 2001 From: Xiao Yijun Date: Sun, 30 Jun 2024 22:43:48 +0800 Subject: [PATCH 2/2] refactor: use imported uris for go docs --- .../console/src/assets/docs/guides/web-go/README.mdx | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/console/src/assets/docs/guides/web-go/README.mdx b/packages/console/src/assets/docs/guides/web-go/README.mdx index 634f4a1d64b..a99aa17d929 100644 --- a/packages/console/src/assets/docs/guides/web-go/README.mdx +++ b/packages/console/src/assets/docs/guides/web-go/README.mdx @@ -2,7 +2,7 @@ import UriInputField from '@/mdx-components/UriInputField'; import Steps from '@/mdx-components/Steps'; import Step from '@/mdx-components/Step'; import InlineNotification from '@/ds-components/InlineNotification'; -import RedirectUrisWeb from '../../fragments/_redirect-uris-web.mdx'; +import RedirectUrisWeb, {defaultBaseUrl, defaultRedirectUri} from '../../fragments/_redirect-uris-web.mdx'; import Checkpoint from '../../fragments/_checkpoint.md'; @@ -13,8 +13,7 @@ import Checkpoint from '../../fragments/_checkpoint.md'; The following demonstration is built upon the Gin Web Framework. - You may also integrate Logto into other frameworks by taking the same steps. - We assume your app is running on http://localhost:3000 in this guide. + You may also integrate Logto into other frameworks by taking the same steps. Execute in the project root directory: @@ -235,7 +234,7 @@ After the redirect URI is configured, we add a `sign-in` route to handle the sig }`} -Now, when your user visit `http://localhost:3000/sign-in`, the user will be redirected to the Logto sign-in page. +Now, when your user visit {defaultBaseUrl}sign-in, the user will be redirected to the Logto sign-in page. @@ -243,7 +242,7 @@ Now, when your user visit `http://localhost:3000/sign-in`, the user will be redi When the user signs in successfully on the Logto sign-in page, Logto will redirect the user to the Redirect URI. -Assuming your Redirect URI is `http://localhost:3000/callback`, then we will add the `/callback` route to handle the callback after signing in. +Assuming your Redirect URI is {defaultRedirectUri}, then we will add the `/callback` route to handle the callback after signing in. ```go title="main.go" func main() {