Skip to content

Commit b95621c

Browse files
Added token logic for existing users
1 parent 97786c8 commit b95621c

4 files changed

Lines changed: 49 additions & 6 deletions

File tree

app/src/main/java/com/cornellappdev/uplift/data/repositories/AuthInterceptor.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class AuthInterceptor @Inject constructor(
1111
) : Interceptor {
1212
override fun intercept(chain: Interceptor.Chain): Response {
1313
val token = tokenManager.getAccessToken()
14+
android.util.Log.d("AuthInterceptor", "token present = ${token != null}")
1415
val request = chain.request().newBuilder().apply {
1516
if (token != null) {
1617
addHeader("Authorization", "Bearer $token")

app/src/main/java/com/cornellappdev/uplift/data/repositories/CheckInRepository.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,13 @@ class CheckInRepository @Inject constructor(
162162

163163
return try {
164164
val response = apolloClient
165-
.mutation(LogWorkoutMutation(facilityId = gymId, workoutTime = time, id = userId ))
165+
.mutation(
166+
LogWorkoutMutation(
167+
facilityId = gymId,
168+
workoutTime = time,
169+
userId = userId
170+
)
171+
)
166172
.execute()
167173

168174
val ok = response.data?.logWorkout?.workoutFields != null && !response.hasErrors()

app/src/main/java/com/cornellappdev/uplift/data/repositories/UserInfoRepository.kt

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class UserInfoRepository @Inject constructor(
8787
}
8888
val goalResponse = apolloClient.mutation(
8989
SetWorkoutGoalsMutation(
90-
id = id,
90+
userId = id,
9191
workoutGoal = goal
9292
)
9393
)
@@ -114,8 +114,36 @@ class UserInfoRepository @Inject constructor(
114114
}
115115
}
116116

117+
suspend fun loginAndStoreTokens(netId: String): Boolean {
118+
return try {
119+
val loginResponse = apolloClient.mutation(
120+
LoginUserMutation(netId = netId)
121+
).execute()
122+
123+
val loginData = loginResponse.data?.loginUser
124+
if (loginResponse.hasErrors() ||
125+
loginData?.accessToken == null ||
126+
loginData.refreshToken == null
127+
) {
128+
Log.e("UserInfoRepository", "Login failed: ${loginResponse.errors}")
129+
return false
130+
}
131+
132+
tokenManager.saveTokens(
133+
loginData.accessToken,
134+
loginData.refreshToken
135+
)
136+
Log.d("UserInfoRepository", "Saved backend tokens successfully")
137+
true
138+
} catch (e: Exception) {
139+
Log.e("UserInfoRepository", "Error logging in user", e)
140+
false
141+
}
142+
}
143+
117144
suspend fun syncUserToDataStore(netId: String): Boolean {
118145
return try {
146+
if (!loginAndStoreTokens(netId)) return false
119147
val user = getUserByNetId(netId) ?: return false
120148

121149
storeUserFields(

app/src/main/java/com/cornellappdev/uplift/ui/screens/profile/ProfileScreen.kt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,17 +172,25 @@ private fun ProfileScreenTopBar(
172172
@Preview
173173
@Composable
174174
private fun ProfileScreenContentPreview() {
175+
val now = System.currentTimeMillis()
176+
val historyItems = listOf(
177+
HistoryItem("Morrison", "11:00 PM", "March 29, 2024", now - (1 * 24 * 60 * 60 * 1000), "1 day ago"),
178+
HistoryItem("Noyes", "1:00 PM", "March 29, 2024", now - (3 * 24 * 60 * 60 * 1000), "2 days ago"),
179+
HistoryItem("Teagle Up", "2:00 PM", "March 29, 2024", now - (7 * 24 * 60 * 60 * 1000), "1 day ago"),
180+
HistoryItem("Teagle Down", "12:00 PM", "March 29, 2024", now - (15 * 24 * 60 * 60 * 1000), "1 day ago"),
181+
HistoryItem("Helen Newman", "10:00 AM", "March 29, 2024", now, "Today"),
182+
)
175183
ProfileScreenContent(
176184
uiState = ProfileUiState(
177185
name = "Melissa Velasquez",
178186
netId = "mv477",
179-
totalGymDays = 0,
187+
totalGymDays = 1,
180188
activeStreak = 0,
181189
workoutGoal = 4,
182-
historyItems = emptyList(),
190+
historyItems = historyItems,
183191
daysOfMonth = listOf(10, 11, 12, 13, 14, 15, 16),
184-
completedDays = listOf(false, false, false, false, false, false, false),
185-
workoutsCompleted = 0
192+
completedDays = listOf(true, false, false, false, false, false, false),
193+
workoutsCompleted = 1
186194
),
187195
{},
188196
{},

0 commit comments

Comments
 (0)