Skip to content

Commit cfd50b2

Browse files
authored
Move auth snippets from quickstart-js (firebase#57)
1 parent 31690d5 commit cfd50b2

File tree

6 files changed

+182
-4
lines changed

6 files changed

+182
-4
lines changed

auth/anonymous.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// These samples are intended for Web so this import would normally be
2+
// done in HTML however using modules here is more convenient for
3+
// ensuring sample correctness offline.
4+
import firebase from "firebase/app";
5+
import "firebase/auth";
6+
7+
function anonSignIn() {
8+
// [START auth_anon_sign_in]
9+
firebase.auth().signInAnonymously()
10+
.then(() => {
11+
// Signed in..
12+
})
13+
.catch((error) => {
14+
var errorCode = error.code;
15+
var errorMessage = error.message;
16+
// ...
17+
})
18+
// [END auth_anon_sign_in]
19+
}

auth/custom.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// These samples are intended for Web so this import would normally be
2+
// done in HTML however using modules here is more convenient for
3+
// ensuring sample correctness offline.
4+
import firebase from "firebase/app";
5+
import "firebase/auth";
6+
7+
function signInCustom() {
8+
var token = "token123";
9+
// [START auth_sign_in_custom]
10+
firebase.auth().signInWithCustomToken(token)
11+
.then((user) => {
12+
// Signed in
13+
// ...
14+
})
15+
.catch((error) => {
16+
var errorCode = error.code;
17+
var errorMessage = error.message;
18+
// ...
19+
})
20+
// [END auth_sign_in_custom]
21+
}

auth/email-link-auth.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,12 @@ function emailLinkSend(email, actionCodeSettings) {
3535
// Save the email locally so you don't need to ask the user for it again
3636
// if they open the link on the same device.
3737
window.localStorage.setItem('emailForSignIn', email);
38+
// ...
3839
})
3940
.catch((error) => {
40-
// Some error occurred, you can inspect the code: error.code
41+
var errorCode = error.code;
42+
var errorMessage = error.message;
43+
// ...
4144
});
4245
// [END auth_email_link_send]
4346
}

auth/email.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// These samples are intended for Web so this import would normally be
2+
// done in HTML however using modules here is more convenient for
3+
// ensuring sample correctness offline.
4+
import firebase from "firebase/app";
5+
import "firebase/auth";
6+
7+
function signInWithEmailPassword() {
8+
var email = "test@example.com";
9+
var password = "hunter2";
10+
// [START auth_signin_password]
11+
firebase.auth().signInWithEmailAndPassword(email, password)
12+
.then((user) => {
13+
// Signed in
14+
// ...
15+
})
16+
.catch((error) => {
17+
var errorCode = error.code;
18+
var errorMessage = error.message;
19+
});
20+
// [END auth_signin_password]
21+
}
22+
23+
function signUpWithEmailPasswoerd() {
24+
var email = "test@example.com";
25+
var password = "hunter2";
26+
// [START auth_signup_password]
27+
firebase.auth().createUserWithEmailAndPassword(email, password)
28+
.then((user) => {
29+
// Signed in
30+
// ...
31+
})
32+
.catch((error) => {
33+
var errorCode = error.code;
34+
var errorMessage = error.message;
35+
// ..
36+
});
37+
// [END auth_signup_password]
38+
}
39+
40+
function sendEmailVerification() {
41+
// [START auth_send_email_verification]
42+
firebase.auth().currentUser.sendEmailVerification()
43+
.then(() => {
44+
// Email verification sent!
45+
// ...
46+
});
47+
// [END auth_send_email_verification]
48+
}
49+
50+
function sendPasswordReset() {
51+
const email = "sam@example.com";
52+
// [START auth_send_password_reset]
53+
firebase.auth().sendPasswordResetEmail(email)
54+
.then(() => {
55+
// Password reset email sent!
56+
// ..
57+
})
58+
.catch((error) => {
59+
var errorCode = error.code;
60+
var errorMessage = error.message;
61+
// ..
62+
});
63+
// [END auth_send_password_reset]
64+
}

