Skip to content

Commit e6cd497

Browse files
Merge pull request #265 from supertokens/fix/existing-email-field-error
fix: Respond with field error on existing email id in signup api
2 parents 1fbd96f + 89c41c8 commit e6cd497

File tree

6 files changed

+90
-5
lines changed

6 files changed

+90
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## unreleased
99

10+
## [0.11.10] - 2022-12-12
11+
12+
- Fixes issue of sign up API not sending a `FIELD_ERROR` response in case of duplicate email: https://github.com/supertokens/supertokens-python/issues/264
13+
1014
## [0.11.9] - 2022-12-06
1115

1216
- Fixes issue where if send_email is overridden with a different email, it will reset that email.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070

7171
setup(
7272
name="supertokens_python",
73-
version="0.11.9",
73+
version="0.11.10",
7474
author="SuperTokens",
7575
license="Apache 2.0",
7676
author_email="[email protected]",

supertokens_python/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414
SUPPORTED_CDI_VERSIONS = ["2.9", "2.10", "2.11", "2.12", "2.13", "2.14", "2.15"]
15-
VERSION = "0.11.9"
15+
VERSION = "0.11.10"
1616
TELEMETRY = "/telemetry"
1717
USER_COUNT = "/users/count"
1818
USER_DELETE = "/user/remove"

supertokens_python/recipe/emailpassword/api/signup.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@
1515

1616
from typing import TYPE_CHECKING, Any
1717

18+
from ..exceptions import raise_form_field_exception
19+
from supertokens_python.recipe.emailpassword.interfaces import (
20+
SignUpOkResult,
21+
SignUpPostEmailAlreadyExistsError,
22+
)
23+
from supertokens_python.types import GeneralErrorResponse
24+
from ..types import ErrorFormField
25+
1826
if TYPE_CHECKING:
1927
from supertokens_python.recipe.emailpassword.interfaces import (
2028
APIOptions,
@@ -43,4 +51,17 @@ async def handle_sign_up_api(api_implementation: APIInterface, api_options: APIO
4351
form_fields, api_options, user_context
4452
)
4553

46-
return send_200_response(response.to_json(), api_options.response)
54+
if isinstance(response, SignUpOkResult):
55+
return send_200_response(response.to_json(), api_options.response)
56+
if isinstance(response, GeneralErrorResponse):
57+
return send_200_response(response.to_json(), api_options.response)
58+
if isinstance(response, SignUpPostEmailAlreadyExistsError):
59+
return raise_form_field_exception(
60+
"EMAIL_ALREADY_EXISTS_ERROR",
61+
[
62+
ErrorFormField(
63+
id="email",
64+
error="This email already exists. Please sign in instead.",
65+
)
66+
],
67+
)

supertokens_python/recipe/emailpassword/exceptions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313
# under the License.
1414
from __future__ import annotations
1515

16-
from typing import TYPE_CHECKING, Any, Dict, List
16+
from typing import TYPE_CHECKING, Any, Dict, List, NoReturn
1717

1818
from supertokens_python.exceptions import SuperTokensError
1919

2020
if TYPE_CHECKING:
2121
from .types import ErrorFormField
2222

2323

24-
def raise_form_field_exception(msg: str, form_fields: List[ErrorFormField]):
24+
def raise_form_field_exception(msg: str, form_fields: List[ErrorFormField]) -> NoReturn:
2525
raise FieldError(msg, form_fields)
2626

2727

tests/emailpassword/test_signup.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Copyright (c) 2021, VRAI Labs and/or its affiliates. All rights reserved.
2+
#
3+
# This software is licensed under the Apache License, Version 2.0 (the
4+
# "License") as published by the Apache Software Foundation.
5+
#
6+
# You may not use this file except in compliance with the License. You may
7+
# obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.
14+
from fastapi import FastAPI
15+
import json
16+
from fastapi.testclient import TestClient
17+
from pytest import fixture, mark
18+
from supertokens_python import init
19+
from supertokens_python.framework.fastapi import get_middleware
20+
from supertokens_python.recipe import emailpassword, session
21+
from tests.utils import (
22+
get_st_init_args,
23+
setup_function,
24+
start_st,
25+
teardown_function,
26+
sign_up_request,
27+
)
28+
29+
_ = setup_function # type: ignore
30+
_ = teardown_function # type: ignore
31+
32+
pytestmark = mark.asyncio
33+
34+
35+
@fixture(scope="function")
36+
async def app():
37+
app = FastAPI()
38+
app.add_middleware(get_middleware())
39+
40+
return TestClient(app)
41+
42+
43+
async def test_field_error_on_existing_email_signup(app: TestClient):
44+
init_args = get_st_init_args([emailpassword.init(), session.init()])
45+
init(**init_args)
46+
start_st()
47+
48+
response = json.loads(sign_up_request(app, "[email protected]", "validpass123").text)
49+
assert response["status"] == "OK"
50+
51+
response = json.loads(sign_up_request(app, "[email protected]", "validpass123").text)
52+
assert response == {
53+
"status": "FIELD_ERROR",
54+
"formFields": [
55+
{
56+
"id": "email",
57+
"error": "This email already exists. Please sign in instead.",
58+
}
59+
],
60+
}

0 commit comments

Comments
 (0)