Skip to content

Commit

Permalink
jwt producer moved to data layer
Browse files Browse the repository at this point in the history
Merge branch 'refactor'
  • Loading branch information
mustfaunlu committed Aug 16, 2023
2 parents 56f915b + cbc5975 commit 3101cb2
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mustafaunlu.ecommerce.data.repository

import com.mustafaunlu.ecommerce.common.TokenManager
import com.mustafaunlu.ecommerce.data.source.remote.FirebaseDataSource
import com.mustafaunlu.ecommerce.domain.entity.user.FirebaseSignInUserEntity
import com.mustafaunlu.ecommerce.domain.entity.user.UserInformationEntity
Expand All @@ -8,6 +9,7 @@ import javax.inject.Inject

class FirebaseRepositoryImpl @Inject constructor(
private val firebaseDataSource: FirebaseDataSource,
private val tokenManager: TokenManager
) : FirebaseRepository {
override fun signUpWithFirebase(
user: UserInformationEntity,
Expand All @@ -22,7 +24,14 @@ class FirebaseRepositoryImpl @Inject constructor(
onSuccess: (UserInformationEntity) -> Unit,
onFailure: (String) -> Unit
) {
firebaseDataSource.signInWithFirebase(user, onSuccess, onFailure)
firebaseDataSource.signInWithFirebase(
user,
onSuccess = { userInformationEntity ->
tokenManager.saveToken(userInformationEntity.token)
onSuccess(userInformationEntity)
},
onFailure
)
}

override fun forgotPassword(email: String, onSuccess: () -> Unit, onFailure: (String) -> Unit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import com.google.firebase.auth.ktx.userProfileChangeRequest
import com.google.firebase.firestore.FirebaseFirestore
import com.mustafaunlu.ecommerce.domain.entity.user.FirebaseSignInUserEntity
import com.mustafaunlu.ecommerce.domain.entity.user.UserInformationEntity
import io.github.nefilim.kjwt.JWT
import io.github.nefilim.kjwt.toJWTKeyID
import java.time.Instant
import javax.inject.Inject

class FirebaseDataSourceImpl @Inject constructor(
Expand Down Expand Up @@ -44,6 +47,7 @@ class FirebaseDataSourceImpl @Inject constructor(
phone = firebaseUser?.phoneNumber ?: "",
image = firebaseUser?.photoUrl.toString(),
password = "",
token = createJwtTokenForFirebaseUser(),
)
)
}.addOnFailureListener {
Expand Down Expand Up @@ -103,10 +107,20 @@ class FirebaseDataSourceImpl @Inject constructor(
phone = snapshot.getString("phone") ?: "",
image = snapshot.getString("image") ?: "",
password = "",
token = "",
)
)
}.addOnFailureListener {
onFailure(it.message ?: "An error occurred")
}
}
private fun createJwtTokenForFirebaseUser(): String {
val now = Instant.now()
val expirationTime = now.plusSeconds(180)
val jwt = JWT.es256("fb-user123".toJWTKeyID()) {
issuedAt(now)
claim("exp", expirationTime.epochSecond)
}.encode()
return jwt
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ data class UserInformationEntity(
val phone: String,
val image: String = "",
val password: String,
val token: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ data class UserInformationUiData(
val phone: String,
val image: String,
val password: String,
val token: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.mustafaunlu.ecommerce.ui.auth.sign_in

import android.content.SharedPreferences
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand All @@ -12,7 +11,6 @@ import androidx.navigation.fragment.findNavController
import com.mustafaunlu.ecommerce.R
import com.mustafaunlu.ecommerce.common.Constants.PREF_FIREBASE_USERID_KEY
import com.mustafaunlu.ecommerce.common.ScreenState
import com.mustafaunlu.ecommerce.common.TokenManager
import com.mustafaunlu.ecommerce.databinding.FragmentSignInBinding
import com.mustafaunlu.ecommerce.domain.entity.user.FirebaseSignInUserEntity
import com.mustafaunlu.ecommerce.utils.checkInternetConnection
Expand All @@ -21,9 +19,6 @@ import com.mustafaunlu.ecommerce.utils.safeNavigate
import com.mustafaunlu.ecommerce.utils.showToast
import com.mustafaunlu.ecommerce.utils.visible
import dagger.hilt.android.AndroidEntryPoint
import io.github.nefilim.kjwt.JWT
import io.github.nefilim.kjwt.toJWTKeyID
import java.time.Instant
import javax.inject.Inject

@AndroidEntryPoint
Expand All @@ -34,9 +29,6 @@ class SignInFragment : Fragment() {
@Inject
lateinit var sharedPref: SharedPreferences

@Inject
lateinit var tokenManager: TokenManager

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand Down Expand Up @@ -72,7 +64,6 @@ class SignInFragment : Fragment() {
}

is ScreenState.Success -> {
tokenManager.saveToken(createJwtTokenForFirebaseUser())
binding.apply {
loading.gone()
btnSignIn.isEnabled = true
Expand Down Expand Up @@ -104,18 +95,6 @@ class SignInFragment : Fragment() {
findNavController().safeNavigate(SignInFragmentDirections.actionLoginFragmentToHomeFragment())
}

// TODO() Move this logic to backend
private fun createJwtTokenForFirebaseUser(): String {
val now = Instant.now()
val expirationTime = now.plusSeconds(180)
val jwt = JWT.es256("fb-user123".toJWTKeyID()) {
issuedAt(now)
claim("exp", expirationTime.epochSecond)
}.encode()
Log.d("JWT", "Firebase JWT: $jwt")
return jwt
}

private fun setupLoginButton() {
binding.btnSignIn.setOnClickListener {
firebaseLoginLogic()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class SignupFragment : Fragment() {
phone = phone,
image = "",
password = password,
token = "",
),
)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class UserInfoEntityToUiDataMapper @Inject constructor() : ProductBaseMapper<Use
phone = input.phone,
image = input.image,
password = input.password,
token = input.token,
)
}
}
Expand All @@ -29,6 +30,7 @@ class UserInfoUiDataToEntityMapper @Inject constructor() : ProductBaseMapper<Use
phone = input.phone,
image = input.image,
password = input.password,
token = input.token,
)
}
}

0 comments on commit 3101cb2

Please sign in to comment.