如何从 android 中的应用注销用户
How to signout a user from the app in android
我是 android 的新手,现在我正在努力让用户使用他们的 facebook 和 gmail 登录一切顺利,但我需要知道如何让用户从应用程序注销和使用新帐户登录用户。我尝试了很多教程,但其中 none 并不像我想象的那样。用户从另一个 activity 单击注销按钮并返回登录 activity。
示例:当用户从 UserAction.class
登录时,注销按钮位于 ProfileFragment.class
我尝试了很多,但没有找到任何解决方案。
我试过的代码
public class UserAction extends AppCompatActivity {
private CallbackManager callbackManager;
public LoginButton loginButton;
private static final String EMAIL = "email";
private SignInButton signInButton;
GoogleSignInClient mGoogleSignInClient;
private FirebaseAuth mAuth;
private int RC_SIGN_IN = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_action);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
signInButton = findViewById(R.id.gmail_login_button);
mAuth = FirebaseAuth.getInstance();
GoogleSignInOptions gsp = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gsp);
signInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GmailLogin();
}
private void GmailLogin() {
Intent mailSignIn = mGoogleSignInClient.getSignInIntent();
startActivityForResult(mailSignIn,RC_SIGN_IN);
}
});
AccessToken accessToken = AccessToken.getCurrentAccessToken();
boolean isLoggedIn = accessToken != null && !accessToken.isExpired();
AppEventsLogger.activateApp(getApplication());
callbackManager = CallbackManager.Factory.create();
loginButton = (LoginButton) findViewById(R.id.fb_login_button);
loginButton.setReadPermissions(Arrays.asList(EMAIL));
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Intent intent = new Intent(UserAction.this, AppMain.class);
startActivity(intent);
}
@Override
public void onCancel() {
// App code
}
@Override
public void onError(FacebookException exception) {
// App code
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
callbackManager.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN){
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
}
super.onActivityResult(requestCode, resultCode, data);
}
private void handleSignInResult(Task<GoogleSignInAccount> CompletedTask) {
try {
GoogleSignInAccount account = CompletedTask.getResult(ApiException.class);
Toast.makeText(this, "Signed in Successfully", Toast.LENGTH_SHORT).show();
FirebaseGoogleAuth(Objects.requireNonNull(account));
} catch (ApiException e) {
ConstraintLayout UserAction = findViewById(R.id.userAction);
Snackbar snackbar = Snackbar.make(UserAction,"Login Failed Please try Again.", Snackbar.LENGTH_SHORT);
snackbar.show();
FirebaseGoogleAuth(null);
}
}
private void FirebaseGoogleAuth(final GoogleSignInAccount account) {
AuthCredential authCredential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
mAuth.signInWithCredential(authCredential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
FirebaseUser user = mAuth.getCurrentUser();
GoogleSignInAccount googleSignInAccount = GoogleSignIn.getLastSignedInAccount(getApplicationContext());
String personName = Objects.requireNonNull(googleSignInAccount).getDisplayName();
String personEmail = googleSignInAccount.getEmail();
String personId = googleSignInAccount.getId();
Toast.makeText(UserAction.this, personName+ "\n" +personEmail + "\n" + personId , Toast.LENGTH_SHORT).show();
}
}
});
}
}
要从 Firebase 注销,请使用
mAuth.signOut();
并退出 google client 使用
mGoogleSignInClient.signOut()
我是 android 的新手,现在我正在努力让用户使用他们的 facebook 和 gmail 登录一切顺利,但我需要知道如何让用户从应用程序注销和使用新帐户登录用户。我尝试了很多教程,但其中 none 并不像我想象的那样。用户从另一个 activity 单击注销按钮并返回登录 activity。
示例:当用户从 UserAction.class
登录时,注销按钮位于 ProfileFragment.class
我尝试了很多,但没有找到任何解决方案。
我试过的代码
public class UserAction extends AppCompatActivity {
private CallbackManager callbackManager;
public LoginButton loginButton;
private static final String EMAIL = "email";
private SignInButton signInButton;
GoogleSignInClient mGoogleSignInClient;
private FirebaseAuth mAuth;
private int RC_SIGN_IN = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_action);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
signInButton = findViewById(R.id.gmail_login_button);
mAuth = FirebaseAuth.getInstance();
GoogleSignInOptions gsp = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gsp);
signInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GmailLogin();
}
private void GmailLogin() {
Intent mailSignIn = mGoogleSignInClient.getSignInIntent();
startActivityForResult(mailSignIn,RC_SIGN_IN);
}
});
AccessToken accessToken = AccessToken.getCurrentAccessToken();
boolean isLoggedIn = accessToken != null && !accessToken.isExpired();
AppEventsLogger.activateApp(getApplication());
callbackManager = CallbackManager.Factory.create();
loginButton = (LoginButton) findViewById(R.id.fb_login_button);
loginButton.setReadPermissions(Arrays.asList(EMAIL));
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Intent intent = new Intent(UserAction.this, AppMain.class);
startActivity(intent);
}
@Override
public void onCancel() {
// App code
}
@Override
public void onError(FacebookException exception) {
// App code
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
callbackManager.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN){
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
}
super.onActivityResult(requestCode, resultCode, data);
}
private void handleSignInResult(Task<GoogleSignInAccount> CompletedTask) {
try {
GoogleSignInAccount account = CompletedTask.getResult(ApiException.class);
Toast.makeText(this, "Signed in Successfully", Toast.LENGTH_SHORT).show();
FirebaseGoogleAuth(Objects.requireNonNull(account));
} catch (ApiException e) {
ConstraintLayout UserAction = findViewById(R.id.userAction);
Snackbar snackbar = Snackbar.make(UserAction,"Login Failed Please try Again.", Snackbar.LENGTH_SHORT);
snackbar.show();
FirebaseGoogleAuth(null);
}
}
private void FirebaseGoogleAuth(final GoogleSignInAccount account) {
AuthCredential authCredential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
mAuth.signInWithCredential(authCredential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
FirebaseUser user = mAuth.getCurrentUser();
GoogleSignInAccount googleSignInAccount = GoogleSignIn.getLastSignedInAccount(getApplicationContext());
String personName = Objects.requireNonNull(googleSignInAccount).getDisplayName();
String personEmail = googleSignInAccount.getEmail();
String personId = googleSignInAccount.getId();
Toast.makeText(UserAction.this, personName+ "\n" +personEmail + "\n" + personId , Toast.LENGTH_SHORT).show();
}
}
});
}
}
要从 Firebase 注销,请使用
mAuth.signOut();
并退出 google client 使用
mGoogleSignInClient.signOut()