Advertisement
Guest User

Untitled

a guest
May 3rd, 2022
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 6.64 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:       apiURL,
  10.             WebsiteDomain:   clientURL,
  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.                                 log.Println(provider, code, authCodeResponse, redirectURI, options, userContext)
  25.                                 log.Println(err)
  26.                                 return tpmodels.SignInUpPOSTResponse{}, err
  27.                             }
  28.  
  29.                             if resp.OK != nil {
  30.                                 // Create a Bearer string by appending string access token
  31.                                 authCodeResponse := resp.OK.AuthCodeResponse
  32.                                 accessToken := authCodeResponse.(map[string]interface{})["access_token"].(string)
  33.                                 var bearer = "Bearer " + accessToken
  34.  
  35.                                 if provider.ID == "discord" {
  36.                                     url := "https://discord.com/api/users/@me"
  37.  
  38.                                     // Create a new request using http
  39.                                     req, err := http.NewRequest("GET", url, nil)
  40.  
  41.                                     // add authorization header to the req
  42.                                     req.Header.Add("Authorization", bearer)
  43.  
  44.                                     // Send req using http Client
  45.                                     client := &http.Client{}
  46.                                     discordResp, err := client.Do(req)
  47.                                     if err != nil {
  48.                                         log.Println("Error on response.\n[ERROR] -", err)
  49.                                     }
  50.                                     defer discordResp.Body.Close()
  51.  
  52.                                     var data map[string]interface{}
  53.                                     err = json.NewDecoder(discordResp.Body).Decode(&data)
  54.                                     if err != nil {
  55.                                         log.Println("Error while decoding the response bytes:", err)
  56.                                     }
  57.  
  58.                                     var userMap map[string]interface{}
  59.                                     user, _ := json.Marshal(ResolvePayload("discord", data))
  60.                                     json.Unmarshal(user, &userMap)
  61.  
  62.                                     resp.OK.Session.UpdateAccessTokenPayload(userMap)
  63.                                 }
  64.                                 if provider.ID == "reddit" {
  65.                                     log.Println("test")
  66.                                     url := "https://www.reddit.com/api/v1/me"
  67.  
  68.                                     // Create a new request using http
  69.                                     req, err := http.NewRequest("GET", url, nil)
  70.  
  71.                                     // add authorization header to the req
  72.                                     req.Header.Add("Authorization", bearer)
  73.  
  74.                                     // Send req using http Client
  75.                                     client := &http.Client{}
  76.                                     meResp, err := client.Do(req)
  77.                                     if err != nil {
  78.                                         log.Println("Error on response.\n[ERROR] -", err)
  79.                                     }
  80.                                     defer meResp.Body.Close()
  81.  
  82.                                     var data map[string]interface{}
  83.                                     err = json.NewDecoder(meResp.Body).Decode(&data)
  84.                                     if err != nil {
  85.                                         log.Println("Error while decoding the response bytes:", err)
  86.                                     }
  87.  
  88.                                     var userMap map[string]interface{}
  89.                                     user, _ := json.Marshal(ResolvePayload("reddit", data))
  90.                                     json.Unmarshal(user, &userMap)
  91.  
  92.                                     resp.OK.Session.UpdateAccessTokenPayload(userMap)
  93.                                 }
  94.                             }
  95.  
  96.                             return resp, err
  97.                         }
  98.  
  99.                         return originalImplementation
  100.                     },
  101.                 },
  102.                 SignInAndUpFeature: tpmodels.TypeInputSignInAndUp{
  103.                     Providers: []tpmodels.TypeProvider{
  104.                         thirdparty.Discord(tpmodels.DiscordConfig{
  105.                             ClientID:     discordKey,
  106.                             ClientSecret: discordSecret,
  107.                         }),
  108.                         {
  109.                             ID: "reddit",
  110.                             Get: func(redirectURI, authCodeFromRequest *string, userContext supertokens.UserContext) tpmodels.TypeProviderGetResponse {
  111.                                 if redirectURI == nil {
  112.                                     temp := ""
  113.                                     redirectURI = &temp
  114.                                 }
  115.  
  116.                                 if authCodeFromRequest == nil {
  117.                                     temp := ""
  118.                                     authCodeFromRequest = &temp
  119.                                 }
  120.  
  121.                                 return tpmodels.TypeProviderGetResponse{
  122.                                     AccessTokenAPI: tpmodels.AccessTokenAPI{
  123.                                         // this contains info about the token endpoint which exchanges the auth code with the access token and profile info.
  124.                                         URL: "https://www.reddit.com/api/v1/access_token",
  125.                                         Params: map[string]string{
  126.                                             // example post params
  127.                                             "client_id":     redditKey,
  128.                                             "client_secret": redditSecret,
  129.                                             "grant_type":    "authorization_code",
  130.                                             "redirect_uri":  *redirectURI,
  131.                                             "code":          *authCodeFromRequest,
  132.                                             //...
  133.                                         },
  134.                                     },
  135.                                     AuthorisationRedirect: tpmodels.AuthorisationRedirect{
  136.                                         // this contains info about forming the authorisation redirect URL without the state params and without the redirect_uri param
  137.                                         URL: "https://www.reddit.com/api/v1/authorize",
  138.                                         Params: map[string]interface{}{
  139.                                             "client_id":     redditKey,
  140.                                             "scope":         "identity",
  141.                                             "response_type": "code",
  142.                                             "duration":      "permanent",
  143.                                         },
  144.                                     },
  145.                                     GetClientId: func(userContext supertokens.UserContext) string {
  146.                                         return redditKey
  147.                                     },
  148.                                     GetProfileInfo: func(authCodeResponse interface{}, userContext supertokens.UserContext) (tpmodels.UserInfo, error) {
  149.                                         accessToken := authCodeResponse.(map[string]interface{})["access_token"].(string)
  150.                                         authHeader := "Bearer " + accessToken
  151.                                         response, err := GetAuthRequest(authHeader)
  152.                                         if err != nil {
  153.                                             return tpmodels.UserInfo{}, err
  154.                                         }
  155.                                         userInfo := response.(map[string]interface{})
  156.                                         log.Println(userInfo)
  157.                                         _, emailOk := userInfo["email"]
  158.                                         if !emailOk {
  159.                                             return tpmodels.UserInfo{
  160.                                                 ID:    userInfo["id"].(string),
  161.                                                 Email: nil,
  162.                                             }, nil
  163.                                         }
  164.                                         return tpmodels.UserInfo{
  165.                                             ID: userInfo["id"].(string),
  166.                                             Email: &tpmodels.EmailStruct{
  167.                                                 ID:         userInfo["email"].(string),
  168.                                                 IsVerified: userInfo["verified"].(bool),
  169.                                             },
  170.                                         }, nil
  171.                                     },
  172.                                 }
  173.                             },
  174.                         },
  175.                     },
  176.                 },
  177.             }),
  178.             session.Init(nil),
  179.         },
  180.     })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement