-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCognitoMethods
More file actions
224 lines (198 loc) · 10.4 KB
/
CognitoMethods
File metadata and controls
224 lines (198 loc) · 10.4 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
fun initAmazonMobile() {
AWSMobileClient.getInstance().initialize(applicationContext, object : Callback<UserStateDetails> {
override fun onResult(userStateDetails: UserStateDetails) {
MyLog.e("INIT = onResult: " + userStateDetails.userState)
when (userStateDetails.userState) {
UserState.SIGNED_IN -> runOnUiThread {
toast("Logged IN")
// 로그인된 상태 메인으로 보내기
}
UserState.SIGNED_OUT -> runOnUiThread {
toast("Logged OUT")
// 로그아웃이 된 상태 로긴창 으로 보내기
}
else -> runOnUiThread{
AWSMobileClient.getInstance().signOut()
}
}
}
override fun onError(e: Exception) {
MyLog.e("INIT Initialization error. e=" + e.toString())
runOnUiThread{
AWSMobileClient.getInstance().signOut()
}
}
})
}
fun signUp(username:String, password:String, attributes:HashMap<String, String>) {
AWSMobileClient.getInstance().signUp(username, password, attributes, null, object : Callback<SignUpResult> {
override fun onResult(signUpResult: SignUpResult) {
runOnUiThread {
MyLog.e("Sign-up callback state: " + signUpResult.getConfirmationState())
if (!signUpResult.getConfirmationState()) {
toast("Confirm sign-up with: " + signUpResult.getUserCodeDeliveryDetails().getDestination())
} else {
toast("Sign-up done.")
}
}
}
override fun onError(e: Exception) {
MyLog.e("Sign-up error / e="+e)
runOnUiThread {
toast(e.toString())
}
}
})
}
fun resendConfirmCode(username:String) {
AWSMobileClient.getInstance().resendSignUp(username, object : Callback<SignUpResult> {
override fun onResult(signUpResult: SignUpResult) {
MyLog.e("A verification code has been sent via" +
signUpResult.userCodeDeliveryDetails.deliveryMedium
+ " at " + signUpResult.userCodeDeliveryDetails.destination )
}
override fun onError(e: Exception) {
MyLog.e("e=" +e.toString())
}
})
}
fun confirmSignupCode(username:String, code:String) {
AWSMobileClient.getInstance().confirmSignUp(username, code, object : Callback<SignUpResult> {
override fun onResult(signUpResult: SignUpResult) {
runOnUiThread {
MyLog.e("Sign-up callback state: " + signUpResult.confirmationState)
if (!signUpResult.confirmationState) {
val details = signUpResult.userCodeDeliveryDetails
toast("Confirm sign-up with: " + details.destination)
MyLog.e("Confirm sign-up with: " + details.destination)
} else {
toast("Sign-up done.") //인증번호 완료 후 이쪽으로
MyLog.e("Sign-up done.")
}
}
}
override fun onError(e: Exception) {
MyLog.e("Confirm sign-up error / e=" + e.toString())
}
})
}
fun signIn(username:String, password:String) {
AWSMobileClient.getInstance().signIn(username, password, null, object : Callback<SignInResult> {
override fun onResult(signInResult: SignInResult) {
runOnUiThread {
MyLog.e("Sign-in callback state: " + signInResult.signInState)
when (signInResult.signInState) {
SignInState.DONE -> toast("Sign-in done.") //이메일 인증 완료 후 로그인을 하면 이쪽으로 넘어옴
SignInState.SMS_MFA -> toast("Please confirm sign-in with SMS.")
SignInState.NEW_PASSWORD_REQUIRED -> toast("Please confirm sign-in with new password.")
else -> toast("Unsupported sign-in confirmation: " + signInResult.signInState)
}
}
}
override fun onError(e: Exception) {
MyLog.e( "Sign-in error / e="+e.toString())
runOnUiThread {
toast( "Sign-in error / e="+e.toString())
}
}
})
}
fun signOut() {
AWSMobileClient.getInstance()
.signOut(SignOutOptions.builder().signOutGlobally(true).build(), object : Callback<Void> {
override fun onResult(result: Void) {
MyLog.e("signed-out")
}
override fun onError(e: Exception) {
MyLog.e("sign-out error / e=" +e.toString())
}
})
}
fun forgotPassword(username:String) {
AWSMobileClient.getInstance().forgotPassword(username, object : Callback<ForgotPasswordResult> { //가입 시 이메일로 코드 날라감
override fun onResult(result: ForgotPasswordResult) {
runOnUiThread {
MyLog.e("forgot password state: " + result.getState())
when (result.getState()) {
ForgotPasswordState.CONFIRMATION_CODE -> {
toast("Confirmation code is sent to reset password")
MyLog.e("Confirmation code is sent to reset password")
}
else -> MyLog.e("un-supported forgot password state")
}
}
}
override fun onError(e: Exception) {
MyLog.e("forgot password error / e="+ e)
}
})
}
fun changePassword(newpassword : String, verifyingCode : String) {
AWSMobileClient.getInstance()
.confirmForgotPassword(newpassword, verifyingCode, object : Callback<ForgotPasswordResult> { //forgotPassword 이후에 메일로 코드가 감. 그거랑 신규패스워드 넣고 다시세팅하기
override fun onResult(result: ForgotPasswordResult) {
MyLog.e("forgot password state: " + result.state)
runOnUiThread {
when (result.state) {
ForgotPasswordState.DONE -> {
toast("Password changed successfully")
MyLog.e("Password changed successfully")
}
else -> MyLog.e("un-supported forgot password state")
}
}
}
override fun onError(e: Exception) {
MyLog.e("forgot password error / e="+ e)
}
})
}
fun getUserInfo() {
MyLog.e("AWSMobileClient.getInstance().getUsername()="+AWSMobileClient.getInstance().getUsername()) //String
MyLog.e("AWSMobileClient.getInstance().isSignedIn()="+AWSMobileClient.getInstance().isSignedIn()) //Boolean
MyLog.e("AWSMobileClient.getInstance().getIdentityId()="+AWSMobileClient.getInstance().getIdentityId()) //String
MyLog.e("AWSMobileClient.getInstance().getTokens()="+AWSMobileClient.getInstance().getTokens()) //OIDC Tokens
MyLog.e("AWSMobileClient.getInstance().getTokens().getIdToken().getTokenString()="+AWSMobileClient.getInstance().getTokens().getIdToken().getTokenString()) //OIDC Tokens
val mPreferenceUtil : PreferenceUtil by inject()
mPreferenceUtil.putAccessToken(AWSMobileClient.getInstance().getTokens().getIdToken().getTokenString()) //TODO put access token for Header
doAsync {
MyLog.e("AWSMobileClient.getInstance().getCredentials().awsAccessKeyId="+AWSMobileClient.getInstance().getCredentials().awsAccessKeyId.toString()) //AWS Credentials
MyLog.e("AWSMobileClient.getInstance().getCredentials().awsSecretKey="+AWSMobileClient.getInstance().getCredentials().awsSecretKey.toString()) //AWS Credentials
}
}
fun getUserAttributes() {
AWSMobileClient.getInstance().getUserAttributes(object : Callback<Map<String, String>> {
override fun onResult(result: Map<String, String>?) {
MyLog.e("getUserAttributes / result = " + result.toString())
MyLog.e("getUserAttributes / result?.entries = " + result?.keys.toString())
MyLog.e("getUserAttributes / result?.entries = " + result?.entries.toString())
MyLog.e("- - - - - - - - - - - - - - - - - - -")
MyLog.e("getUserAttributes / result?.get(\"sub\") = " + result?.get("sub"))
MyLog.e("getUserAttributes / result?.get(\"gender\") = " + result?.get("gender"))
MyLog.e("getUserAttributes / result?.get(\"nickname\") = " + result?.get("nickname"))
MyLog.e("getUserAttributes / result?.get(\"locale\") = " + result?.get("locale"))
MyLog.e("getUserAttributes / result?.get(\"email\") = " + result?.get("email"))
//TODO UI UPDATE HERE
}
override fun onError(e: java.lang.Exception?) {
MyLog.e( "getUserAttributes / onError ="+ e?.localizedMessage.toString())
toast(e?.localizedMessage.toString())
}
})
}
fun updateUserAttributes(params : MutableMap<String, String>) {
AWSMobileClient.getInstance().updateUserAttributes( params , object : Callback<List<UserCodeDeliveryDetails>> {
override fun onResult(result: List<UserCodeDeliveryDetails>?) {
MyLog.e( "getUserAttributes / result ="+ result.toString())
runOnUiThread {
toast("str_success")
}
}
override fun onError(e: java.lang.Exception?) {
MyLog.e( "getUserAttributes / onError ="+ e?.localizedMessage.toString())
runOnUiThread {
toast(e?.localizedMessage.toString())
}
}
})
}