Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,25 @@
android:exported="true"
android:label="@string/app_name"
android:configChanges="orientation|screenSize|screenLayout|keyboardHidden|uiMode"
android:launchMode="singleTask"
android:theme="@style/Theme.Nudj">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />


<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

<data
android:scheme="https"
android:host="nudj-697e5.firebaseapp.com"
android:pathPrefix="/__/auth/action" />
</intent-filter>
</activity>
</application>

</manifest>
94 changes: 85 additions & 9 deletions app/src/main/java/com/tpc/nudj/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.tpc.nudj

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
Expand All @@ -8,6 +10,9 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.viewmodel.navigation3.rememberViewModelStoreNavEntryDecorator
Expand All @@ -16,6 +21,7 @@ import androidx.navigation3.runtime.entryProvider
import androidx.navigation3.runtime.rememberNavBackStack
import androidx.navigation3.ui.NavDisplay
import com.tpc.nudj.ui.navigation.ScreenRoute
import com.tpc.nudj.ui.navigation.VerificationPurpose
import com.tpc.nudj.ui.screen.DemoScreen
import com.tpc.nudj.ui.screen.auth.clubVerification.ClubVerificationScreen
import com.tpc.nudj.ui.screen.auth.emailVerification.EmailVerificationScreen
Expand All @@ -34,17 +40,33 @@ import dagger.hilt.android.AndroidEntryPoint
@AndroidEntryPoint

