Skip to content

Commit

Permalink
new user hotfix and sign in cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
voynow committed Oct 25, 2024
1 parent 6c5030a commit 2f1c5a0
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 62 deletions.
61 changes: 33 additions & 28 deletions mobile/mobile/mobileApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,43 @@ import SwiftUI

@main
struct mobileApp: App {
@StateObject private var appState = AppState()
@StateObject private var appState = AppState()

var body: some Scene {
WindowGroup {
ContentView(appState: appState)
.environmentObject(appState)
.onAppear {
if let token = UserDefaults.standard.string(forKey: "jwt_token") {
appState.isLoggedIn = true
appState.jwtToken = token
}
}
var body: some Scene {
WindowGroup {
ContentView(appState: appState)
.environmentObject(appState)
.onAppear {
if let token = UserDefaults.standard.string(forKey: "jwt_token") {
appState.isLoggedIn = true
appState.jwtToken = token
}
}
// .onAppear {
// // Temporary: Always set isLoggedIn to false
// appState.isLoggedIn = false
// appState.jwtToken = nil
// }
}
}

private func checkAndRefreshToken() {
if let token = UserDefaults.standard.string(forKey: "jwt_token") {
APIManager.shared.refreshToken(token: token) { result in
DispatchQueue.main.async {
switch result {
case .success(let newToken):
appState.isLoggedIn = true
appState.jwtToken = newToken
UserDefaults.standard.set(newToken, forKey: "jwt_token")
case .failure(_):
// Token refresh failed, user needs to log in again
appState.isLoggedIn = false
appState.jwtToken = nil
UserDefaults.standard.removeObject(forKey: "jwt_token")
}
}
}
private func checkAndRefreshToken() {
if let token = UserDefaults.standard.string(forKey: "jwt_token") {
APIManager.shared.refreshToken(token: token) { result in
DispatchQueue.main.async {
switch result {
case .success(let newToken):
appState.isLoggedIn = true
appState.jwtToken = newToken
UserDefaults.standard.set(newToken, forKey: "jwt_token")
case .failure(_):
// Token refresh failed, user needs to log in again
appState.isLoggedIn = false
appState.jwtToken = nil
UserDefaults.standard.removeObject(forKey: "jwt_token")
}
}
}
}
}
}
30 changes: 4 additions & 26 deletions src/auth_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,42 +141,20 @@ def authenticate_with_code(code: str) -> UserAuthRow:
return user_auth_row


def signup(code: str, email: Optional[str] = None) -> dict:
"""
Get authenticated user, upsert user with email and preferences
:param email: user email
:param code: strava code
:return: jwt_token
"""
def signup(user_auth: UserAuthRow, email: Optional[str] = None) -> dict:
""" """
preferences = (
"I'm looking to improve my running performance while being smart and realistic."
)
send_alert_email(
subject="TrackFlow Alert: New Signup Attempt",
text_content=f"You have a new client {email=} attempting to signup with {preferences=}",
)
user_auth = authenticate_with_code(code)
upsert_user(
UserRow(
athlete_id=user_auth.athlete_id,
email=email,
preferences=preferences,
)
)
return {"success": True, "jwt_token": user_auth.jwt_token}


def signup_with_user_auth(user_auth: UserAuthRow) -> dict:
""" """
preferences = (
"I'm looking to improve my running performance while being smart and realistic."
)
upsert_user(UserRow(athlete_id=user_auth.athlete_id, preferences=preferences))
return {"success": True, "jwt_token": user_auth.jwt_token}


def authenticate_and_maybe_signup(code: str, email: Optional[str] = None) -> dict:
def authenticate_on_signin(code: str, email: Optional[str] = None) -> dict:
"""
Authenticate with Strava code, and sign up the user if they don't exist.
Expand All @@ -187,6 +165,6 @@ def authenticate_and_maybe_signup(code: str, email: Optional[str] = None) -> dic
user_auth = authenticate_with_code(code)

if not user_exists(user_auth.athlete_id):
return signup_with_user_auth(user_auth)
return signup(user_auth, email)

return {"success": True, "jwt_token": user_auth.jwt_token}
10 changes: 3 additions & 7 deletions src/lambda_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,11 @@ def strategy_router(event: dict, invocation_id: str) -> dict:
only present if success is False
"""

if event.get("email") and event.get("code"):
return auth_manager.signup(
code=event["code"],
email=event["email"],
if event.get("code"):
return auth_manager.authenticate_on_signin(
code=event["code"], email=event.get("email")
)

elif event.get("code"):
return auth_manager.authenticate_and_maybe_signup(event["code"])

elif event.get("jwt_token") and event.get("method"):
return frontend_router.handle_request(
jwt_token=event["jwt_token"],
Expand Down
2 changes: 1 addition & 1 deletion src/types/user_row.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ class UserRow(BaseModel):
athlete_id: int
preferences: str
email: Optional[str] = None
preferences_json: Optional[Preferences] = None
preferences_json: Optional[Preferences] = {}
is_active: bool = True
created_at: datetime = datetime.now()

0 comments on commit 2f1c5a0

Please sign in to comment.