Guest User

Untitled

a guest
Apr 17th, 2022
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 4.36 KB | None | 0 0
  1. err := supertokens.Init(supertokens.TypeInput{
  2.         Supertokens: &supertokens.ConnectionInfo{
  3.             // 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.
  4.             ConnectionURI: "https://try.supertokens.com",
  5.             // APIKey: "IF YOU HAVE AN API KEY FOR THE CORE, ADD IT HERE",
  6.         },
  7.         AppInfo: supertokens.AppInfo{
  8.             AppName: "BeatBattle.app",
  9.             APIDomain: "http://beatbattle.local",
  10.             WebsiteDomain: "https://localhost:3000",
  11.             APIBasePath: &apiBasePath,
  12.             WebsiteBasePath: &websiteBasePath,
  13.         },
  14.         RecipeList: []supertokens.Recipe{
  15.             thirdparty.Init(&tpmodels.TypeInput{
  16.                 Override: &tpmodels.OverrideStruct{
  17.                     APIs: func(originalImplementation tpmodels.APIInterface) tpmodels.APIInterface {
  18.                         // First we copy the original implementation
  19.                         originalSignInUpPOST := *originalImplementation.SignInUpPOST
  20.  
  21.                         (*originalImplementation.SignInUpPOST) = func(provider tpmodels.TypeProvider, code string, authCodeResponse interface{}, redirectURI string, options tpmodels.APIOptions, userContext supertokens.UserContext) (tpmodels.SignInUpPOSTResponse, error) {
  22.                             resp, err := originalSignInUpPOST(provider, code, authCodeResponse, redirectURI, options, userContext)
  23.                             if err != nil {
  24.                                 return tpmodels.SignInUpPOSTResponse{}, err
  25.                             }
  26.  
  27.                             if resp.OK != nil {
  28.                                 if provider.ID == "discord" {
  29.                                     authCodeResponse := resp.OK.AuthCodeResponse
  30.                                     accessToken := authCodeResponse.(map[string]interface{})["access_token"].(string)
  31.                                     url := "https://discord.com/api/users/@me"
  32.  
  33.                                     // Create a Bearer string by appending string access token
  34.                                     var bearer = "Bearer " + accessToken
  35.                                
  36.                                     // Create a new request using http
  37.                                     req, err := http.NewRequest("GET", url, nil)
  38.                                
  39.                                     // add authorization header to the req
  40.                                     req.Header.Add("Authorization", bearer)
  41.                                
  42.                                     // Send req using http Client
  43.                                     client := &http.Client{}
  44.                                     resp, err := client.Do(req)
  45.                                     if err != nil {
  46.                                         log.Println("Error on response.\n[ERROR] -", err)
  47.                                     }
  48.                                     defer resp.Body.Close()
  49.                                
  50.                                     body, err := ioutil.ReadAll(resp.Body)
  51.                                     if err != nil {
  52.                                         log.Println("Error while reading the response bytes:", err)
  53.                                     }
  54.                                     log.Println(string([]byte(body)))
  55.  
  56.                                     fmt.Println(resp);
  57.                                 }
  58.                             }
  59.  
  60.                             return resp, err
  61.                         }
  62.  
  63.                         return originalImplementation
  64.                     },
  65.                 },
  66.                 SignInAndUpFeature: tpmodels.TypeInputSignInAndUp{
  67.                     Providers: []tpmodels.TypeProvider{
  68.                         thirdparty.Discord(tpmodels.DiscordConfig{
  69.                             ClientID: discordKey,
  70.                             ClientSecret: discordSecret,
  71.                         }),
  72.                     },
  73.                 },
  74.             }),
  75.             session.Init(&sessmodels.TypeInput{
  76.                 Override: &sessmodels.OverrideStruct{
  77.                     Functions: func(originalImplementation sessmodels.RecipeInterface) sessmodels.RecipeInterface {
  78.  
  79.                         // first we create a copy of the original implementation
  80.                         originalCreateNewSession := *originalImplementation.CreateNewSession
  81.  
  82.                         // we override the create new session function
  83.                         (*originalImplementation.CreateNewSession) = func(res http.ResponseWriter, userID string, accessTokenPayload, sessionData map[string]interface{}, userContext supertokens.UserContext) (sessmodels.SessionContainer, error) {
  84.                             if accessTokenPayload == nil {
  85.                                 accessTokenPayload = map[string]interface{}{}
  86.                             }
  87.  
  88.                             // This is where we'd append the discord payload to the access token payload
  89.  
  90.                             return originalCreateNewSession(res, userID, accessTokenPayload, sessionData, userContext)
  91.                         }
  92.  
  93.                         return originalImplementation
  94.                     },
  95.                 },
  96.             }),
  97.         },
  98.     })
Advertisement
Add Comment
Please, Sign In to add comment