-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfront.go
65 lines (52 loc) · 1.36 KB
/
front.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"fmt"
"net/http"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"github.com/logto-io/go/client"
)
var baseUrl = "http://localhost:8080"
func (s *Server) AdminPage(ctx *gin.Context) {
ctx.Data(http.StatusOK, "json", []byte("heyy"))
}
func (s *Server) SignIn(ctx *gin.Context) {
session := sessions.Default(ctx)
logtoClient := client.NewLogtoClient(
s.LogtoConfig,
&SessionStorage{session: session},
)
signInUri, err := logtoClient.SignIn(fmt.Sprintf("%v/auth-callback", baseUrl))
if err != nil {
ctx.String(http.StatusInternalServerError, err.Error())
return
}
ctx.Redirect(http.StatusTemporaryRedirect, signInUri)
}
func (s *Server) AuthCallback(ctx *gin.Context) {
session := sessions.Default(ctx)
logtoClient := client.NewLogtoClient(
s.LogtoConfig,
&SessionStorage{session: session},
)
err := logtoClient.HandleSignInCallback(ctx.Request)
if err != nil {
ctx.String(http.StatusInternalServerError, err.Error())
return
}
ctx.Redirect(http.StatusTemporaryRedirect, "/admin")
}
type SessionStorage struct {
session sessions.Session
}
func (storage *SessionStorage) GetItem(key string) 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()
}