2016-11-30 22 views
17

The example cho việc sử dụng reauthenticate() trong căn cứ hỏa lực chỉ hiển thị như thế nào để tái xác nhận một người dùng ký hợp đồng với Email và mật khẩu:Cách xác thực lại người dùng trên Firebase với Nhà cung cấp Google?

AuthCredential credential = EmailAuthProvider.getCredential("[email protected]", "password1234"); 
FirebaseAuth.getInstance().getCurrentUser().reauthenticate(credential); 

Tôi cũng biết làm thế nào để tái xác nhận với nhà cung cấp Facebook (credential = FacebookAuthProvider.getCredential(AccessToken.getCurrentAccessToken().toString())).

Sự cố không có phương pháp tương đương trong API Google để nhận Mã thông báo truy cập hiện tại và cuối cùng nhận được AuthCredential. Vì vậy, những gì tôi vượt qua để getCredential() trong trường hợp này?

Trả lời

0

Xét bạn đã có thể nhận được GoogleSignInResult như một phản ứng để đăng nhập, tôi nghĩ bạn có thể sử dụng đoạn mã sau:

// assuming result variable has GoogleSignInResult 
// GoogleSignInResult result 

// Get the account 
GoogleSignInAccount acct = result.getSignInAccount(); 

// credential 
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null); 
FirebaseAuth.getInstance().getCurrentUser().reauthenticate(credential).addOnCompleteListener(new OnCompleteListener<Void>() {... 
+0

Tôi không nhớ những gì tôi đã kết thúc làm (đó là 4 months ago), nhưng tôi nghĩ tôi cần một cái gì đó tĩnh mà tôi có thể sử dụng bất cứ khi nào tôi muốn và không chỉ khi đăng nhập (ví dụ: nếu người dùng muốn xóa tài khoản của họ). Dù sao, bởi vì tôi không làm việc này nữa, nếu ai đó tin rằng đây là câu trả lời đúng, tôi sẽ chọn nó như một câu trả lời. –

0

Bạn có thể nhận GoogleSignInResult qua 2 cách để xác thực.

i) Bằng cách nhập id email và mật khẩu vào màn hình đăng nhập google.

ii) Bằng cách chọn tài khoản từ tài khoản đã đăng nhập trong điện thoại.

tôi đã sử dụng phương pháp thứ 2 để nhận mã thông báo truy cập và xác thực người dùng.

để có thêm liên kết tham chiếu hỗ trợ được cung cấp bên dưới.

google sign in link 1

Stackoverflow - token refresh

google auth provider documentation

server side token verification docs

nếu mục tiêu duy nhất của bạn là, để nhận mã thông báo, do đó bạn cũng có thể thử this github source.

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) 
    .requestEmail() 
    .build(); 

// sử dụng tùy chọn đăng nhập để tạo bản sao máy khách api.

mGoogleApiClient = new GoogleApiClient.Builder(this) 
    .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */) 
    .addApi(Auth.GOOGLE_SIGN_IN_API, gso) 
    .build(); 
private void signIn() { 
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); 
startActivityForResult(signInIntent,RC_SIGN_IN); } 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
super.onActivityResult(requestCode, resultCode, data); 
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...); 
if (requestCode == RC_SIGN_IN){ 
    GoogleSignInResult result =Auth.GoogleSignInApi.getSignInResultFromIntent(data); 
    handleSignInResult(result); 
    } 


private void handleSignInResult(GoogleSignInResult result) { 
Log.d(TAG, "handleSignInResult:" + result.isSuccess()); 
if (result.isSuccess()) { 
    // Signed in successfully, show authenticated UI. 
    GoogleSignInAccount acct = result.getSignInAccount(); 


} else { 
    // Signed out, show unauthenticated. 

     } 
} 

// được chứng thực

AuthCredential credential = 
GoogleAuthProvider.getCredential(acct.getIdToken(), null); 
FirebaseAuth.getInstance().getCurrentUser().reauthenticate(credential) 
.addOnCompleteListener(new OnCompleteListener<Void>() { 
// add your job here on authenticated 
} 
// if token is obsoleted then you can do this 
credential.refreshToken(); 
accessToken = credential.getAccessToken(); 
Các vấn đề liên quan