我的Android应用使用firebase来验证和管理用户。登录和帐户创建似乎工作正常,但是当我尝试在用户创建帐户后获取用户令牌以便将某些数据保存到我的后端时,用户令牌为空。
以下是 SignUpMainFragment :
public class SignUpMainFragment extends Fragment {
private EditText mEmailEditText;
private EditText mPasswordEditText;
private SignUpListener mCallback;
public SignUpMainFragment() {
// Required empty public constructor
}
public interface SignUpListener {
void signUp(String email, String password);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_sign_up_main, container, false);
Button continueButton = (Button) view.findViewById(R.id.sign_up_continue_button);
mEmailEditText = (EditText) view.findViewById(R.id.sign_up_email_edit_text);
mPasswordEditText = (EditText) view.findViewById(R.id.sign_up_password_edit_text);
continueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Validate the data and if successful continue if not get user to re-enter data.
final String email = mEmailEditText.getText().toString();
final String password = mPasswordEditText.getText().toString();
if (Validate.isEmailValid(email) && Validate.isPasswordValid(password)) {
mCallback.signUp(email, password);
} else if (!Validate.isEmailValid(email) && !Validate.isPasswordValid(password)) {
mEmailEditText.setError("Email invalid");
mPasswordEditText.setError("Password invalid");
} else if (!Validate.isEmailValid(email)) {
mEmailEditText.setError("Email invalid");
} else {
mPasswordEditText.setError("Password invalid");
}
}
});
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof SignUpListener) {
mCallback = (SignUpListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mCallback = null;
}
}
以下是 SignUpActivity :
public class SignUpActivity extends AppCompatActivity implements SignUpMainFragment.SignUpListener,
SignUpAboutFragment.DoneListener, SignUpActivityView {
public static final String SIGN_UP_MAIN_FRAGMENT_TAG = "sign_up_main_fragment";
public static final String SIGN_UP_ABOUT_FRAGMENT_TAG = "sign_up_about_fragment";
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private SignUpActivityPresenter mPresenter;
private String mUserId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
mPresenter = new SignUpActivityPresenter(this);
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
mUserId = user.getToken(true).toString();
Fragment signUpAboutFragment = new SignUpAboutFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.sign_up_fragment_container, signUpAboutFragment, SIGN_UP_ABOUT_FRAGMENT_TAG);
ft.addToBackStack(null);
ft.commit();
}
}
};
Fragment signUpMainFragment = new SignUpMainFragment();
mPresenter.addInitialFragment(signUpMainFragment, SIGN_UP_MAIN_FRAGMENT_TAG);
}
@Override
public void signUp(String email, String password) {
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
/**
* If sign in fails, display a message to the user. If sign in succeeds
* the auth state listener will be notified and logic to handle the
* signed in user can be handled in the listener.
*/
if (!task.isSuccessful()) { }
}
});
}
@Override
public void saveAboutData(String firstName, String lastName, String birthday) {
mPresenter.saveUserData(firstName, lastName, birthday, mUserId, this, MainActivity.class);
}
@Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
public void onStop() {
super.onStop();
mAuth.removeAuthStateListener(mAuthListener);
}
/**
* Activity view operations.
*
* @param fragment to be initially added.
* @param TAG the fragment tag.
*/
@Override
public void addInitialFragment(Fragment fragment, String TAG) {
Fragment savedFragment = getSupportFragmentManager().findFragmentByTag(SIGN_UP_MAIN_FRAGMENT_TAG);
if (savedFragment == null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.sign_up_fragment_container, fragment, TAG);
ft.addToBackStack(null);
ft.commit();
}
}
@Override
public void startActivity(Context context, Class activity) {
Intent intent = new Intent(context, activity);
startActivity(intent);
}
}
此片段验证输入的用户凭据,如果它们有效,则会对实际创建帐户的活动进行接口回调。创建帐户时,应调用身份验证状态侦听器,并且当前FirebaseUser
应为非null。然后我将全局成员变量mUserId
设置为user.getToken(true).toString()
,然后将其用于保存SignUpAboutFragment片段中的数据。
那么为什么mUserId
成员变量 null 以及如何解决此问题以返回实际的用户令牌?