-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAuthManager.kt
More file actions
108 lines (88 loc) · 2.85 KB
/
AuthManager.kt
File metadata and controls
108 lines (88 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package com.kharagedition.tibetankeyboard.auth
import android.app.Activity
import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
import com.google.firebase.auth.ktx.auth
import com.google.firebase.ktx.Firebase
import com.kharagedition.tibetankeyboard.LoginActivity
import com.kharagedition.tibetankeyboard.UserPreferences
import com.kharagedition.tibetankeyboard.subscription.RevenueCatManager
/**
* Manages authentication state and user session
*/
class AuthManager(private val context: Context) {
private val auth: FirebaseAuth = Firebase.auth
private val userPreferences: UserPreferences = UserPreferences(context)
private val revenueCatManager = RevenueCatManager.getInstance()
/**
* Check if user is authenticated
*/
fun isUserAuthenticated(): Boolean {
return auth.currentUser != null && userPreferences.isUserLoggedIn()
}
/**
* Get current user display name
*/
fun getCurrentUserName(): String {
return userPreferences.getUserName().takeIf { it.isNotEmpty() } ?: "User"
}
/**
* Get current user photo URL
*/
fun getCurrentUserPhotoUrl(): String {
return userPreferences.getUserPhotoUrl()
}
/**
* Sign out user from all services
*/
fun signOut(onComplete: () -> Unit) {
revenueCatManager.logout(object : RevenueCatManager.SubscriptionCallback {
override fun onSuccess(message: String) {
auth.signOut()
userPreferences.clearUserData()
onComplete()
}
override fun onError(error: String) {
auth.signOut()
userPreferences.clearUserData()
onComplete()
}
override fun onUserCancelled() {
}
})
}
/**
* Redirect to login activity
*/
fun redirectToLogin() {
val intent = Intent(context, LoginActivity::class.java)
if (context !is Activity) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
context.startActivity(intent)
(context as? Activity)?.finish()
}
/**
* Initialize user session (call this in activities)
*/
fun initializeUserSession(callback: RevenueCatManager.SubscriptionCallback? = null) {
if (!isUserAuthenticated()) {
redirectToLogin()
return
}
// Initialize RevenueCat with current user and Firebase UID
revenueCatManager.initialize(context, auth, callback)
}
/**
* Check if this is user's first time
*/
fun isFirstTimeUser(): Boolean {
return userPreferences.isFirstTimeUser()
}
fun getUser(): FirebaseUser? {
return auth.currentUser
}
}