Skip to content
This repository was archived by the owner on Oct 16, 2024. It is now read-only.

Commit 1deb85d

Browse files
committed
Update code to render email address
1 parent bafa206 commit 1deb85d

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

Diff for: 03-Gin/html/dashboard.html renamed to 03-Gin/html/dashboard.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<title>Gin + Passage</title>
44
</head>
55
<body>
6-
<h1>You're signed in!</h1>
6+
<h1>You're signed in, {{ .email }}!</h1>
77

88
<style>
99
body {

Diff for: 03-Gin/main.go

+15-3
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,24 @@ import (
99

1010
func authRequired() gin.HandlerFunc {
1111
return func(c *gin.Context) {
12-
psg, _ := passage.New("<Passage App ID>", nil)
13-
_, err := psg.AuthenticateRequest(c.Request)
12+
psg, _ := passage.New("<Passage App ID>", &passage.Config{
13+
APIKey: "<Passage API Key>",
14+
})
15+
passageUserID, err := psg.AuthenticateRequest(c.Request)
1416
if err != nil {
1517
// Authentication failed!
1618
c.HTML(http.StatusForbidden, "unauthorized.html", nil)
1719
c.Abort()
1820
}
1921

22+
// Get the authenticated user's email and set it in the context
23+
passageUser, err := psg.GetUser(passageUserID)
24+
if err != nil {
25+
// This should not fail, but abort the request if it does
26+
c.AbortWithStatus(http.StatusInternalServerError)
27+
}
28+
c.Set("userEmail", passageUser.Email)
29+
2030
// Authentication was successful, proceed.
2131
c.Next()
2232
}
@@ -33,7 +43,9 @@ func main() {
3343
authenticated := r.Group("/", authRequired())
3444

3545
authenticated.GET("/dashboard", func(c *gin.Context) {
36-
c.HTML(http.StatusOK, "dashboard.html", nil)
46+
c.HTML(http.StatusOK, "dashboard.tmpl", gin.H{
47+
"email": c.MustGet("userEmail").(string),
48+
})
3749
})
3850

3951
r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")

0 commit comments

Comments
 (0)