Skip to content

Commit 3dbf723

Browse files
Merge pull request #173 from supertokens/session-claims-example-changes
fix: Session claims example changes
2 parents 89f16c3 + fa0d899 commit 3dbf723

File tree

9 files changed

+61
-8
lines changed

9 files changed

+61
-8
lines changed

examples/with-chi-oso/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"github.com/go-chi/cors"
99
"github.com/supertokens/supertokens-golang/examples/with-chi-oso/database"
1010
"github.com/supertokens/supertokens-golang/examples/with-chi-oso/service"
11+
"github.com/supertokens/supertokens-golang/recipe/emailverification"
12+
"github.com/supertokens/supertokens-golang/recipe/emailverification/evmodels"
1113
"github.com/supertokens/supertokens-golang/recipe/session"
1214
"github.com/supertokens/supertokens-golang/recipe/thirdparty"
1315
"github.com/supertokens/supertokens-golang/recipe/thirdparty/tpmodels"
@@ -32,6 +34,9 @@ func main() {
3234
WebsiteDomain: "http://localhost:3000",
3335
},
3436
RecipeList: []supertokens.Recipe{
37+
emailverification.Init(evmodels.TypeInput{
38+
Mode: evmodels.ModeRequired,
39+
}),
3540
thirdpartyemailpassword.Init(&tpepmodels.TypeInput{
3641
Providers: []tpmodels.TypeProvider{
3742
// We have provided you with development keys which you can use for testsing.

examples/with-chi/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66

77
"github.com/go-chi/chi/v5"
88
"github.com/go-chi/cors"
9+
"github.com/supertokens/supertokens-golang/recipe/emailverification"
10+
"github.com/supertokens/supertokens-golang/recipe/emailverification/evmodels"
911
"github.com/supertokens/supertokens-golang/recipe/session"
1012
"github.com/supertokens/supertokens-golang/recipe/thirdparty"
1113
"github.com/supertokens/supertokens-golang/recipe/thirdparty/tpmodels"
@@ -25,6 +27,9 @@ func main() {
2527
WebsiteDomain: "http://localhost:3000",
2628
},
2729
RecipeList: []supertokens.Recipe{
30+
emailverification.Init(evmodels.TypeInput{
31+
Mode: evmodels.ModeRequired,
32+
}),
2833
thirdpartyemailpassword.Init(&tpepmodels.TypeInput{
2934
/*
3035
We use different credentials for different platforms when required. For example the redirect URI for Github

examples/with-fiber/main.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"github.com/gofiber/adaptor/v2"
88
"github.com/gofiber/fiber/v2"
99
"github.com/gofiber/fiber/v2/middleware/cors"
10+
"github.com/supertokens/supertokens-golang/recipe/emailverification"
11+
"github.com/supertokens/supertokens-golang/recipe/emailverification/evmodels"
1012
"github.com/supertokens/supertokens-golang/recipe/session"
1113
"github.com/supertokens/supertokens-golang/recipe/session/sessmodels"
1214
"github.com/supertokens/supertokens-golang/recipe/thirdparty"
@@ -28,6 +30,9 @@ func main() {
2830
WebsiteDomain: "http://localhost:3000",
2931
},
3032
RecipeList: []supertokens.Recipe{
33+
emailverification.Init(evmodels.TypeInput{
34+
Mode: evmodels.ModeRequired,
35+
}),
3136
thirdpartyemailpassword.Init(&tpepmodels.TypeInput{
3237
/*
3338
We use different credentials for different platforms when required. For example the redirect URI for Github
@@ -113,7 +118,7 @@ func main() {
113118
log.Fatal(app.Listen(":3001"))
114119
}
115120

116-
//wrapper of the original implementation of verify session to match the required function signature
121+
// wrapper of the original implementation of verify session to match the required function signature
117122
func verifySession(options *sessmodels.VerifySessionOptions) fiber.Handler {
118123
return func(c *fiber.Ctx) error {
119124
return adaptor.HTTPHandlerFunc(http.HandlerFunc(session.VerifySession(options, func(rw http.ResponseWriter, r *http.Request) {
@@ -156,7 +161,7 @@ func sessioninfo(c *fiber.Ctx) error {
156161
} else {
157162
counter = int(counter.(float64) + 1)
158163
}
159-
err = sessionContainer.UpdateAccessTokenPayload(map[string]interface{}{
164+
err = sessionContainer.MergeIntoAccessTokenPayload(map[string]interface{}{
160165
"counter": counter.(int),
161166
})
162167
if err != nil {
@@ -170,7 +175,7 @@ func sessioninfo(c *fiber.Ctx) error {
170175
})
171176
}
172177

173-
//utility funtion to help convert an array of string to convert to comma separeted string format
178+
// utility funtion to help convert an array of string to convert to comma separeted string format
174179
func stringArrayToStringConvertor(stringArray []string) string {
175180
var stringToBeReturned string
176181
for _, val := range stringArray {

examples/with-gin/config/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"log"
55

66
"github.com/spf13/viper"
7+
"github.com/supertokens/supertokens-golang/recipe/emailverification"
8+
"github.com/supertokens/supertokens-golang/recipe/emailverification/evmodels"
79
"github.com/supertokens/supertokens-golang/recipe/session"
810
"github.com/supertokens/supertokens-golang/recipe/thirdparty"
911
"github.com/supertokens/supertokens-golang/recipe/thirdparty/tpmodels"
@@ -38,6 +40,9 @@ func Init() {
3840
WebsiteDomain: "http://localhost" + config.GetString("server.websitePort"),
3941
},
4042
RecipeList: []supertokens.Recipe{
43+
emailverification.Init(evmodels.TypeInput{
44+
Mode: evmodels.ModeRequired,
45+
}),
4146
thirdpartyemailpassword.Init(&tpepmodels.TypeInput{
4247
/*
4348
We use different credentials for different platforms when required. For example the redirect URI for Github

examples/with-go-zero/main.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"net/http"
77
"strings"
88

9+
"github.com/supertokens/supertokens-golang/recipe/emailverification"
10+
"github.com/supertokens/supertokens-golang/recipe/emailverification/evmodels"
911
"github.com/supertokens/supertokens-golang/recipe/session"
1012
"github.com/supertokens/supertokens-golang/recipe/thirdparty"
1113
"github.com/supertokens/supertokens-golang/recipe/thirdparty/tpmodels"
@@ -26,6 +28,9 @@ func main() {
2628
WebsiteDomain: "http://localhost:3000",
2729
},
2830
RecipeList: []supertokens.Recipe{
31+
emailverification.Init(evmodels.TypeInput{
32+
Mode: evmodels.ModeRequired,
33+
}),
2934
thirdpartyemailpassword.Init(&tpepmodels.TypeInput{
3035
/*
3136
We use different credentials for different platforms when required. For example the redirect URI for Github
@@ -95,7 +100,7 @@ func main() {
95100
server := rest.MustNewServer(
96101
rest.RestConf{
97102
Host: "0.0.0.0",
98-
Port: 8000,
103+
Port: 3001,
99104
},
100105
)
101106
defer server.Stop()
@@ -105,11 +110,20 @@ func main() {
105110
addSupertokensRoutes("/auth", server) // go-zero doesn't execute middlewares if matching routes are not found
106111
server.AddRoute(rest.Route{
107112
Method: http.MethodGet,
108-
Path: "/sessionInfo",
113+
Path: "/sessioninfo",
109114
Handler: func(rw http.ResponseWriter, r *http.Request) {
110115
session.VerifySession(nil, sessioninfo).ServeHTTP(rw, r)
111116
},
112117
})
118+
119+
// go-zero does not honour middlewares for CORS requests unless we explicitly add a route for it.
120+
server.AddRoute(rest.Route{
121+
Method: http.MethodOptions,
122+
Path: "/sessioninfo",
123+
Handler: func(rw http.ResponseWriter, r *http.Request) {
124+
rw.WriteHeader(http.StatusOK)
125+
},
126+
})
113127
server.Start()
114128
}
115129

@@ -148,12 +162,11 @@ func sessioninfo(w http.ResponseWriter, r *http.Request) {
148162

149163
func corsMiddleware(next http.HandlerFunc) http.HandlerFunc {
150164
return func(response http.ResponseWriter, r *http.Request) {
151-
response.Header().Set("Access-Control-Allow-Origin", "localhost:8000")
165+
response.Header().Set("Access-Control-Allow-Origin", "http://localhost:3000")
152166
response.Header().Set("Access-Control-Allow-Credentials", "true")
153167
if r.Method == "OPTIONS" {
154168
response.Header().Set("Access-Control-Allow-Headers", strings.Join(append([]string{"Content-Type"}, supertokens.GetAllCORSHeaders()...), ","))
155-
response.Header().Set("Access-Control-Allow-Methods", "*")
156-
response.Write([]byte(""))
169+
response.Header().Set("Access-Control-Allow-Methods", "GET,POST,PUT,HEAD,OPTIONS")
157170
} else {
158171
next.ServeHTTP(response, r)
159172
}

examples/with-http/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"net/http"
66
"strings"
77

8+
"github.com/supertokens/supertokens-golang/recipe/emailverification"
9+
"github.com/supertokens/supertokens-golang/recipe/emailverification/evmodels"
810
"github.com/supertokens/supertokens-golang/recipe/session"
911
"github.com/supertokens/supertokens-golang/recipe/thirdparty"
1012
"github.com/supertokens/supertokens-golang/recipe/thirdparty/tpmodels"
@@ -24,6 +26,9 @@ func main() {
2426
WebsiteDomain: "http://localhost:3000",
2527
},
2628
RecipeList: []supertokens.Recipe{
29+
emailverification.Init(evmodels.TypeInput{
30+
Mode: evmodels.ModeRequired,
31+
}),
2732
thirdpartyemailpassword.Init(&tpepmodels.TypeInput{
2833
/*
2934
We use different credentials for different platforms when required. For example the redirect URI for Github

examples/with-labstack-echo/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"strings"
88

99
"github.com/labstack/echo/v4"
10+
"github.com/supertokens/supertokens-golang/recipe/emailverification"
11+
"github.com/supertokens/supertokens-golang/recipe/emailverification/evmodels"
1012
"github.com/supertokens/supertokens-golang/recipe/session"
1113
"github.com/supertokens/supertokens-golang/recipe/session/sessmodels"
1214
"github.com/supertokens/supertokens-golang/recipe/thirdparty"
@@ -27,6 +29,9 @@ func main() {
2729
WebsiteDomain: "http://localhost:3000",
2830
},
2931
RecipeList: []supertokens.Recipe{
32+
emailverification.Init(evmodels.TypeInput{
33+
Mode: evmodels.ModeRequired,
34+
}),
3035
thirdpartyemailpassword.Init(&tpepmodels.TypeInput{
3136
/*
3237
We use different credentials for different platforms when required. For example the redirect URI for Github

examples/with-mux/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66

77
"github.com/gorilla/handlers"
88
"github.com/gorilla/mux"
9+
"github.com/supertokens/supertokens-golang/recipe/emailverification"
10+
"github.com/supertokens/supertokens-golang/recipe/emailverification/evmodels"
911
"github.com/supertokens/supertokens-golang/recipe/session"
1012
"github.com/supertokens/supertokens-golang/recipe/thirdparty"
1113
"github.com/supertokens/supertokens-golang/recipe/thirdparty/tpmodels"
@@ -25,6 +27,9 @@ func main() {
2527
WebsiteDomain: "http://localhost:3000",
2628
},
2729
RecipeList: []supertokens.Recipe{
30+
emailverification.Init(evmodels.TypeInput{
31+
Mode: evmodels.ModeRequired,
32+
}),
2833
thirdpartyemailpassword.Init(&tpepmodels.TypeInput{
2934
/*
3035
We use different credentials for different platforms when required. For example the redirect URI for Github

examples/with-twirp/cmd/server/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import (
2323
"github.com/supertokens/supertokens-golang/examples/with-twirp/internal/haberdasherserver"
2424
"github.com/supertokens/supertokens-golang/examples/with-twirp/internal/hooks"
2525
"github.com/supertokens/supertokens-golang/examples/with-twirp/internal/interceptor"
26+
"github.com/supertokens/supertokens-golang/recipe/emailverification"
27+
"github.com/supertokens/supertokens-golang/recipe/emailverification/evmodels"
2628
"github.com/supertokens/supertokens-golang/recipe/session"
2729
"github.com/supertokens/supertokens-golang/recipe/session/sessmodels"
2830
"github.com/supertokens/supertokens-golang/recipe/thirdparty"
@@ -44,6 +46,9 @@ func main() {
4446
WebsiteDomain: "http://localhost:3000",
4547
},
4648
RecipeList: []supertokens.Recipe{
49+
emailverification.Init(evmodels.TypeInput{
50+
Mode: evmodels.ModeRequired,
51+
}),
4752
thirdpartyemailpassword.Init(&tpepmodels.TypeInput{
4853
/*
4954
We use different credentials for different platforms when required. For example the redirect URI for Github

0 commit comments

Comments
 (0)