mirror of
https://github.com/logto-io/logto.git
synced 2025-03-31 22:51:25 -05:00
refactor(console): update go sdk integration guide (#5272)
This commit is contained in:
parent
0d4d1b16f5
commit
4976154e6d
2 changed files with 95 additions and 80 deletions
|
@ -5,8 +5,6 @@ import Step from '@/mdx-components/Step';
|
|||
|
||||
<Steps>
|
||||
|
||||
<InlineNotification>We use Kotlin in this example, but the concepts are the same for Java.</InlineNotification>
|
||||
|
||||
<Step
|
||||
title="Installation"
|
||||
subtitle="Install Logto Logto SDK for your project"
|
||||
|
@ -53,6 +51,8 @@ Since the SDK needs internet access, you need to add the following permission to
|
|||
subtitle="1 step"
|
||||
>
|
||||
|
||||
<InlineNotification>We use Kotlin in this example, but the concepts are the same for Java.</InlineNotification>
|
||||
|
||||
Create a `LogtoViewModel.kt` and init `LogtoClient` in this view model:
|
||||
|
||||
<pre>
|
||||
|
|
|
@ -6,15 +6,16 @@ import InlineNotification from '@/ds-components/InlineNotification';
|
|||
<Steps>
|
||||
|
||||
<Step
|
||||
title="Add Logto SDK as a dependency"
|
||||
title="Installation"
|
||||
>
|
||||
|
||||
<InlineNotification>
|
||||
The following demonstration is built upon the <a href="https://gin-gonic.com">Gin Web Framework</a>.
|
||||
You may also integrate Logto into other frameworks by taking the same steps.
|
||||
In the following code snippets, we assume your app is running on <code>http://localhost:8080</code>.
|
||||
We assume your app is running on <code>http://localhost:8080</code> in this guide.
|
||||
</InlineNotification>
|
||||
|
||||
Run in the project root directory:
|
||||
Execute in the project root directory:
|
||||
|
||||
```bash
|
||||
go get github.com/logto-io/go
|
||||
|
@ -27,25 +28,24 @@ Add the `github.com/logto-io/go/client` package to your application code:
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
// Add dependency
|
||||
"github.com/logto-io/go/client"
|
||||
"github.com/gin-gonic/gin"
|
||||
// Add dependency
|
||||
"github.com/logto-io/go/client"
|
||||
)
|
||||
|
||||
func main() {
|
||||
router := gin.Default()
|
||||
router.GET("/", func(c *gin.Context) {
|
||||
c.String(200, "Hello Logto!")
|
||||
})
|
||||
router.Run(":8080")
|
||||
router := gin.Default()
|
||||
router.GET("/", func(c *gin.Context) {
|
||||
c.String(200, "Hello Logto!")
|
||||
})
|
||||
router.Run(":8080")
|
||||
}
|
||||
```
|
||||
|
||||
</Step>
|
||||
|
||||
<Step
|
||||
title="Use sessions to store user authentication information"
|
||||
subtitle="2 steps"
|
||||
title="Create a session storage"
|
||||
>
|
||||
|
||||
In traditional web applications, the user authentication information will be stored in the user session.
|
||||
|
@ -70,41 +70,37 @@ type Storage interface {
|
|||
}
|
||||
```
|
||||
|
||||
We will use [github.com/gin-contrib/sessions](https://github.com/gin-contrib/sessions) as an example to demonstrate this process.
|
||||
We use [github.com/gin-contrib/sessions](https://github.com/gin-contrib/sessions) middleware as an example to demonstrate this process.
|
||||
|
||||
### Apply session middleware
|
||||
|
||||
Apply the [github.com/gin-contrib/sessions](https://github.com/gin-contrib/sessions) middleware to the application, so that we can get the user session by the user request context in the route handler:
|
||||
Apply the middleware to the application, so that we can get the user session by the user request context in the route handler:
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gin-contrib/sessions"
|
||||
"github.com/gin-contrib/sessions/memstore"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/logto-io/go/client"
|
||||
"github.com/gin-contrib/sessions"
|
||||
"github.com/gin-contrib/sessions/memstore"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/logto-io/go/client"
|
||||
)
|
||||
|
||||
func main() {
|
||||
router := gin.Default()
|
||||
router := gin.Default()
|
||||
|
||||
// We use memory-based session in this example
|
||||
store := memstore.NewStore([]byte("your session secret"))
|
||||
router.Use(sessions.Sessions("logto-session", store))
|
||||
// We use memory-based session in this example
|
||||
store := memstore.NewStore([]byte("your session secret"))
|
||||
router.Use(sessions.Sessions("logto-session", store))
|
||||
|
||||
router.GET("/", func(ctx *gin.Context) {
|
||||
// Get user session
|
||||
session := sessions.Default(ctx)
|
||||
// ...
|
||||
ctx.String(200, "Hello Logto!")
|
||||
})
|
||||
router.Run(":8080")
|
||||
router.GET("/", func(ctx *gin.Context) {
|
||||
// Get user session
|
||||
session := sessions.Default(ctx)
|
||||
// ...
|
||||
ctx.String(200, "Hello Logto!")
|
||||
})
|
||||
router.Run(":8080")
|
||||
}
|
||||
```
|
||||
|
||||
### Create session storage for Logto to store user authentication information
|
||||
|
||||
Create a `session_storage.go` file, define a `SessionStorage` and implement the Logto SDK's `Storage` interfaces:
|
||||
|
||||
```go
|
||||
|
@ -112,28 +108,28 @@ Create a `session_storage.go` file, define a `SessionStorage` and implement the
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/gin-contrib/sessions"
|
||||
"github.com/gin-contrib/sessions"
|
||||
)
|
||||
|
||||
type SessionStorage struct {
|
||||
session sessions.Session
|
||||
session sessions.Session
|
||||
}
|
||||
|
||||
func (storage *SessionStorage) GetItem(key string) string {
|
||||
value := storage.session.Get(key)
|
||||
if value == nil {
|
||||
return ""
|
||||
}
|
||||
return value.(string)
|
||||
value := storage.session.Get(key)
|
||||
if value == nil {
|
||||
return ""
|
||||
}
|
||||
return value.(string)
|
||||
}
|
||||
|
||||
func (storage *SessionStorage) SetItem(key, value string) {
|
||||
storage.session.Set(key, value)
|
||||
storage.session.Save()
|
||||
storage.session.Set(key, value)
|
||||
storage.session.Save()
|
||||
}
|
||||
```
|
||||
|
||||
Now, in the route handler, you can create a session storage for Logto as follows:
|
||||
Now, in the route handler, you can create a session storage for Logto:
|
||||
|
||||
```go
|
||||
session := sessions.Default(ctx)
|
||||
|
@ -144,10 +140,9 @@ sessionStorage := &SessionStorage{session: session}
|
|||
|
||||
<Step
|
||||
title="Init LogtoClient"
|
||||
subtitle="2 steps"
|
||||
>
|
||||
|
||||
### Create LogtoConfig
|
||||
First, create a Logto config:
|
||||
|
||||
<pre>
|
||||
<code className="language-go">
|
||||
|
@ -166,7 +161,7 @@ func main() {
|
|||
</code>
|
||||
</pre>
|
||||
|
||||
### Init LogtoClient for each user request
|
||||
Then, you can create a `LogtoClient` for each user request with the Logto config above:
|
||||
|
||||
```go
|
||||
// main.go
|
||||
|
@ -201,31 +196,32 @@ func main() {
|
|||
</Step>
|
||||
|
||||
<Step
|
||||
title="Sign in"
|
||||
subtitle="3 steps"
|
||||
title="Implement sign-in route"
|
||||
>
|
||||
|
||||
### Configure Redirect URI
|
||||
Before you start implementing the sign-in flow, you need to add a redirect URI in the Admin Console for your application.
|
||||
|
||||
Add `http://localhost:8080/sign-in-callback` to the Redirect URI field.
|
||||
This allows Logto to redirect the user to the `/sign-in-callback` route of your application after signing in.
|
||||
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.
|
||||
|
||||
<UriInputField name="redirectUris" />
|
||||
|
||||
### Add a route for handling sign-in requests
|
||||
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:
|
||||
|
||||
```go
|
||||
//main.go
|
||||
<pre>
|
||||
<code className="language-go">
|
||||
{`//main.go
|
||||
func main() {
|
||||
// ...
|
||||
|
||||
// Add a link to perform a sign-in request on the home page
|
||||
router.GET("/", func(ctx *gin.Context) {
|
||||
// ...
|
||||
homePage := `<h1>Hello Logto</h1>` +
|
||||
homePage := \`<h1>Hello Logto</h1>\` +
|
||||
"<div>" + authState + "</div>" +
|
||||
// Add link
|
||||
`<div><a href="/sign-in">Sign In</a></div>`
|
||||
\`<div><a href="/sign-in">Sign In</a></div>\`
|
||||
|
||||
ctx.Data(http.StatusOK, "text/html; charset=utf-8", []byte(homePage))
|
||||
})
|
||||
|
@ -240,7 +236,7 @@ func main() {
|
|||
|
||||
// The sign-in request is handled by Logto.
|
||||
// The user will be redirected to the Redirect URI on signed in.
|
||||
signInUri, err := logtoClient.SignIn("http://localhost:8080/sign-in-callback")
|
||||
signInUri, err := logtoClient.SignIn("${props.redirectUris[0] ?? '<your-redirect-uri>'}")
|
||||
if err != nil {
|
||||
ctx.String(http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
|
@ -251,14 +247,21 @@ func main() {
|
|||
})
|
||||
|
||||
// ...
|
||||
}
|
||||
```
|
||||
}`}
|
||||
</code>
|
||||
</pre>
|
||||
|
||||
### Add a route for handling sign-in callback requests
|
||||
Now, when your user visit `http://localhost:8080/sign-in`, the user will be redirected to the Logto sign-in page.
|
||||
|
||||
</Step>
|
||||
|
||||
<Step
|
||||
title="Implement the callback route"
|
||||
>
|
||||
|
||||
When the user signs in successfully on the Logto sign-in page, Logto will redirect the user to the Redirect URI.
|
||||
|
||||
Since the Redirect URI is `http://localhost:8080/sign-in-callback`, we add the `/sign-in-callback` route to handle the callback after signing in.
|
||||
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.
|
||||
|
||||
```go
|
||||
// main.go
|
||||
|
@ -292,33 +295,31 @@ func main() {
|
|||
</Step>
|
||||
|
||||
<Step
|
||||
title="Sign out"
|
||||
subtitle="2 steps"
|
||||
title="Implement sign-out route"
|
||||
>
|
||||
|
||||
### Configure Post Sign-out Redirect URI
|
||||
Similar to the sign-in flow, when the user signs out, Logto will redirect the user to the post sign-out redirect URI.
|
||||
|
||||
Add `http://localhost:8080` to the Post Sign-out Redirect URI filed:
|
||||
Assuming that you add `http://localhost:8080` to the Post Sign-out Redirect URI filed, Logto will redirect the user to the home page after signing out.
|
||||
|
||||
<UriInputField name="postLogoutUris" />
|
||||
<UriInputField name="postLogoutRedirectUris" />
|
||||
|
||||
This configuration enables the user to return 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:
|
||||
|
||||
### Add a route for handling signing out requests
|
||||
|
||||
```go
|
||||
//main.go
|
||||
<pre>
|
||||
<code className="language-go">
|
||||
{`//main.go
|
||||
func main() {
|
||||
// ...
|
||||
|
||||
// Add a link to perform a sign-out request on the home page
|
||||
router.GET("/", func(ctx *gin.Context) {
|
||||
// ...
|
||||
homePage := `<h1>Hello Logto</h1>` +
|
||||
homePage := \`<h1>Hello Logto</h1>\` +
|
||||
"<div>" + authState + "</div>" +
|
||||
`<div><a href="/sign-in">Sign In</a></div>` +
|
||||
\`<div><a href="/sign-in">Sign In</a></div>\` +
|
||||
// Add link
|
||||
`<div><a href="/sign-out">Sign Out</a></div>`
|
||||
\`<div><a href="/sign-out">Sign Out</a></div>\`
|
||||
|
||||
ctx.Data(http.StatusOK, "text/html; charset=utf-8", []byte(homePage))
|
||||
})
|
||||
|
@ -333,7 +334,7 @@ func main() {
|
|||
|
||||
// The sign-out request is handled by Logto.
|
||||
// The user will be redirected to the Post Sign-out Redirect URI on signed out.
|
||||
signOutUri, signOutErr := logtoClient.SignOut("http://localhost:8080")
|
||||
signOutUri, signOutErr := logtoClient.SignOut("${props.postLogoutRedirectUris[0] ?? '<your-post-sign-out-uri>'}")
|
||||
|
||||
if signOutErr != nil {
|
||||
ctx.String(http.StatusOK, signOutErr.Error())
|
||||
|
@ -344,11 +345,25 @@ func main() {
|
|||
})
|
||||
|
||||
// ...
|
||||
}
|
||||
```
|
||||
}`}
|
||||
</code>
|
||||
</pre>
|
||||
|
||||
After the user makes a signing-out request, Logto will clear all user authentication information in the session.
|
||||
|
||||
</Step>
|
||||
|
||||
<Step
|
||||
title="Checkpoint: Test your application"
|
||||
>
|
||||
|
||||
Now, you can test your application:
|
||||
|
||||
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.
|
||||
|
||||
</Step>
|
||||
|
||||
</Steps>
|
||||
|
|
Loading…
Add table
Reference in a new issue