Advertisement
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: apiURL,
- WebsiteDomain: clientURL,
- 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 {
- log.Println(provider, code, authCodeResponse, redirectURI, options, userContext)
- log.Println(err)
- return tpmodels.SignInUpPOSTResponse{}, err
- }
- if resp.OK != nil {
- // Create a Bearer string by appending string access token
- authCodeResponse := resp.OK.AuthCodeResponse
- accessToken := authCodeResponse.(map[string]interface{})["access_token"].(string)
- var bearer = "Bearer " + accessToken
- if provider.ID == "discord" {
- url := "https://discord.com/api/users/@me"
- // 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{}
- discordResp, err := client.Do(req)
- if err != nil {
- log.Println("Error on response.\n[ERROR] -", err)
- }
- defer discordResp.Body.Close()
- var data map[string]interface{}
- err = json.NewDecoder(discordResp.Body).Decode(&data)
- if err != nil {
- log.Println("Error while decoding the response bytes:", err)
- }
- var userMap map[string]interface{}
- user, _ := json.Marshal(ResolvePayload("discord", data))
- json.Unmarshal(user, &userMap)
- resp.OK.Session.UpdateAccessTokenPayload(userMap)
- }
- if provider.ID == "reddit" {
- log.Println("test")
- url := "https://www.reddit.com/api/v1/me"
- // 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{}
- meResp, err := client.Do(req)
- if err != nil {
- log.Println("Error on response.\n[ERROR] -", err)
- }
- defer meResp.Body.Close()
- var data map[string]interface{}
- err = json.NewDecoder(meResp.Body).Decode(&data)
- if err != nil {
- log.Println("Error while decoding the response bytes:", err)
- }
- var userMap map[string]interface{}
- user, _ := json.Marshal(ResolvePayload("reddit", data))
- json.Unmarshal(user, &userMap)
- resp.OK.Session.UpdateAccessTokenPayload(userMap)
- }
- }
- return resp, err
- }
- return originalImplementation
- },
- },
- SignInAndUpFeature: tpmodels.TypeInputSignInAndUp{
- Providers: []tpmodels.TypeProvider{
- thirdparty.Discord(tpmodels.DiscordConfig{
- ClientID: discordKey,
- ClientSecret: discordSecret,
- }),
- {
- ID: "reddit",
- Get: func(redirectURI, authCodeFromRequest *string, userContext supertokens.UserContext) tpmodels.TypeProviderGetResponse {
- if redirectURI == nil {
- temp := ""
- redirectURI = &temp
- }
- if authCodeFromRequest == nil {
- temp := ""
- authCodeFromRequest = &temp
- }
- return tpmodels.TypeProviderGetResponse{
- AccessTokenAPI: tpmodels.AccessTokenAPI{
- // this contains info about the token endpoint which exchanges the auth code with the access token and profile info.
- URL: "https://www.reddit.com/api/v1/access_token",
- Params: map[string]string{
- // example post params
- "client_id": redditKey,
- "client_secret": redditSecret,
- "grant_type": "authorization_code",
- "redirect_uri": *redirectURI,
- "code": *authCodeFromRequest,
- //...
- },
- },
- AuthorisationRedirect: tpmodels.AuthorisationRedirect{
- // this contains info about forming the authorisation redirect URL without the state params and without the redirect_uri param
- URL: "https://www.reddit.com/api/v1/authorize",
- Params: map[string]interface{}{
- "client_id": redditKey,
- "scope": "identity",
- "response_type": "code",
- "duration": "permanent",
- },
- },
- GetClientId: func(userContext supertokens.UserContext) string {
- return redditKey
- },
- GetProfileInfo: func(authCodeResponse interface{}, userContext supertokens.UserContext) (tpmodels.UserInfo, error) {
- accessToken := authCodeResponse.(map[string]interface{})["access_token"].(string)
- authHeader := "Bearer " + accessToken
- response, err := GetAuthRequest(authHeader)
- if err != nil {
- return tpmodels.UserInfo{}, err
- }
- userInfo := response.(map[string]interface{})
- log.Println(userInfo)
- _, emailOk := userInfo["email"]
- if !emailOk {
- return tpmodels.UserInfo{
- ID: userInfo["id"].(string),
- Email: nil,
- }, nil
- }
- return tpmodels.UserInfo{
- ID: userInfo["id"].(string),
- Email: &tpmodels.EmailStruct{
- ID: userInfo["email"].(string),
- IsVerified: userInfo["verified"].(bool),
- },
- }, nil
- },
- }
- },
- },
- },
- },
- }),
- session.Init(nil),
- },
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement