Skip to content

Commit facde11

Browse files
feat: replace hardcoded urls
1 parent c9299ab commit facde11

File tree

7 files changed

+24
-14
lines changed

7 files changed

+24
-14
lines changed

backend/src/auth/auth0_api.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919

2020

2121
@router.get("/login", name="auth0_login")
22-
async def login(request: Request):
22+
async def login(request: Request, redirect_to: str = "/collections"):
23+
request.session["redirect_to"] = redirect_to
2324
redirect_uri = request.url_for("auth0_callback")
2425
return await oauth.auth0.authorize_redirect(
2526
request, redirect_uri, prompt="select_account", connection="google-oauth2"
@@ -29,10 +30,8 @@ async def login(request: Request):
2930
@router.get("/callback", name="auth0_callback")
3031
async def auth0_callback(request: Request):
3132
token = await oauth.auth0.authorize_access_token(request)
32-
3333
user = token.get("userinfo") or await oauth.auth0.userinfo(token=token)
3434

35-
# Create or get the user from database
3635
db = next(get_db())
3736
db_user = get_or_create_user_by_email(
3837
session=db,
@@ -45,7 +44,6 @@ async def auth0_callback(request: Request):
4544
},
4645
)
4746

48-
# Store in session
4947
request.session["user"] = {
5048
"email": user["email"],
5149
"name": user.get("name"),
@@ -54,7 +52,11 @@ async def auth0_callback(request: Request):
5452
}
5553
request.session["user_id"] = str(db_user.id)
5654

57-
return RedirectResponse(url="http://localhost:5173/collections")
55+
frontend_url = settings.FRONTEND_URL
56+
redirect_to = request.session.pop("redirect_to", "/collections")
57+
redirect_url = f"{frontend_url}{redirect_to}"
58+
59+
return RedirectResponse(url=redirect_url)
5860

5961

6062
@router.get("/logout", name="auth0_logout")

backend/src/core/config.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class Settings(BaseSettings):
3434
DOMAIN: str = "localhost"
3535
ENVIRONMENT: Literal["local", "staging", "production"] = "local"
3636

37+
FRONTEND_URL: str
38+
3739
BACKEND_CORS_ORIGINS: Annotated[
3840
list[AnyUrl] | str, BeforeValidator(parse_cors)
3941
] = []
@@ -51,6 +53,10 @@ class Settings(BaseSettings):
5153
AUTH0_CLIENT_SECRET: str
5254
AUTH0_DOMAIN: str
5355

56+
ALLOWED_REDIRECT_ORIGINS: Annotated[
57+
list[str] | str, BeforeValidator(parse_cors)
58+
] = []
59+
5460
@computed_field # type: ignore[misc]
5561
@property
5662
def SQLALCHEMY_DATABASE_URI(self) -> PostgresDsn:

backend/tests/flashcards/card/test_api.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ def test_different_user_access(
133133
):
134134
collection_id = test_collection["id"]
135135
card_id = test_card["id"]
136+
client.cookies.clear()
136137

137138
rsp = client.get(
138139
f"{settings.API_V1_STR}/collections/{collection_id}/cards/{card_id}",

backend/tests/flashcards/collection/test_api.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ def test_different_user_access(
170170
normal_user_token_headers: dict[str, str],
171171
superuser_token_headers: dict[str, str],
172172
):
173+
client.cookies.clear()
173174
collection_data = CollectionCreate(name="User Restricted Collection")
174175
rsp = client.post(
175176
f"{settings.API_V1_STR}/collections/",
@@ -271,6 +272,7 @@ def test_different_user_update(
271272
superuser_token_headers: dict[str, str],
272273
test_collection: dict[str, Any],
273274
):
275+
client.cookies.clear()
274276
collection_id = test_collection["id"]
275277
update_data = CollectionUpdate(name="Cross User Collection Update")
276278
rsp = client.put(
@@ -338,13 +340,14 @@ def test_different_user_delete(
338340
test_collection: dict[str, Any],
339341
):
340342
collection_id = test_collection["id"]
343+
client.cookies.clear()
344+
341345
rsp = client.delete(
342346
f"{settings.API_V1_STR}/collections/{collection_id}",
343347
headers=superuser_token_headers,
344348
)
345349

346350
assert rsp.status_code == 404
347-
348351
# Verity the data still exists
349352
verify_rsp = client.get(
350353
f"{settings.API_V1_STR}/collections/{collection_id}",

frontend/src/hooks/useAuthContext.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ function AuthProvider({ children }: { children: React.ReactNode }) {
3131

3232
try {
3333
const user = await UsersService.readUserMe()
34-
if (user) {
35-
setIsLoggedIn(true)
36-
}
34+
setIsLoggedIn(Boolean(user))
35+
3736
} catch (error) {
3837
console.error('Error fetching user:', error)
3938
}
@@ -69,7 +68,7 @@ function AuthProvider({ children }: { children: React.ReactNode }) {
6968

7069
const logout = async () => {
7170
try {
72-
await fetch('http://localhost:8000/api/v1/auth0/logout', {
71+
await fetch(`${import.meta.env.VITE_API_URL}/auth0/logout`, {
7372
method: 'GET',
7473
credentials: 'include',
7574
})

frontend/src/routes/_publicLayout/login.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ function Login() {
5353
}
5454

5555
const handleGoogleLogin = () => {
56-
window.location.href = 'http://localhost:8000/api/v1/auth0/login'
56+
window.location.href = `${import.meta.env.VITE_API_URL}/api/v1/auth0/login?redirect_to=/collections`
57+
5758
}
5859

5960
return (

frontend/src/routes/_publicLayout/signup.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ function SignUp() {
6464
}
6565

6666
const handleGoogleSignup = () => {
67-
// This function will be implemented later when we add Auth0 integration
68-
console.log('Google signup clicked')
69-
window.location.href = 'http://localhost:8000/api/v1/auth0/login'
67+
window.location.href = `${import.meta.env.VITE_API_URL}/api/v1/auth0/login?redirect_to=/collections`
7068
}
7169

7270
return (

0 commit comments

Comments
 (0)