Minimum setup (2 mins)
1) Initialise SuperTokens
- Initialize SuperTokens by calling the
Config
function
import "github.com/supertokens/supertokens-go/supertokens"
func main() {
// ; separated addresses
supertokens.Config(supertokens.ConfigMap{
Hosts: "http://localhost:9000;https://try.supertokens.com",
APIKey: "key",
})
}
import "github.com/supertokens/supertokens-go/gin/supertokens"
func main() {
// ; separated addresses
supertokens.Config(supertokens.ConfigMap{
Hosts: "http://localhost:9000;https://try.supertokens.com",
APIKey: "key",
})
}
- All config values:
Hosts: string
-;
separated string for all the locations of SuperTokens instances.AccessTokenPath: string
- Seeaccess_token_path
in the config.yaml fileRefreshAPIPath: string
- Seerefresh_api_path
in the config.yaml fileCookieDomain: string
- Seecookie_domain
in the config.yaml fileCookieSecure: *bool
- Seecookie_secure
in the config.yaml fileCookieSameSite: string
- Seecookie_same_site
in the config.yaml fileAPIKey
- Specify any one of the API keys that you have set in the config.yaml
- There are more config values in the
config.yaml
file (for on premise) or on the SaaS dashboard. The values you set via theinit
function above will override those.
2) Create a refresh API
- This API will be used to get new access and refresh tokens (done automatically from our frontend SDK).
import "github.com/supertokens/supertokens-go/supertokens"
http.HandleFunc("/session/refresh", supertokens.Middleware(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("success"))
}))
import "github.com/supertokens/supertokens-go/gin/supertokens"
r.POST("/session/refresh", supertokens.Middleware(), func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"msg" : "success",
})
}))
3) Add error handlers
- By default, SuperTokens takes care of handling session errors for you. However, you can define your own logic as well.