Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- err := supertokens.Init(supertokens.TypeInput{
- Supertokens: &supertokens.ConnectionInfo{
- // try.supertokens.com is for demo purposes. Replace this with the address of your core instance (sign up on supertokens.com), or self host a core.
- ConnectionURI: "https://try.supertokens.com",
- // APIKey: "IF YOU HAVE AN API KEY FOR THE CORE, ADD IT HERE",
- },
- AppInfo: supertokens.AppInfo{
- AppName: "BeatBattle.app",
- APIDomain: "http://beatbattle.local",
- WebsiteDomain: "https://localhost:3000",
- APIBasePath: &apiBasePath,
- WebsiteBasePath: &websiteBasePath,
- },
- RecipeList: []supertokens.Recipe{
- thirdparty.Init(&tpmodels.TypeInput{
- Override: &tpmodels.OverrideStruct{
- APIs: func(originalImplementation tpmodels.APIInterface) tpmodels.APIInterface {
- // First we copy the original implementation
- originalSignInUpPOST := *originalImplementation.SignInUpPOST
- (*originalImplementation.SignInUpPOST) = func(provider tpmodels.TypeProvider, code string, authCodeResponse interface{}, redirectURI string, options tpmodels.APIOptions, userContext supertokens.UserContext) (tpmodels.SignInUpPOSTResponse, error) {
- resp, err := originalSignInUpPOST(provider, code, authCodeResponse, redirectURI, options, userContext)
- if err != nil {
- return tpmodels.SignInUpPOSTResponse{}, err
- }
- if resp.OK != nil {
- if provider.ID == "discord" {
- authCodeResponse := resp.OK.AuthCodeResponse
- accessToken := authCodeResponse.(map[string]interface{})["access_token"].(string)
- url := "https://discord.com/api/users/@me"
- // Create a Bearer string by appending string access token
- var bearer = "Bearer " + accessToken
- // Create a new request using http
- req, err := http.NewRequest("GET", url, nil)
- // add authorization header to the req
- req.Header.Add("Authorization", bearer)
- // Send req using http Client
- client := &http.Client{}
- resp, err := client.Do(req)
- if err != nil {
- log.Println("Error on response.\n[ERROR] -", err)
- }
- defer resp.Body.Close()
- body, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- log.Println("Error while reading the response bytes:", err)
- }
- log.Println(string([]byte(body)))
- fmt.Println(resp);
- }
- }
- return resp, err
- }
- return originalImplementation
- },
- },
- SignInAndUpFeature: tpmodels.TypeInputSignInAndUp{
- Providers: []tpmodels.TypeProvider{
- thirdparty.Discord(tpmodels.DiscordConfig{
- ClientID: discordKey,
- ClientSecret: discordSecret,
- }),
- },
- },
- }),
- session.Init(&sessmodels.TypeInput{
- Override: &sessmodels.OverrideStruct{
- Functions: func(originalImplementation sessmodels.RecipeInterface) sessmodels.RecipeInterface {
- // first we create a copy of the original implementation
- originalCreateNewSession := *originalImplementation.CreateNewSession
- // we override the create new session function
- (*originalImplementation.CreateNewSession) = func(res http.ResponseWriter, userID string, accessTokenPayload, sessionData map[string]interface{}, userContext supertokens.UserContext) (sessmodels.SessionContainer, error) {
- if accessTokenPayload == nil {
- accessTokenPayload = map[string]interface{}{}
- }
- // This is where we'd append the discord payload to the access token payload
- return originalCreateNewSession(res, userID, accessTokenPayload, sessionData, userContext)
- }
- return originalImplementation
- },
- },
- }),
- },
- })
Advertisement
Add Comment
Please, Sign In to add comment