/
AuthenticationViewModel.kt
130 lines (119 loc) · 4.6 KB
/
AuthenticationViewModel.kt
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package authentication
import app.cash.sqldelight.db.SqlDriver
import com.dwarshb.firebaseauthentication.Database
import com.dwarshb.firebaseauthentication.DatabaseQueries
import com.dwarshb.firebaseauthentication.User
import dev.icerock.moko.mvvm.viewmodel.ViewModel
import firebase.AuthResponse
import firebase.Firebase
import firebase.FirebaseAuth
import firebase.FirebaseDatabase
import firebase.FirebaseUser
import firebase.TokenResponse
import firebase.onCompletion
import io.ktor.http.parameters
import kotlinx.coroutines.launch
class AuthenticationViewModel(var firebase: Firebase? = null,var sqlDriver: SqlDriver) : ViewModel() {
private var databaseQuery : DatabaseQueries
var firebaseAuth = FirebaseAuth()
init {
val database = Database(sqlDriver)
databaseQuery = database.databaseQueries
}
fun validateEmail(email: String): Boolean {
if (email == "") return false
val emailRegex = Regex("^[a-zA-Z0-9+_.-]+@[a-zA-Z0-9.-]+\$")
return emailRegex.matches(email)
}
suspend fun signUp(
email: String,
password: String,
confirmPassword: String,
completion: onCompletion<String>
) {
if (password == confirmPassword) {
firebaseAuth.signUpWithEmailAndPassword(email,password, object : onCompletion<AuthResponse> {
override fun onSuccess(T: AuthResponse) {
storeUserDetails(T)
updateUserInDatabase(T.localId,T.email,T.refreshToken)
completion.onSuccess(T.idToken)
}
override fun onError(e: Exception) {
completion.onError(e)
}
})
} else {
completion.onError(Exception("Password doesn't match"))
}
}
suspend fun login(
email: String,
password: String,
completion: onCompletion<String>
) {
firebaseAuth.login(email,password,object : onCompletion<AuthResponse> {
override fun onSuccess(T: AuthResponse) {
storeUserDetails(T)
updateUserInDatabase(T.localId,T.email,T.refreshToken)
completion.onSuccess(T.idToken)
}
override fun onError(e: Exception) {
completion.onError(e)
}
})
}
private fun updateUserInDatabase(uid:String, email:String, token:String) {
firebase?.setCurrentUser(FirebaseUser(email,token,uid))
val firebaseDatabase = FirebaseDatabase()
viewModelScope.launch {
val child = listOf("users",uid)
val map = HashMap<String,Any>()
map.put("uid",uid)
map.put("email",email)
firebaseDatabase.updateFirebaseDatabase(child,map,
object : onCompletion<String> {
override fun onSuccess(T: String) {
print(T)
}
override fun onError(e: Exception) {
print(e.message)
}
}
)
}
}
internal fun storeUserDetails(response: AuthResponse) {
databaseQuery.insertUser(
response.idToken, response.email, response.refreshToken,
response.email,response.localId
)
}
internal fun checkSession(onCompletion: onCompletion<User>) {
for(user in databaseQuery.selectAllUsers().executeAsList()) {
println(user.toString())
if (user!=null) {
viewModelScope.launch {
firebaseAuth.getRefreshToken(user.refreshToken,
object : onCompletion<TokenResponse> {
override fun onSuccess(T: TokenResponse) {
val _user = User(
idToken = T.id_token,
email = user.email,
localId = T.user_id,
refreshToken = T.refresh_token,
name = user.email
)
onCompletion.onSuccess(_user)
}
override fun onError(e: Exception) {
onCompletion.onError(e)
}
})
}
} else {
onCompletion.onError(Exception("No session found"))
}
}
}
internal fun getDriver() = sqlDriver
}