Skip to content

Commit 58b87db

Browse files
authored
feat(auth): move snippets from quickstart-android (#287)
1 parent 968b6f8 commit 58b87db

14 files changed

+1513
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/**
2+
* Copyright 2021 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.firebase.quickstart.auth;
18+
19+
import android.app.Activity;
20+
import android.os.Bundle;
21+
import android.util.Log;
22+
import android.widget.Toast;
23+
24+
import androidx.annotation.NonNull;
25+
26+
import com.google.android.gms.tasks.OnCompleteListener;
27+
import com.google.android.gms.tasks.Task;
28+
import com.google.firebase.auth.AuthCredential;
29+
import com.google.firebase.auth.AuthResult;
30+
import com.google.firebase.auth.EmailAuthProvider;
31+
import com.google.firebase.auth.FirebaseAuth;
32+
import com.google.firebase.auth.FirebaseUser;
33+
34+
/**
35+
* Activity to demonstrate anonymous login and account linking (with an email/password account).
36+
*/
37+
public class AnonymousAuthActivity extends Activity {
38+
39+
private static final String TAG = "AnonymousAuth";
40+
41+
// [START declare_auth]
42+
private FirebaseAuth mAuth;
43+
// [END declare_auth]
44+
45+
@Override
46+
protected void onCreate(Bundle savedInstanceState) {
47+
super.onCreate(savedInstanceState);
48+
// [START initialize_auth]
49+
// Initialize Firebase Auth
50+
mAuth = FirebaseAuth.getInstance();
51+
// [END initialize_auth]
52+
}
53+
54+
// [START on_start_check_user]
55+
@Override
56+
public void onStart() {
57+
super.onStart();
58+
// Check if user is signed in (non-null) and update UI accordingly.
59+
FirebaseUser currentUser = mAuth.getCurrentUser();
60+
updateUI(currentUser);
61+
}
62+
// [END on_start_check_user]
63+
64+
private void signInAnonymously() {
65+
// [START signin_anonymously]
66+
mAuth.signInAnonymously()
67+
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
68+
@Override
69+
public void onComplete(@NonNull Task<AuthResult> task) {
70+
if (task.isSuccessful()) {
71+
// Sign in success, update UI with the signed-in user's information
72+
Log.d(TAG, "signInAnonymously:success");
73+
FirebaseUser user = mAuth.getCurrentUser();
74+
updateUI(user);
75+
} else {
76+
// If sign in fails, display a message to the user.
77+
Log.w(TAG, "signInAnonymously:failure", task.getException());
78+
Toast.makeText(AnonymousAuthActivity.this, "Authentication failed.",
79+
Toast.LENGTH_SHORT).show();
80+
updateUI(null);
81+
}
82+
}
83+
});
84+
// [END signin_anonymously]
85+
}
86+
87+
private void linkAccount() {
88+
AuthCredential credential = EmailAuthProvider.getCredential("", "");
89+
90+
// [START link_credential]
91+
mAuth.getCurrentUser().linkWithCredential(credential)
92+
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
93+
@Override
94+
public void onComplete(@NonNull Task<AuthResult> task) {
95+
if (task.isSuccessful()) {
96+
Log.d(TAG, "linkWithCredential:success");
97+
FirebaseUser user = task.getResult().getUser();
98+
updateUI(user);
99+
} else {
100+
Log.w(TAG, "linkWithCredential:failure", task.getException());
101+
Toast.makeText(AnonymousAuthActivity.this, "Authentication failed.",
102+
Toast.LENGTH_SHORT).show();
103+
updateUI(null);
104+
}
105+
}
106+
});
107+
// [END link_credential]
108+
}
109+
110+
private void updateUI(FirebaseUser user) {
111+
112+
}
113+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* Copyright Google Inc. All Rights Reserved.
3+
* <p/>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p/>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p/>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.firebase.quickstart.auth;
17+
18+
import android.app.Activity;
19+
import android.os.Bundle;
20+
import android.util.Log;
21+
import android.widget.Toast;
22+
23+
import androidx.annotation.NonNull;
24+
25+
import com.google.android.gms.tasks.OnCompleteListener;
26+
import com.google.android.gms.tasks.Task;
27+
import com.google.firebase.auth.AuthResult;
28+
import com.google.firebase.auth.FirebaseAuth;
29+
import com.google.firebase.auth.FirebaseUser;
30+
31+
/**
32+
* Demonstrate Firebase Authentication using a custom minted token. For more information, see:
33+
* https://firebase.google.com/docs/auth/android/custom-auth
34+
*/
35+
public class CustomAuthActivity extends Activity {
36+
37+
private static final String TAG = "CustomAuthActivity";
38+
private String mCustomToken;
39+
40+
// [START declare_auth]
41+
private FirebaseAuth mAuth;
42+
// [END declare_auth]
43+
44+
@Override
45+
protected void onCreate(Bundle savedInstanceState) {
46+
super.onCreate(savedInstanceState);
47+
// [START initialize_auth]
48+
// Initialize Firebase Auth
49+
mAuth = FirebaseAuth.getInstance();
50+
// [END initialize_auth]
51+
}
52+
53+
// [START on_start_check_user]
54+
@Override
55+
public void onStart() {
56+
super.onStart();
57+
// Check if user is signed in (non-null) and update UI accordingly.
58+
FirebaseUser currentUser = mAuth.getCurrentUser();
59+
updateUI(currentUser);
60+
}
61+
// [END on_start_check_user]
62+
63+
private void startSignIn() {
64+
// [START sign_in_custom]
65+
mAuth.signInWithCustomToken(mCustomToken)
66+
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
67+
@Override
68+
public void onComplete(@NonNull Task<AuthResult> task) {
69+
if (task.isSuccessful()) {
70+
// Sign in success, update UI with the signed-in user's information
71+
Log.d(TAG, "signInWithCustomToken:success");
72+
FirebaseUser user = mAuth.getCurrentUser();
73+
updateUI(user);
74+
} else {
75+
// If sign in fails, display a message to the user.
76+
Log.w(TAG, "signInWithCustomToken:failure", task.getException());
77+
Toast.makeText(CustomAuthActivity.this, "Authentication failed.",
78+
Toast.LENGTH_SHORT).show();
79+
updateUI(null);
80+
}
81+
}
82+
});
83+
// [END sign_in_custom]
84+
}
85+
86+
private void updateUI(FirebaseUser user) {
87+
88+
}
89+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/**
2+
* Copyright 2021 Google Inc. All Rights Reserved.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.firebase.quickstart.auth;
18+
19+
import android.app.Activity;
20+
import android.os.Bundle;
21+
import android.util.Log;
22+
import android.widget.Toast;
23+
24+
import androidx.annotation.NonNull;
25+
26+
import com.google.android.gms.tasks.OnCompleteListener;
27+
import com.google.android.gms.tasks.Task;
28+
import com.google.firebase.auth.AuthResult;
29+
import com.google.firebase.auth.FirebaseAuth;
30+
import com.google.firebase.auth.FirebaseUser;
31+
32+
public class EmailPasswordActivity extends Activity {
33+
34+
private static final String TAG = "EmailPassword";
35+
// [START declare_auth]
36+
private FirebaseAuth mAuth;
37+
// [END declare_auth]
38+
39+
@Override
40+
public void onCreate(Bundle savedInstanceState) {
41+
super.onCreate(savedInstanceState);
42+
// [START initialize_auth]
43+
// Initialize Firebase Auth
44+
mAuth = FirebaseAuth.getInstance();
45+
// [END initialize_auth]
46+
}
47+
48+
// [START on_start_check_user]
49+
@Override
50+
public void onStart() {
51+
super.onStart();
52+
// Check if user is signed in (non-null) and update UI accordingly.
53+
FirebaseUser currentUser = mAuth.getCurrentUser();
54+
if(currentUser != null){
55+
reload();
56+
}
57+
}
58+
// [END on_start_check_user]
59+
60+
private void createAccount(String email, String password) {
61+
// [START create_user_with_email]
62+
mAuth.createUserWithEmailAndPassword(email, password)
63+
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
64+
@Override
65+
public void onComplete(@NonNull Task<AuthResult> task) {
66+
if (task.isSuccessful()) {
67+
// Sign in success, update UI with the signed-in user's information
68+
Log.d(TAG, "createUserWithEmail:success");
69+
FirebaseUser user = mAuth.getCurrentUser();
70+
updateUI(user);
71+
} else {
72+
// If sign in fails, display a message to the user.
73+
Log.w(TAG, "createUserWithEmail:failure", task.getException());
74+
Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
75+
Toast.LENGTH_SHORT).show();
76+
updateUI(null);
77+
}
78+
}
79+
});
80+
// [END create_user_with_email]
81+
}
82+
83+
private void signIn(String email, String password) {
84+
// [START sign_in_with_email]
85+
mAuth.signInWithEmailAndPassword(email, password)
86+
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
87+
@Override
88+
public void onComplete(@NonNull Task<AuthResult> task) {
89+
if (task.isSuccessful()) {
90+
// Sign in success, update UI with the signed-in user's information
91+
Log.d(TAG, "signInWithEmail:success");
92+
FirebaseUser user = mAuth.getCurrentUser();
93+
updateUI(user);
94+
} else {
95+
// If sign in fails, display a message to the user.
96+
Log.w(TAG, "signInWithEmail:failure", task.getException());
97+
Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
98+
Toast.LENGTH_SHORT).show();
99+
updateUI(null);
100+
}
101+
}
102+
});
103+
// [END sign_in_with_email]
104+
}
105+
106+
private void sendEmailVerification() {
107+
// Send verification email
108+
// [START send_email_verification]
109+
final FirebaseUser user = mAuth.getCurrentUser();
110+
user.sendEmailVerification()
111+
.addOnCompleteListener(this, new OnCompleteListener<Void>() {
112+
@Override
113+
public void onComplete(@NonNull Task<Void> task) {
114+
// Email sent
115+
}
116+
});
117+
// [END send_email_verification]
118+
}
119+
120+
private void reload() { }
121+
122+
private void updateUI(FirebaseUser user) {
123+
124+
}
125+
}

0 commit comments

Comments
 (0)