mirror of
https://github.com/ClaytonWWilson/Listify.git
synced 2025-12-16 02:38:47 +00:00
Finish conversion to synchronized methods
Finished setting callbacks and made waiting volatile so cached value is not used across threads.
This commit is contained in:
parent
63d06b46c2
commit
2b1ecbadef
@ -4,19 +4,21 @@ import android.util.Log;
|
|||||||
import com.amplifyframework.auth.AuthException;
|
import com.amplifyframework.auth.AuthException;
|
||||||
import com.amplifyframework.auth.AuthSession;
|
import com.amplifyframework.auth.AuthSession;
|
||||||
import com.amplifyframework.auth.options.AuthSignUpOptions;
|
import com.amplifyframework.auth.options.AuthSignUpOptions;
|
||||||
|
import com.amplifyframework.auth.result.AuthSignInResult;
|
||||||
import com.amplifyframework.auth.result.AuthSignUpResult;
|
import com.amplifyframework.auth.result.AuthSignUpResult;
|
||||||
import com.amplifyframework.core.Amplify;
|
import com.amplifyframework.core.Amplify;
|
||||||
|
|
||||||
public class AuthManager {
|
public class AuthManager {
|
||||||
AuthSession authSession = null;
|
AuthSession authSession = null;
|
||||||
AuthSignUpResult authSignUpResult = null;
|
AuthSignUpResult authSignUpResult = null;
|
||||||
|
AuthSignInResult authSignInResult = null;
|
||||||
AuthException authError = null;
|
AuthException authError = null;
|
||||||
String email = null;
|
String email = null;
|
||||||
String password = null;
|
String password = null;
|
||||||
boolean waiting = false;
|
volatile boolean waiting = false;
|
||||||
|
|
||||||
|
|
||||||
public void fetchAuthSession() throws AuthException {
|
void fetchAuthSession() throws AuthException {
|
||||||
waiting = true;
|
waiting = true;
|
||||||
Amplify.Auth.fetchAuthSession(
|
Amplify.Auth.fetchAuthSession(
|
||||||
result -> setAuthSession(result),
|
result -> setAuthSession(result),
|
||||||
@ -25,9 +27,15 @@ public class AuthManager {
|
|||||||
throwIfAuthError();
|
throwIfAuthError();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public AuthSession getAuthSession() throws AuthException {
|
||||||
|
fetchAuthSession();
|
||||||
|
return authSession;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public void setAuthSession(AuthSession toSet) {
|
public void setAuthSession(AuthSession toSet) {
|
||||||
authSession = toSet;
|
authSession = toSet;
|
||||||
|
waiting = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAuthError(AuthException newError) {
|
public void setAuthError(AuthException newError) {
|
||||||
@ -49,7 +57,12 @@ public class AuthManager {
|
|||||||
authSignUpResult = toSet;
|
authSignUpResult = toSet;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void startSignup(String email, String password) throws AuthException {
|
public void setAuthSignInResult(AuthSignInResult toSet) {
|
||||||
|
authSignInResult = toSet;
|
||||||
|
waiting = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void startSignUp(String email, String password) throws AuthException {
|
||||||
this.email = email;
|
this.email = email;
|
||||||
this.password = password;
|
this.password = password;
|
||||||
waiting = true;
|
waiting = true;
|
||||||
@ -64,22 +77,28 @@ public class AuthManager {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void confirmSignUp(String confirmationCode) {
|
public void confirmSignUp(String confirmationCode) throws AuthException {
|
||||||
|
waiting = true;
|
||||||
Amplify.Auth.confirmSignUp(
|
Amplify.Auth.confirmSignUp(
|
||||||
email,
|
email,
|
||||||
confirmationCode,
|
confirmationCode,
|
||||||
result -> Log.i("AuthQuickstart", result.isSignUpComplete() ? "Confirm signUp succeeded" : "Confirm sign up not complete"),
|
result -> setAuthSignUpResult(result),
|
||||||
error -> Log.e("AuthQuickstart", error.toString())
|
error -> setAuthError(error)
|
||||||
);
|
);
|
||||||
|
throwIfAuthError();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void signIn(String email, String password) {
|
public void signIn(String email, String password) throws AuthException{
|
||||||
|
this.email = email;
|
||||||
|
this.password = password;
|
||||||
|
waiting = true;
|
||||||
Amplify.Auth.signIn(
|
Amplify.Auth.signIn(
|
||||||
email,
|
email,
|
||||||
password,
|
password,
|
||||||
result -> Log.i("AuthQuickstart", result.isSignInComplete() ? "Sign in succeeded" : "Sign in not complete"),
|
result -> setAuthSignInResult(result),
|
||||||
error -> Log.e("AuthQuickstart", error.toString())
|
error -> setAuthError(error)
|
||||||
);
|
);
|
||||||
|
throwIfAuthError();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -5,6 +5,8 @@ import android.util.Log;
|
|||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
|
|
||||||
|
import com.amazonaws.mobileconnectors.cognitoauth.Auth;
|
||||||
|
import com.amplifyframework.auth.AuthException;
|
||||||
import com.amplifyframework.auth.AuthUserAttributeKey;
|
import com.amplifyframework.auth.AuthUserAttributeKey;
|
||||||
import com.amplifyframework.auth.options.AuthSignUpOptions;
|
import com.amplifyframework.auth.options.AuthSignUpOptions;
|
||||||
import com.amplifyframework.core.Amplify;
|
import com.amplifyframework.core.Amplify;
|
||||||
@ -29,6 +31,19 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
//------------------------------Auth Testing---------------------------------------------//
|
//------------------------------Auth Testing---------------------------------------------//
|
||||||
|
|
||||||
|
AuthManager authManager = new AuthManager();
|
||||||
|
try {
|
||||||
|
authManager.signIn("merzn@purdue.edu", "Password123");
|
||||||
|
Log.i("Authentication", authManager.getAuthSession().toString());
|
||||||
|
} catch (AuthException e) {
|
||||||
|
Log.i("Authentication", "Login failed. User probably needs to register. Exact error: " + e.getMessage());
|
||||||
|
try {
|
||||||
|
authManager.startSignUp("merzn@purdue.edu", "Password123");
|
||||||
|
authManager.confirmSignUp("######");
|
||||||
|
} catch (AuthException signUpError) {
|
||||||
|
Log.e("Authentication", "SignUp error: " + signUpError.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user