@@ -4,148 +4,151 @@ import com.amplifyframework.auth.AuthException
4
4
import com.amplifyframework.auth.AuthUserAttribute
5
5
import com.amplifyframework.auth.AuthUserAttributeKey
6
6
import com.amplifyframework.auth.cognito.AWSCognitoAuthSession
7
- import com.amplifyframework.auth.cognito.AWSCognitoUserPoolTokens
8
7
import com.amplifyframework.auth.options.AuthSignUpOptions
9
8
import com.amplifyframework.kotlin.core.Amplify
9
+ import com.github.code.gambit.data.remote.interceptor.AuthInterceptor
10
10
import com.github.code.gambit.helper.ServiceResult
11
11
import com.github.code.gambit.helper.auth.AuthData
12
12
import com.github.code.gambit.utility.extention.defaultBuilder
13
13
14
- class AuthServiceImpl : AuthService {
14
+ class AuthServiceImpl ( private val authInterceptor : AuthInterceptor ) : AuthService {
15
15
16
16
override suspend fun login (authData : AuthData ): ServiceResult <Unit > {
17
- return try {
18
- val result = Amplify .Auth .signIn(authData.email, authData.password)
17
+ return authInterceptor.authRequest({
18
+ Amplify .Auth .signIn(
19
+ authData.email,
20
+ authData.password
21
+ )
22
+ }) { result ->
19
23
if (result.isSignInComplete) {
20
24
ServiceResult .Success (Unit )
21
25
} else {
22
26
ServiceResult .Error (Exception (" Login incomplete + ${result.nextStep} " ))
23
27
}
24
- } catch (e: AuthException ) {
25
- ServiceResult .Error (e)
26
28
}
27
29
}
28
30
29
31
override suspend fun signUp (authData : AuthData ): ServiceResult <Unit > {
30
- val options = AuthSignUpOptions .builder().defaultBuilder(authData)
31
- return try {
32
- val result = Amplify .Auth .signUp(authData.email, authData.password, options)
32
+ return authInterceptor.authRequest({
33
+ val options = AuthSignUpOptions .builder().defaultBuilder(authData)
34
+ Amplify .Auth .signUp(authData.email, authData.password, options)
35
+ }) { result ->
33
36
if (result.isSignUpComplete) {
34
37
ServiceResult .Success (Unit )
35
38
} else {
36
39
ServiceResult .Error (Exception (" SignUp incomplete + ${result.nextStep} " ))
37
40
}
38
- } catch (e: AuthException ) {
39
- ServiceResult .Error (e)
40
41
}
41
42
}
42
43
43
44
override suspend fun logOut (): ServiceResult <Unit > {
44
- return try {
45
- Amplify .Auth .signOut()
46
- ServiceResult .Success (Unit )
47
- } catch (e: AuthException ) {
48
- ServiceResult .Error (e)
49
- }
45
+ return authInterceptor.authRequest({ Amplify .Auth .signOut() }) { ServiceResult .Success (Unit ) }
50
46
}
51
47
52
- override suspend fun resetPassword (oldPassword : String , newPassword : String ): ServiceResult <Unit > {
53
- return try {
54
- val result = Amplify .Auth .updatePassword(oldPassword, newPassword)
55
- ServiceResult .Success (result)
56
- } catch (e: java.lang.Exception ) {
57
- ServiceResult .Error (e)
48
+ override suspend fun resetPassword (
49
+ oldPassword : String ,
50
+ newPassword : String
51
+ ): ServiceResult <Unit > {
52
+ return authInterceptor.authRequest({
53
+ Amplify .Auth .updatePassword(
54
+ oldPassword,
55
+ newPassword
56
+ )
57
+ }) {
58
+ ServiceResult .Success (it)
58
59
}
59
60
}
60
61
61
62
override suspend fun forgotPassword (userEmail : String ): ServiceResult <Unit > {
62
- return try {
63
- Amplify .Auth .resetPassword(userEmail)
63
+ return authInterceptor.authRequest({ Amplify .Auth .resetPassword(userEmail) }) {
64
64
ServiceResult .Success (Unit )
65
- } catch (e: java.lang.Exception ) {
66
- ServiceResult .Error (e)
67
65
}
68
66
}
69
67
70
68
override suspend fun changePassword (
71
69
newPassword : String ,
72
70
confirmationCode : String
73
71
): ServiceResult <Unit > {
74
- return try {
75
- Amplify .Auth .confirmResetPassword(newPassword, confirmationCode)
72
+ return authInterceptor.authRequest({
73
+ Amplify .Auth .confirmResetPassword(
74
+ newPassword,
75
+ confirmationCode
76
+ )
77
+ }) {
76
78
ServiceResult .Success (Unit )
77
- } catch (e: java.lang.Exception ) {
78
- ServiceResult .Error (e)
79
79
}
80
80
}
81
81
82
82
override suspend fun updateUserName (fullName : String ): ServiceResult <String > {
83
- return try {
83
+ return authInterceptor.authRequest( {
84
84
val attribute = AuthUserAttribute (AuthUserAttributeKey .name(), fullName)
85
- val result = Amplify .Auth .updateUserAttribute(attribute)
85
+ Amplify .Auth .updateUserAttribute(attribute)
86
+ }) { result ->
86
87
if (result.isUpdated) {
87
88
ServiceResult .Success (fullName)
88
89
} else {
89
- ServiceResult .Error (AuthException (" User name update failed" , " Switch you internet connection" ))
90
+ ServiceResult .Error (
91
+ AuthException (
92
+ " User name update failed" ,
93
+ " Switch your internet connection"
94
+ )
95
+ )
90
96
}
91
- } catch (e: java.lang.Exception ) {
92
- ServiceResult .Error (e)
93
97
}
94
98
}
95
99
96
100
override suspend fun confirmSignUp (authData : AuthData ): ServiceResult <Unit > {
97
- return try {
98
- val result = Amplify .Auth .confirmSignUp(authData.email, authData.confirmationCode!! )
101
+ return authInterceptor.authRequest({
102
+ Amplify .Auth .confirmSignUp(
103
+ authData.email,
104
+ authData.confirmationCode!!
105
+ )
106
+ }) { result ->
99
107
if (result.isSignUpComplete) {
100
108
ServiceResult .Success (Unit )
101
109
} else {
102
110
ServiceResult .Error (Exception (" Confirmation incomplete + ${result.nextStep} " ))
103
111
}
104
- } catch (e: AuthException ) {
105
- ServiceResult .Error (e)
106
112
}
107
113
}
108
114
109
115
override suspend fun fetchSession (): ServiceResult <AWSCognitoAuthSession > {
110
- return try {
111
- val authSession = Amplify .Auth .fetchAuthSession() as AWSCognitoAuthSession
112
- ServiceResult .Success (authSession)
113
- } catch (e: AuthException ) {
114
- ServiceResult .Error (e)
116
+ return authInterceptor.authRequest({ Amplify .Auth .fetchAuthSession() as AWSCognitoAuthSession }) {
117
+ ServiceResult .Success (
118
+ it
119
+ )
115
120
}
116
121
}
117
122
118
123
override suspend fun fetchIdToken (): ServiceResult <String > {
119
- when ( val session = fetchSession()) {
120
- is ServiceResult . Error -> {
121
- return ServiceResult .Error (session.exception)
122
- }
123
- is ServiceResult . Success < AWSCognitoAuthSession > -> {
124
- val tokens : AWSCognitoUserPoolTokens = session.data.userPoolTokens.value
125
- ? : return ServiceResult . Error ( Exception ( " token not generated, user might be not authentication " ))
126
- return ServiceResult .Success (tokens .idToken)
127
- }
128
- else -> {
129
- return ServiceResult . Error ( Exception ( " Illegal state!! " ))
124
+ return authInterceptor.authRequest({ fetchSession() } ) { session ->
125
+ when (session) {
126
+ is ServiceResult .Error -> {
127
+ ServiceResult . Error (session.exception)
128
+ }
129
+ is ServiceResult . Success < AWSCognitoAuthSession > -> {
130
+ session.data.userPoolTokens.value?. let {
131
+ ServiceResult .Success (it .idToken)
132
+ }
133
+ ? : ServiceResult . Error ( Exception ( " token not generated, user might be not authentication " ))
134
+ }
130
135
}
131
136
}
132
137
}
133
138
134
139
override suspend fun fetchUserAttribute (): ServiceResult <List <AuthUserAttribute >> {
135
- return try {
136
- val attributes = Amplify .Auth .fetchUserAttributes()
137
- ServiceResult .Success (attributes)
138
- } catch (e: AuthException ) {
139
- ServiceResult .Error (e)
140
+ return authInterceptor.authRequest({ Amplify .Auth .fetchUserAttributes() }) {
141
+ ServiceResult .Success (
142
+ it
143
+ )
140
144
}
141
145
}
142
146
143
147
override suspend fun resentConfirmationCode (email : String ): ServiceResult <Unit > {
144
- return try {
145
- Amplify .Auth .resendSignUpCode(email)
146
- ServiceResult .Success (Unit )
147
- } catch (e: AuthException ) {
148
- ServiceResult .Error (e)
148
+ return authInterceptor.authRequest({ Amplify .Auth .resendSignUpCode(email) }) {
149
+ ServiceResult .Success (
150
+ Unit
151
+ )
149
152
}
150
153
}
151
154
}
0 commit comments