auth/facebook.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// These samples are intended for Web so this import would normally be
2+
// done in HTML however using modules here is more convenient for
3+
// ensuring sample correctness offline.
4+
import firebase from "firebase/app";
5+
import "firebase/auth";
6+
7+
// [START auth_facebook_callback]
8+
function checkLoginState(response) {
9+
if (response.authResponse) {
10+
// User is signed-in Facebook.
11+
var unsubscribe = firebase.auth().onAuthStateChanged((firebaseUser) => {
12+
unsubscribe();
13+
// Check if we are already signed-in Firebase with the correct user.
14+
if (!isUserEqual(response.authResponse, firebaseUser)) {
15+
// Build Firebase credential with the Facebook auth token.
16+
var credential = firebase.auth.FacebookAuthProvider.credential(
17+
response.authResponse.accessToken);
18+
19+
// Sign in with the credential from the Facebook user.
20+
firebase.auth().signInWithCredential(credential).catch((error) => {
21+
// Handle Errors here.
22+
var errorCode = error.code;
23+
var errorMessage = error.message;
24+
// The email of the user's account used.
25+
var email = error.email;
26+
// The firebase.auth.AuthCredential type that was used.
27+
var credential = error.credential;
28+
// ...
29+
});
30+
} else {
31+
// User is already signed-in Firebase with the correct user.
32+
}
33+
});
34+
} else {
35+
// User is signed-out of Facebook.
36+
firebase.auth().signOut();
37+
}
38+
}
39+
// [END auth_facebook_callback]
40+
41+
// [START auth_facebook_checksameuser]
42+
function isUserEqual(facebookAuthResponse, firebaseUser) {
43+
if (firebaseUser) {
44+
var providerData = firebaseUser.providerData;
45+
for (var i = 0; i < providerData.length; i++) {
46+
if (providerData[i].providerId === firebase.auth.FacebookAuthProvider.PROVIDER_ID &&
47+
providerData[i].uid === facebookAuthResponse.userID) {
48+
// We don't need to re-auth the Firebase connection.
49+
return true;
50+
}
51+
}
52+
}
53+
return false;
54+
}
55+
// [END auth_facebook_checksameuser]

auth/index.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import firebase from "firebase/app";
55
import "firebase/auth";
66

7-
// Docs: https://source.corp.google.com/piper///depot/google3/third_party/devsite/firebase/en/docs/auth/_includes/_get_credentials_web.html
7+
// ==========================================================================================
8+
// Docs: Snippets in this file are "general purpose" and are used on more than one docs page
9+
// ==========================================================================================
810

911
function makeGoogleCredential(googleUser) {
1012
// [START auth_make_google_credential]
@@ -26,8 +28,6 @@ function makeEmailCredential(email, password) {
2628
// [END auth_make_email_credential]
2729
}
2830

29-
// Docs: https://source.corp.google.com/piper///depot/google3/third_party/devsite/firebase/en/docs/auth/_includes/_next_steps_web.html
30-
3131
function signOut() {
3232
// [START auth_sign_out]
3333
firebase.auth().signOut().then(() => {
@@ -37,3 +37,19 @@ function signOut() {
3737
});
3838
// [END auth_sign_out]
3939
}
40+
41+
function authStateListener() {
42+
// [START auth_state_listener]
43+
firebase.auth().onAuthStateChanged((user) => {
44+
if (user) {
45+
// User is signed in, see docs for a list of available properties
46+
// https://firebase.google.com/docs/reference/js/firebase.User
47+
var uid = user.uid;
48+
// ...
49+
} else {
50+
// User is signed out
51+
// ...
52+
}
53+
});
54+
// [END auth_state_listener]
55+
}

0 commit comments

Comments
 (0)