class MainActivity : ComponentActivity() {
private var deepLinkuri by mutableStateOf<Uri?>(null)

override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
setIntent(intent)
deepLinkuri = intent.data
}

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)
deepLinkuri = intent?.data
enableEdgeToEdge()
setContent {
NudjTheme {
val appViewModel: AppViewModel = hiltViewModel()
val authState by appViewModel.authState.collectAsState()
var comingEmailActionData by remember {
mutableStateOf<EmailActionData?>(null)
}

val backStack = rememberNavBackStack(ScreenRoute.Auth.SplashScreen)

LaunchedEffect(authState) {
val deepLinkMode = deepLinkuri?.getQueryParameter("mode")
if (deepLinkMode == "resetPassword" || deepLinkMode == "verifyEmail") {
return@LaunchedEffect
}
when (val state = authState) {
is AppViewModel.AuthState.Initial -> {}
is AppViewModel.AuthState.Unauthenticated -> {
Expand All @@ -53,7 +75,7 @@ class MainActivity : ComponentActivity() {
}
is AppViewModel.AuthState.EmailNotVerified -> {
backStack.clear()
backStack.add(ScreenRoute.Auth.EmailVerification)
backStack.add(ScreenRoute.Auth.EmailVerification())
}
is AppViewModel.AuthState.Authenticated -> {
backStack.clear()
Expand All @@ -70,7 +92,35 @@ class MainActivity : ComponentActivity() {
}
}
}
NavDisplay(
LaunchedEffect(deepLinkuri) {
val uri = deepLinkuri ?: return@LaunchedEffect
val emailActionData = EmailActionData(
mode = uri.getQueryParameter("mode"),
oobCode = uri.getQueryParameter("oobCode")
)
if (
emailActionData.mode == "resetPassword" ||
emailActionData.mode == "verifyEmail"
) {
comingEmailActionData = emailActionData
if (backStack.lastOrNull() !is ScreenRoute.Auth.EmailVerification) {
backStack.clear()
backStack.add(
ScreenRoute.Auth.EmailVerification(
purpose = when (emailActionData.mode) {
"resetPassword" -> VerificationPurpose.PASSWORD_RESET
else -> VerificationPurpose.REGISTRATION
}
)
)
}
}
deepLinkuri = null
}



NavDisplay(
backStack = backStack,
modifier = Modifier.fillMaxSize(),
entryDecorators = listOf(
Expand All @@ -92,6 +142,9 @@ class MainActivity : ComponentActivity() {
LoginScreen(
navigateToCreateAccount ={
backStack.add(ScreenRoute.Auth.Register)
},
navigateToForgotPassword = {
backStack.add(ScreenRoute.Auth.ForgotPassword)
}
)
}
Expand All @@ -102,7 +155,7 @@ class MainActivity : ComponentActivity() {
},
onNavigateToEmailVerification = {
backStack.clear()
backStack.add(ScreenRoute.Auth.EmailVerification)
backStack.add(ScreenRoute.Auth.EmailVerification(purpose = VerificationPurpose.REGISTRATION))
},
onNavigateToUserDetailsInput = {
backStack.clear()
Expand All @@ -115,23 +168,42 @@ class MainActivity : ComponentActivity() {

)
}
entry<ScreenRoute.Auth.EmailVerification> {
entry<ScreenRoute.Auth.EmailVerification> { route ->
EmailVerificationScreen(
email = route.email,
purpose = route.purpose,
emailActionData = comingEmailActionData ?: EmailActionData(),
onNavigateBack = {},
onNavigateToEmailVerified = {
comingEmailActionData = null
backStack.clear()
backStack.add(ScreenRoute.Auth.EmailVerified)
},
onNavigateToResetPassword = { validOobCode ->
comingEmailActionData = null
backStack.clear()
backStack.add(ScreenRoute.Auth.ResetPassword(validOobCode))
}
)
}

entry<ScreenRoute.Auth.ForgotPassword> {
ForgetPasswordScreen(
ForgetPasswordScreen(onNavigateToEmailVerification = { email ->
comingEmailActionData = null
backStack.add(
ScreenRoute.Auth.EmailVerification(
email = email,
purpose = VerificationPurpose.PASSWORD_RESET
)
)
},

onLoginClick = {}
)
}
entry<ScreenRoute.Auth.ResetPassword> {
entry<ScreenRoute.Auth.ResetPassword> {route ->
ResetPasswordScreen(
oobCode = route.oobCode,
onLoginClick = {
backStack.add(ScreenRoute.Auth.Login)
}
Expand All @@ -153,10 +225,10 @@ class MainActivity : ComponentActivity() {
DemoScreen(text = "Student Dashboard")
}
entry<ScreenRoute.App.ClubDashboard>{
ClubLandingScreen()
DemoScreen(text = "Club Dashboard")
}
entry<ScreenRoute.App.ClubVerificationScreen>{
ClubVerificationScreen(onNavigationBack = {})
DemoScreen(text = "Club Verification Dashboard")
}
entry<ScreenRoute.App.UserDetailsInput> {
DemoScreen(text = "User Details Input")
Expand All @@ -167,3 +239,7 @@ class MainActivity : ComponentActivity() {
}
}
}
data class EmailActionData(
val mode: String? = null,
val oobCode: String? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,7 @@ interface AuthRepository {
*/
suspend fun signOut()
suspend fun reloadAndCheckEmailVerified(): Boolean
suspend fun applyEmailVerificationCode(code: String): Result<Unit>
suspend fun resetPassword(code: String , newPassword:String ): Flow<AuthResult>
suspend fun verifyPasswordResetCode(code: String): Result<String>
}
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,30 @@ class FirebaseAuthRepository(
firebaseUser.reload().await()
return firebaseAuth.currentUser?.isEmailVerified == true
}
override suspend fun verifyPasswordResetCode(
code: String
): Result<String> = runCatching {
firebaseAuth.verifyPasswordResetCode(code).await()
}

override suspend fun resetPassword(code: String, newPassword: String): Flow<AuthResult>
= flow {
try {
emit(AuthResult.Loading)
firebaseAuth.confirmPasswordReset(code, newPassword).await()
emit(AuthResult.Success(User()))
} catch (e: Exception) {
emit(
AuthResult.Error(
e.message ?: "Unable to reset password"
)
)
}
}
override suspend fun applyEmailVerificationCode(
code: String
): Result<Unit> = runCatching {
firebaseAuth.applyActionCode(code).await()
firebaseAuth.currentUser?.reload()?.await()
}
}
15 changes: 13 additions & 2 deletions app/src/main/java/com/tpc/nudj/ui/navigation/ScreenRoute.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ package com.tpc.nudj.ui.navigation
import androidx.navigation3.runtime.NavKey
import kotlinx.serialization.Serializable

@Serializable
enum class VerificationPurpose {
REGISTRATION,
PASSWORD_RESET
}

@Serializable
sealed interface ScreenRoute : NavKey {

Expand All @@ -21,7 +27,10 @@ sealed interface ScreenRoute : NavKey {
data object Register : Auth

@Serializable
data object EmailVerification : Auth
data class EmailVerification(
val email: String = "",
val purpose: VerificationPurpose = VerificationPurpose.REGISTRATION
) : Auth

@Serializable
data object EmailVerified : Auth
Expand All @@ -30,7 +39,9 @@ sealed interface ScreenRoute : NavKey {
data object ForgotPassword : Auth

@Serializable
data object ResetPassword : Auth
data class ResetPassword(
val oobCode : String
) : Auth
}

@Serializable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.tpc.nudj.ui.screen.auth.emailVerification

sealed interface EmailVerificationEvent {
data object NavigateToEmailVerified : EmailVerificationEvent
data class showSnackBar(val message: String) : EmailVerificationEvent
data class ShowSnackBar(val message: String) : EmailVerificationEvent
data object RegistrationVerificationCompleted : EmailVerificationEvent
data class NavigateToResetPassword(val oobCode: String) : EmailVerificationEvent
}
Loading