Skip to content

Instantly share code, notes, and snippets.

View theanilpaudel's full-sized avatar

Anil Paudel theanilpaudel

  • Kathmandu, Nepal
View GitHub Profile
@theanilpaudel
theanilpaudel / NetworkModule.kt
Created June 15, 2022 14:34
DaggerHilt implementation of Dependency Injection of Network module.
@Provides
@Singleton
fun provideTokenInterceptor():TokenInterceptor {
return TokenInterceptor()
}
@Provides
@Singleton
fun provideTokenAuthenticator(@ApplicationContext context: Context):TokenAutheticator {
return TokenAutheticator(context)
@theanilpaudel
theanilpaudel / BaseActivity.kt
Last active June 15, 2022 14:32
BaseActivity.kt class for receiving the information from LocalBrodcastReceiver
// Include in BaseActivity
val logoutReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
Toast.makeText(
this@BaseActivity,
getString(R.string.session_expired),
Toast.LENGTH_SHORT
).show()
FirebaseAuth.getInstance().signOut()
LoginActivity.start(this@BaseActivity)
@theanilpaudel
theanilpaudel / TokenInterceptor.kt
Created June 15, 2022 14:07
TokenInterceptor for passing token in every api request except /login
@Singleton
class TokenInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
var request = chain.request()
try {
if (request.url.encodedPath.contains("/login") && request.method.equals("POST")) {
return chain.proceed(request)
}
@theanilpaudel
theanilpaudel / TokenAuthenticator.kt
Created June 15, 2022 14:06
TokenAuthenticator for handling token expiry.
@Singleton
class TokenAutheticator @Inject constructor(
@ApplicationContext private val context: Context
) : Authenticator {
override fun authenticate(route: Route?, response: Response): Request? {
if (!response.isSuccessful) {
val localBroadcastManager = LocalBroadcastManager.getInstance(context)
val intent = Intent(Keys.ACTION_LOGOUT)
localBroadcastManager.sendBroadcast(intent)
@theanilpaudel
theanilpaudel / LoginActivity.kt
Created June 15, 2022 14:03
LoginActivity with code for FirebaseAuthentication using email and password
bLogin.setOnClickListener {
Timber.d("email %s and password %s", email, password)
auth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this@LoginActivity) { task ->
if (task.isSuccessful) {
Timber.d("signInWithEmail:success")
val user = auth.currentUser
updateUI(user)
} else {
task.exception?.printStackTrace()