Skip to content

Commit 376aac7

Browse files
committed
fix: pless changes for ev
1 parent fe11666 commit 376aac7

File tree

7 files changed

+54
-112
lines changed

7 files changed

+54
-112
lines changed

recipe/passwordless/api/implementation.go

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
"github.com/supertokens/supertokens-golang/ingredients/emaildelivery"
2222
"github.com/supertokens/supertokens-golang/ingredients/smsdelivery"
23+
"github.com/supertokens/supertokens-golang/recipe/emailverification"
2324
"github.com/supertokens/supertokens-golang/recipe/passwordless/plessmodels"
2425
"github.com/supertokens/supertokens-golang/recipe/session"
2526
"github.com/supertokens/supertokens-golang/recipe/session/sessmodels"
@@ -44,6 +45,22 @@ func MakeAPIImplementation() plessmodels.APIInterface {
4445

4546
user := response.OK.User
4647

48+
if user.Email != nil {
49+
evInstance := emailverification.GetRecipeInstance()
50+
if evInstance != nil {
51+
tokenResponse, err := (*evInstance.RecipeImpl.CreateEmailVerificationToken)(user.ID, *user.Email, userContext)
52+
if err != nil {
53+
return plessmodels.ConsumeCodePOSTResponse{}, err
54+
}
55+
if tokenResponse.OK != nil {
56+
_, err := (*evInstance.RecipeImpl.VerifyEmailUsingToken)(tokenResponse.OK.Token, userContext)
57+
if err != nil {
58+
return plessmodels.ConsumeCodePOSTResponse{}, err
59+
}
60+
}
61+
}
62+
}
63+
4764
session, err := session.CreateNewSessionWithContext(options.Res, user.ID, map[string]interface{}{}, map[string]interface{}{}, userContext)
4865
if err != nil {
4966
return plessmodels.ConsumeCodePOSTResponse{}, err
@@ -63,6 +80,10 @@ func MakeAPIImplementation() plessmodels.APIInterface {
6380
}
6481

6582
createCodePOST := func(email *string, phoneNumber *string, options plessmodels.APIOptions, userContext supertokens.UserContext) (plessmodels.CreateCodePOSTResponse, error) {
83+
stInstance, err := supertokens.GetInstanceOrThrowError()
84+
if err != nil {
85+
return plessmodels.CreateCodePOSTResponse{}, err
86+
}
6687

6788
var userInputCodeInput *string
6889
if options.Config.GetCustomUserInputCode != nil {
@@ -83,12 +104,14 @@ func MakeAPIImplementation() plessmodels.APIInterface {
83104
var userInputCode *string
84105
flowType := options.Config.FlowType
85106
if flowType == "MAGIC_LINK" || flowType == "USER_INPUT_CODE_AND_MAGIC_LINK" {
86-
link, err := options.Config.GetLinkDomainAndPath(email, phoneNumber, userContext)
87-
if err != nil {
88-
return plessmodels.CreateCodePOSTResponse{}, err
89-
}
90-
link = link + "?rid=" + options.RecipeID + "&preAuthSessionId=" + response.OK.PreAuthSessionID + "#" + response.OK.LinkCode
91-
107+
link := fmt.Sprintf(
108+
"%s%s/verify?rid=%s&preAuthSessionId=%s#%s",
109+
stInstance.AppInfo.WebsiteDomain.GetAsStringDangerous(),
110+
stInstance.AppInfo.WebsiteBasePath.GetAsStringDangerous(),
111+
options.RecipeID,
112+
response.OK.PreAuthSessionID,
113+
response.OK.LinkCode,
114+
)
92115
magicLink = &link
93116
}
94117

@@ -210,6 +233,10 @@ func MakeAPIImplementation() plessmodels.APIInterface {
210233
}
211234

212235
resendCodePOST := func(deviceID string, preAuthSessionID string, options plessmodels.APIOptions, userContext supertokens.UserContext) (plessmodels.ResendCodePOSTResponse, error) {
236+
stInstance, err := supertokens.GetInstanceOrThrowError()
237+
if err != nil {
238+
return plessmodels.ResendCodePOSTResponse{}, err
239+
}
213240
deviceInfo, err := (*options.RecipeImplementation.ListCodesByDeviceID)(deviceID, userContext)
214241
if err != nil {
215242
return plessmodels.ResendCodePOSTResponse{}, err
@@ -255,11 +282,14 @@ func MakeAPIImplementation() plessmodels.APIInterface {
255282
var userInputCode *string
256283
flowType := options.Config.FlowType
257284
if flowType == "MAGIC_LINK" || flowType == "USER_INPUT_CODE_AND_MAGIC_LINK" {
258-
link, err := options.Config.GetLinkDomainAndPath(deviceInfo.Email, deviceInfo.PhoneNumber, userContext)
259-
if err != nil {
260-
return plessmodels.ResendCodePOSTResponse{}, err
261-
}
262-
link = link + "?rid=" + options.RecipeID + "&preAuthSessionId=" + response.OK.PreAuthSessionID + "#" + response.OK.LinkCode
285+
link := fmt.Sprintf(
286+
"%s%s/verify?rid=%s&preAuthSessionId=%s#%s",
287+
stInstance.AppInfo.WebsiteDomain.GetAsStringDangerous(),
288+
stInstance.AppInfo.WebsiteBasePath.GetAsStringDangerous(),
289+
options.RecipeID,
290+
response.OK.PreAuthSessionID,
291+
response.OK.LinkCode,
292+
)
263293

264294
magicLink = &link
265295
}

recipe/passwordless/config_test.go

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,92 +1337,6 @@ func TestCreateAndSendCustomEmailIfErrorIsThrownTheStatusInTheResponseShouldBeA5
13371337
assert.True(t, isCreateAndSendCustomEmailCalled)
13381338
}
13391339

1340-
func TestPassingGetLinkDomainAndPath(t *testing.T) {
1341-
var magicLinkFromAPI *string
1342-
customPath := "http://customPath.com"
1343-
1344-
configValue := supertokens.TypeInput{
1345-
Supertokens: &supertokens.ConnectionInfo{
1346-
ConnectionURI: "http://localhost:8080",
1347-
},
1348-
AppInfo: supertokens.AppInfo{
1349-
APIDomain: "api.supertokens.io",
1350-
AppName: "SuperTokens",
1351-
WebsiteDomain: "supertokens.io",
1352-
},
1353-
RecipeList: []supertokens.Recipe{
1354-
session.Init(nil),
1355-
Init(plessmodels.TypeInput{
1356-
FlowType: "MAGIC_LINK",
1357-
ContactMethodEmail: plessmodels.ContactMethodEmailConfig{
1358-
Enabled: true,
1359-
CreateAndSendCustomEmail: func(email string, userInputCode, urlWithLinkCode *string, codeLifetime uint64, preAuthSessionId string, userContext supertokens.UserContext) error {
1360-
magicLinkFromAPI = urlWithLinkCode
1361-
return nil
1362-
},
1363-
},
1364-
GetLinkDomainAndPath: func(email, phoneNumber *string, userContext supertokens.UserContext) (string, error) {
1365-
return customPath, nil
1366-
},
1367-
}),
1368-
},
1369-
}
1370-
BeforeEach()
1371-
unittesting.StartUpST("localhost", "8080")
1372-
defer AfterEach()
1373-
err := supertokens.Init(configValue)
1374-
if err != nil {
1375-
t.Error(err.Error())
1376-
}
1377-
q, err := supertokens.GetNewQuerierInstanceOrThrowError("")
1378-
if err != nil {
1379-
t.Error(err.Error())
1380-
}
1381-
apiV, err := q.GetQuerierAPIVersion()
1382-
if err != nil {
1383-
t.Error(err.Error())
1384-
}
1385-
1386-
if unittesting.MaxVersion(apiV, "2.11") == "2.11" {
1387-
return
1388-
}
1389-
mux := http.NewServeMux()
1390-
testServer := httptest.NewServer(supertokens.Middleware(mux))
1391-
defer testServer.Close()
1392-
1393-
email := map[string]interface{}{
1394-
"email": "[email protected]",
1395-
}
1396-
1397-
emailBody, err := json.Marshal(email)
1398-
if err != nil {
1399-
t.Error(err.Error())
1400-
}
1401-
1402-
emailResp, err := http.Post(testServer.URL+"/auth/signinup/code", "application/json", bytes.NewBuffer(emailBody))
1403-
1404-
assert.NoError(t, err)
1405-
assert.Equal(t, http.StatusOK, emailResp.StatusCode)
1406-
1407-
emailDataInBytes, err := io.ReadAll(emailResp.Body)
1408-
if err != nil {
1409-
t.Error(err.Error())
1410-
}
1411-
emailResp.Body.Close()
1412-
1413-
var emailResult map[string]interface{}
1414-
err = json.Unmarshal(emailDataInBytes, &emailResult)
1415-
if err != nil {
1416-
t.Error(err.Error())
1417-
}
1418-
magicLinkFromFunction, err := CreateMagicLinkByEmail("[email protected]")
1419-
if err != nil {
1420-
t.Error(err.Error())
1421-
}
1422-
assert.Contains(t, magicLinkFromFunction, customPath)
1423-
assert.Contains(t, *magicLinkFromAPI, customPath)
1424-
}
1425-
14261340
func TestPassingGetCustomUserInputCodeUsingDifferentCodes(t *testing.T) {
14271341
var customCode string
14281342
var userCodeSent *string

recipe/passwordless/plessmodels/models.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ type TypeInput struct {
3333
ContactMethodEmail ContactMethodEmailConfig
3434
ContactMethodEmailOrPhone ContactMethodEmailOrPhoneConfig
3535
FlowType string
36-
GetLinkDomainAndPath func(email *string, phoneNumber *string, userContext supertokens.UserContext) (string, error)
3736
GetCustomUserInputCode func(userContext supertokens.UserContext) (string, error)
3837
Override *OverrideStruct
3938
EmailDelivery *emaildelivery.TypeInput
@@ -45,7 +44,6 @@ type TypeNormalisedInput struct {
4544
ContactMethodEmail ContactMethodEmailConfig
4645
ContactMethodEmailOrPhone ContactMethodEmailOrPhoneConfig
4746
FlowType string
48-
GetLinkDomainAndPath func(email *string, phoneNumber *string, userContext supertokens.UserContext) (string, error)
4947
GetCustomUserInputCode func(userContext supertokens.UserContext) (string, error)
5048
Override OverrideStruct
5149
GetEmailDeliveryConfig func() emaildelivery.TypeInputWithService

recipe/passwordless/recipe.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package passwordless
1717

1818
import (
1919
"errors"
20+
"fmt"
2021
"net/http"
2122

2223
"github.com/supertokens/supertokens-golang/ingredients/emaildelivery"
@@ -177,6 +178,10 @@ func (r *Recipe) handleError(err error, req *http.Request, res http.ResponseWrit
177178
}
178179

179180
func (r *Recipe) CreateMagicLink(email *string, phoneNumber *string, userContext supertokens.UserContext) (string, error) {
181+
stInstance, err := supertokens.GetInstanceOrThrowError()
182+
if err != nil {
183+
return "", err
184+
}
180185
var userInputCodeInput *string
181186
if r.Config.GetCustomUserInputCode != nil {
182187
c, err := r.Config.GetCustomUserInputCode(userContext)
@@ -190,11 +195,14 @@ func (r *Recipe) CreateMagicLink(email *string, phoneNumber *string, userContext
190195
if err != nil {
191196
return "", err
192197
}
193-
link, err := r.Config.GetLinkDomainAndPath(email, phoneNumber, userContext)
194-
if err != nil {
195-
return "", err
196-
}
197-
link = link + "?rid=" + r.RecipeModule.GetRecipeID() + "&preAuthSessionId=" + response.OK.PreAuthSessionID + "#" + response.OK.LinkCode
198+
link := fmt.Sprintf(
199+
"%s%s/verify?rid=%s&preAuthSessionId=%s#%s",
200+
stInstance.AppInfo.WebsiteDomain.GetAsStringDangerous(),
201+
stInstance.AppInfo.WebsiteBasePath.GetAsStringDangerous(),
202+
r.RecipeModule.GetRecipeID(),
203+
response.OK.PreAuthSessionID,
204+
response.OK.LinkCode,
205+
)
198206

199207
return link, nil
200208
}

recipe/passwordless/utils.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,6 @@ func validateAndNormaliseUserInput(appInfo supertokens.NormalisedAppinfo, config
8686

8787
// FlowType is initialized correctly in makeTypeNormalisedInput
8888

89-
if config.GetLinkDomainAndPath != nil {
90-
typeNormalisedInput.GetLinkDomainAndPath = config.GetLinkDomainAndPath
91-
}
92-
9389
// GetCustomUserInputCode is initialized correctly in makeTypeNormalisedInput
9490

9591
typeNormalisedInput.GetEmailDeliveryConfig = func() emaildelivery.TypeInputWithService {
@@ -172,7 +168,6 @@ func makeTypeNormalisedInput(appInfo supertokens.NormalisedAppinfo, inputConfig
172168
ValidateEmailAddress: defaultValidateEmailAddress,
173169
CreateAndSendCustomEmail: inputConfig.ContactMethodEmail.CreateAndSendCustomEmail,
174170
},
175-
GetLinkDomainAndPath: getDefaultGetLinkDomainAndPath(appInfo),
176171
GetCustomUserInputCode: inputConfig.GetCustomUserInputCode,
177172
Override: plessmodels.OverrideStruct{
178173
Functions: func(originalImplementation plessmodels.RecipeInterface) plessmodels.RecipeInterface {

recipe/thirdpartypasswordless/recipe.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ func MakeRecipe(recipeId string, appInfo supertokens.NormalisedAppinfo, config t
9090
ContactMethodEmail: verifiedConfig.ContactMethodEmail,
9191
ContactMethodEmailOrPhone: verifiedConfig.ContactMethodEmailOrPhone,
9292
FlowType: verifiedConfig.FlowType,
93-
GetLinkDomainAndPath: verifiedConfig.GetLinkDomainAndPath,
9493
GetCustomUserInputCode: verifiedConfig.GetCustomUserInputCode,
9594
Override: &plessmodels.OverrideStruct{
9695
Functions: func(originalImplementation plessmodels.RecipeInterface) plessmodels.RecipeInterface {

test/auth-react-server/main.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,6 @@ func callSTInit(passwordlessConfig *plessmodels.TypeInput) {
402402
ContactMethodEmail: passwordlessConfig.ContactMethodEmail,
403403
ContactMethodEmailOrPhone: passwordlessConfig.ContactMethodEmailOrPhone,
404404
FlowType: passwordlessConfig.FlowType,
405-
GetLinkDomainAndPath: passwordlessConfig.GetLinkDomainAndPath,
406405
GetCustomUserInputCode: passwordlessConfig.GetCustomUserInputCode,
407406
Override: &plessmodels.OverrideStruct{
408407
APIs: func(originalImplementation plessmodels.APIInterface) plessmodels.APIInterface {
@@ -448,7 +447,6 @@ func callSTInit(passwordlessConfig *plessmodels.TypeInput) {
448447
ContactMethodEmail: passwordlessConfig.ContactMethodEmail,
449448
ContactMethodEmailOrPhone: passwordlessConfig.ContactMethodEmailOrPhone,
450449
FlowType: passwordlessConfig.FlowType,
451-
GetLinkDomainAndPath: passwordlessConfig.GetLinkDomainAndPath,
452450
GetCustomUserInputCode: passwordlessConfig.GetCustomUserInputCode,
453451
Providers: []tpmodels.TypeProvider{
454452
thirdparty.Google(tpmodels.GoogleConfig{

0 commit comments

Comments
 (0)