Skip to content

Commit 068dd41

Browse files
Merge pull request #395 from ralf-v093l/master
feat(example-with-gin): provide full config from file
2 parents 5113a4b + 36c2aa5 commit 068dd41

File tree

3 files changed

+107
-124
lines changed

3 files changed

+107
-124
lines changed

examples/with-gin/config/config.go

Lines changed: 11 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -30,137 +30,27 @@ func Init() {
3030
log.Fatal("error on parsing configuration file")
3131
}
3232

33+
var providers []tpmodels.ProviderInput
34+
err = config.UnmarshalKey("Providers", &providers)
35+
if err != nil {
36+
log.Fatal("invalid 'Providers' config, ", err)
37+
}
38+
3339
err = supertokens.Init(supertokens.TypeInput{
3440
Supertokens: &supertokens.ConnectionInfo{
35-
ConnectionURI: "https://try.supertokens.io",
41+
ConnectionURI: config.GetString("SuperTokens.ConnectionURI"),
3642
},
3743
AppInfo: supertokens.AppInfo{
38-
AppName: "SuperTokens Demo App",
39-
APIDomain: "http://localhost" + config.GetString("server.apiPort"),
40-
WebsiteDomain: "http://localhost" + config.GetString("server.websitePort"),
44+
AppName: config.GetString("AppInfo.AppName"),
45+
APIDomain: config.GetString("AppInfo.APIDomain"),
46+
WebsiteDomain: config.GetString("AppInfo.WebsiteDomain"),
4147
},
4248
RecipeList: []supertokens.Recipe{
4349
emailverification.Init(evmodels.TypeInput{
4450
Mode: evmodels.ModeRequired,
4551
}),
4652
thirdpartyemailpassword.Init(&tpepmodels.TypeInput{
47-
/*
48-
We use different credentials for different platforms when required. For example the redirect URI for Github
49-
is different for Web and mobile. In such a case we can provide multiple providers with different client Ids.
50-
51-
When the frontend makes a request and wants to use a specific clientId, it needs to send the clientId to use in the
52-
request. In the absence of a clientId in the request the SDK uses the default provider, indicated by `isDefault: true`.
53-
When adding multiple providers for the same type (Google, Github etc), make sure to set `isDefault: true`.
54-
*/
55-
Providers: []tpmodels.ProviderInput{
56-
// We have provided you with development keys which you can use for testsing.
57-
// IMPORTANT: Please replace them with your own OAuth keys for production use.
58-
{
59-
Config: tpmodels.ProviderConfig{
60-
ThirdPartyId: "google",
61-
Clients: []tpmodels.ProviderClientConfig{
62-
{
63-
ClientType: "web",
64-
ClientID: "1060725074195-kmeum4crr01uirfl2op9kd5acmi9jutn.apps.googleusercontent.com",
65-
ClientSecret: "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",
66-
},
67-
{
68-
// we use this for mobile apps
69-
ClientType: "mobile",
70-
ClientID: "1060725074195-c7mgk8p0h27c4428prfuo3lg7ould5o7.apps.googleusercontent.com",
71-
ClientSecret: "", // this is empty because we follow Authorization code grant flow via PKCE for mobile apps (Google doesn't issue a client secret for mobile apps).
72-
},
73-
},
74-
},
75-
},
76-
{
77-
Config: tpmodels.ProviderConfig{
78-
ThirdPartyId: "github",
79-
Clients: []tpmodels.ProviderClientConfig{
80-
{
81-
ClientType: "web",
82-
ClientID: "467101b197249757c71f",
83-
ClientSecret: "e97051221f4b6426e8fe8d51486396703012f5bd",
84-
},
85-
{
86-
// We use this for mobile apps
87-
ClientType: "mobile",
88-
ClientID: "8a9152860ce869b64c44",
89-
ClientSecret: "00e841f10f288363cd3786b1b1f538f05cfdbda2",
90-
},
91-
},
92-
},
93-
},
94-
/*
95-
For Apple signin, iOS apps always use the bundle identifier as the client ID when communicating with Apple. Android, Web and other platforms
96-
need to configure a Service ID on the Apple developer dashboard and use that as client ID.
97-
In the example below 4398792-io.supertokens.example.service is the client ID for Web. Android etc and thus we mark it as default. For iOS
98-
the frontend for the demo app sends the clientId in the request which is then used by the SDK.
99-
*/
100-
{
101-
Config: tpmodels.ProviderConfig{
102-
ThirdPartyId: "apple",
103-
Clients: []tpmodels.ProviderClientConfig{
104-
{
105-
// For Android and website apps
106-
ClientType: "web",
107-
ClientID: "4398792-io.supertokens.example.service",
108-
AdditionalConfig: map[string]interface{}{
109-
"keyId": "7M48Y4RYDL",
110-
"privateKey": "-----BEGIN PRIVATE KEY-----\nMIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQgu8gXs+XYkqXD6Ala9Sf/iJXzhbwcoG5dMh1OonpdJUmgCgYIKoZIzj0DAQehRANCAASfrvlFbFCYqn3I2zeknYXLwtH30JuOKestDbSfZYxZNMqhF/OzdZFTV0zc5u5s3eN+oCWbnvl0hM+9IW0UlkdA\n-----END PRIVATE KEY-----",
111-
"teamId": "YWQCXGJRJL",
112-
},
113-
},
114-
{
115-
// For iOS Apps
116-
ClientType: "ios",
117-
ClientID: "4398792-io.supertokens.example",
118-
AdditionalConfig: map[string]interface{}{
119-
"keyId": "7M48Y4RYDL",
120-
"privateKey": "-----BEGIN PRIVATE KEY-----\nMIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQgu8gXs+XYkqXD6Ala9Sf/iJXzhbwcoG5dMh1OonpdJUmgCgYIKoZIzj0DAQehRANCAASfrvlFbFCYqn3I2zeknYXLwtH30JuOKestDbSfZYxZNMqhF/OzdZFTV0zc5u5s3eN+oCWbnvl0hM+9IW0UlkdA\n-----END PRIVATE KEY-----",
121-
"teamId": "YWQCXGJRJL",
122-
},
123-
},
124-
},
125-
},
126-
},
127-
{
128-
Config: tpmodels.ProviderConfig{
129-
ThirdPartyId: "discord",
130-
Clients: []tpmodels.ProviderClientConfig{
131-
{
132-
ClientType: "web",
133-
ClientID: "4398792-907871294886928395",
134-
ClientSecret: "His4yXGEovVp5TZkZhEAt0ZXGh8uOVDm",
135-
},
136-
{
137-
// We use this for mobile apps
138-
ClientType: "mobile",
139-
ClientID: "4398792-907871294886928395",
140-
ClientSecret: "His4yXGEovVp5TZkZhEAt0ZXGh8uOVDm",
141-
},
142-
},
143-
},
144-
},
145-
{
146-
Config: tpmodels.ProviderConfig{
147-
ThirdPartyId: "google-workspaces",
148-
Clients: []tpmodels.ProviderClientConfig{
149-
{
150-
ClientType: "web",
151-
ClientID: "1060725074195-kmeum4crr01uirfl2op9kd5acmi9jutn.apps.googleusercontent.com",
152-
ClientSecret: "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",
153-
},
154-
{
155-
// We use this for mobile apps
156-
ClientType: "mobile",
157-
ClientID: "1060725074195-kmeum4crr01uirfl2op9kd5acmi9jutn.apps.googleusercontent.com",
158-
ClientSecret: "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",
159-
},
160-
},
161-
},
162-
},
163-
},
53+
Providers: providers,
16454
}),
16555
session.Init(nil),
16656
dashboard.Init(nil),

examples/with-gin/config/dev.yaml

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
AppInfo:
2+
AppName: SuperTokens Demo App
3+
APIDomain: http://localhost:3001
4+
WebsiteDomain: http://localhost:3000
5+
6+
Server:
7+
Address: localhost:3001
8+
9+
CORS:
10+
AllowOrigins:
11+
- http://localhost:3000
12+
AllowMethods: ["GET", "POST", "DELETE", "PUT", "OPTIONS"]
13+
14+
SuperTokens:
15+
ConnectionURI: https://try.supertokens.io
16+
17+
Providers:
18+
# We use different credentials for different platforms when required. For example the redirect URI for GitHub
19+
# is different for Web and mobile. In such a case we can provide multiple providers with different client Ids.
20+
#
21+
# When the frontend makes a request and wants to use a specific clientId, it needs to send the clientId to use in the
22+
# request. In the absence of a clientId in the request the SDK uses the default provider, indicated by `isDefault: true`.
23+
# When adding multiple providers for the same type (Google, GitHub etc.), make sure to set `isDefault: true`.
24+
25+
# We have provided you with development keys which you can use for testing.
26+
# IMPORTANT: Please replace them with your own OAuth keys for production use.
27+
28+
# Google:
29+
- Config:
30+
ThirdPartyId: google
31+
Clients:
32+
- ClientType: web
33+
ClientID: "1060725074195-kmeum4crr01uirfl2op9kd5acmi9jutn.apps.googleusercontent.com"
34+
ClientSecret: "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"
35+
- ClientType: mobile # We use this for mobile apps
36+
ClientID: "1060725074195-c7mgk8p0h27c4428prfuo3lg7ould5o7.apps.googleusercontent.com"
37+
# ClientSecret is empty because we follow Authorization code grant flow via PKCE for
38+
# mobile apps (Google doesn't issue a client secret for mobile apps):
39+
ClientSecret: ""
40+
41+
# GitHub:
42+
- Config:
43+
ThirdPartyId: github
44+
Clients:
45+
- ClientType: web
46+
ClientID: "467101b197249757c71f"
47+
ClientSecret: "e97051221f4b6426e8fe8d51486396703012f5bd"
48+
- ClientType: mobile # We use this for mobile apps
49+
ClientID: "8a9152860ce869b64c44"
50+
ClientSecret: "00e841f10f288363cd3786b1b1f538f05cfdbda2"
51+
52+
# Apple:
53+
- Config:
54+
ThirdPartyId: apple
55+
# For Apple signin, iOS apps always use the bundle identifier as the client ID when communicating with Apple. Android, Web and other platforms
56+
# need to configure a Service ID on the Apple developer dashboard and use that as client ID.
57+
# In the example below 4398792-io.supertokens.example.service is the client ID for Web. Android etc and thus we mark it as default. For iOS
58+
# the frontend for the demo app sends the clientId in the request which is then used by the SDK.
59+
Clients:
60+
- ClientType: web
61+
ClientID: "4398792-io.supertokens.example.service"
62+
AdditionalConfig:
63+
keyId: "7M48Y4RYDL"
64+
privateKey: "-----BEGIN PRIVATE KEY-----\nMIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQgu8gXs+XYkqXD6Ala9Sf/iJXzhbwcoG5dMh1OonpdJUmgCgYIKoZIzj0DAQehRANCAASfrvlFbFCYqn3I2zeknYXLwtH30JuOKestDbSfZYxZNMqhF/OzdZFTV0zc5u5s3eN+oCWbnvl0hM+9IW0UlkdA\n-----END PRIVATE KEY-----"
65+
teamId: "YWQCXGJRJL"
66+
- ClientType: ios
67+
ClientID: "4398792-io.supertokens.example"
68+
AdditionalConfig:
69+
keyId: "7M48Y4RYDL"
70+
privateKey: "-----BEGIN PRIVATE KEY-----\nMIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQgu8gXs+XYkqXD6Ala9Sf/iJXzhbwcoG5dMh1OonpdJUmgCgYIKoZIzj0DAQehRANCAASfrvlFbFCYqn3I2zeknYXLwtH30JuOKestDbSfZYxZNMqhF/OzdZFTV0zc5u5s3eN+oCWbnvl0hM+9IW0UlkdA\n-----END PRIVATE KEY-----"
71+
teamId: "YWQCXGJRJL"
72+
73+
# Discord
74+
- Config:
75+
ThirdPartyId: discord
76+
Clients:
77+
- ClientType: web
78+
ClientID: "4398792-907871294886928395"
79+
ClientSecret: "His4yXGEovVp5TZkZhEAt0ZXGh8uOVDm"
80+
- ClientType: mobile
81+
ClientID: "4398792-907871294886928395"
82+
ClientSecret: "His4yXGEovVp5TZkZhEAt0ZXGh8uOVDm"
83+
84+
# Google Workspaces:
85+
- Config:
86+
ThirdPartyId: google-workspaces
87+
Clients:
88+
- ClientType: web
89+
ClientID: "1060725074195-kmeum4crr01uirfl2op9kd5acmi9jutn.apps.googleusercontent.com"
90+
ClientSecret: "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"
91+
- ClientType: mobile
92+
ClientID: "1060725074195-kmeum4crr01uirfl2op9kd5acmi9jutn.apps.googleusercontent.com"
93+
ClientSecret: "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"

examples/with-gin/server/server.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ func Init() {
2121

2222
// CORS
2323
router.Use(cors.New(cors.Config{
24-
AllowOrigins: []string{"http://localhost:3000"},
25-
AllowMethods: []string{"GET", "POST", "DELETE", "PUT", "OPTIONS"},
24+
AllowOrigins: config.GetStringSlice("CORS.AllowOrigins"),
25+
AllowMethods: config.GetStringSlice("CORS.AllowMethods"),
2626
AllowHeaders: append([]string{"content-type"}, supertokens.GetAllCORSHeaders()...),
2727
MaxAge: 1 * time.Minute,
2828
AllowCredentials: true,
@@ -41,7 +41,7 @@ func Init() {
4141
router.GET("/sessioninfo", verifySession(nil), sessioninfo)
4242

4343
// starting the server
44-
err := router.Run(config.GetString("server.apiPort"))
44+
err := router.Run(config.GetString("Server.Address"))
4545
if err != nil {
4646
panic(err.Error())
4747
}

0 commit comments

Comments
 (0)