@@ -4,148 +4,151 @@ import com.amplifyframework.auth.AuthException
44import com.amplifyframework.auth.AuthUserAttribute
55import com.amplifyframework.auth.AuthUserAttributeKey
66import com.amplifyframework.auth.cognito.AWSCognitoAuthSession
7- import com.amplifyframework.auth.cognito.AWSCognitoUserPoolTokens
87import com.amplifyframework.auth.options.AuthSignUpOptions
98import com.amplifyframework.kotlin.core.Amplify
9+ import com.github.code.gambit.data.remote.interceptor.AuthInterceptor
1010import com.github.code.gambit.helper.ServiceResult
1111import com.github.code.gambit.helper.auth.AuthData
1212import com.github.code.gambit.utility.extention.defaultBuilder
1313
14- class AuthServiceImpl : AuthService {
14+ class AuthServiceImpl ( private val authInterceptor : AuthInterceptor ) : AuthService {
1515
1616 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 ->
1923 if (result.isSignInComplete) {
2024 ServiceResult .Success (Unit )
2125 } else {
2226 ServiceResult .Error (Exception (" Login incomplete + ${result.nextStep} " ))
2327 }
24- } catch (e: AuthException ) {
25- ServiceResult .Error (e)
2628 }
2729 }
2830
2931 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 ->
3336 if (result.isSignUpComplete) {
3437 ServiceResult .Success (Unit )
3538 } else {
3639 ServiceResult .Error (Exception (" SignUp incomplete + ${result.nextStep} " ))
3740 }
38- } catch (e: AuthException ) {
39- ServiceResult .Error (e)
4041 }
4142 }
4243
4344 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 ) }
5046 }
5147
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)
5859 }
5960 }
6061
6162 override suspend fun forgotPassword (userEmail : String ): ServiceResult <Unit > {
62- return try {
63- Amplify .Auth .resetPassword(userEmail)
63+ return authInterceptor.authRequest({ Amplify .Auth .resetPassword(userEmail) }) {
6464 ServiceResult .Success (Unit )
65- } catch (e: java.lang.Exception ) {
66- ServiceResult .Error (e)
6765 }
6866 }
6967
7068 override suspend fun changePassword (
7169 newPassword : String ,
7270 confirmationCode : String
7371 ): 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+ }) {
7678 ServiceResult .Success (Unit )
77- } catch (e: java.lang.Exception ) {
78- ServiceResult .Error (e)
7979 }
8080 }
8181
8282 override suspend fun updateUserName (fullName : String ): ServiceResult <String > {
83- return try {
83+ return authInterceptor.authRequest( {
8484 val attribute = AuthUserAttribute (AuthUserAttributeKey .name(), fullName)
85- val result = Amplify .Auth .updateUserAttribute(attribute)
85+ Amplify .Auth .updateUserAttribute(attribute)
86+ }) { result ->
8687 if (result.isUpdated) {
8788 ServiceResult .Success (fullName)
8889 } 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+ )
9096 }
91- } catch (e: java.lang.Exception ) {
92- ServiceResult .Error (e)
9397 }
9498 }
9599
96100 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 ->
99107 if (result.isSignUpComplete) {
100108 ServiceResult .Success (Unit )
101109 } else {
102110 ServiceResult .Error (Exception (" Confirmation incomplete + ${result.nextStep} " ))
103111 }
104- } catch (e: AuthException ) {
105- ServiceResult .Error (e)
106112 }
107113 }
108114
109115 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+ )
115120 }
116121 }
117122
118123 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+ }
130135 }
131136 }
132137 }
133138
134139 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+ )
140144 }
141145 }
142146
143147 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+ )
149152 }
150153 }
151154}
0 commit comments