Skip to content

Commit 5062c76

Browse files
authored
Add SAML provider snippets. (#246)
* Add SAML provider snippets. This is a Google Cloud Identity Platform provider. * Add OIDC provider snippets. This is a Google Cloud Identity Platform provider. * add generated snippets
1 parent fdc51dd commit 5062c76

22 files changed

+614
-0
lines changed

auth-next/oidc.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// [SNIPPET_REGISTRY disabled]
2+
// [SNIPPETS_SEPARATION enabled]
3+
4+
function oidcProvider() {
5+
// [START auth_oidc_provider_create]
6+
const { OAuthProvider } = require("firebase/auth");
7+
8+
const provider = new OAuthProvider("oidc.myProvider");
9+
// [END auth_oidc_provider_create]
10+
}
11+
12+
function oidcSignInPopup(provider) {
13+
// [START auth_oidc_signin_popup]
14+
const { getAuth, signInWithPopup, OAuthProvider } = require("firebase/auth");
15+
16+
const auth = getAuth();
17+
signInWithPopup(auth, provider)
18+
.then((result) => {
19+
// User is signed in.
20+
const credential = OAuthProvider.credentialFromResult(result);
21+
// This gives you an access token for the OIDC provider. You can use it to directly interact with that provider
22+
}).catch((error) => {
23+
// Handle Errors here.
24+
const errorCode = error.code;
25+
const errorMessage = error.message;
26+
// The email of the user's account used.
27+
const email = error.email;
28+
// The AuthCredential type that was used.
29+
const credential = OAuthProvider.credentialFromError(error);
30+
// Handle / display error.
31+
// ...
32+
});
33+
// [END auth_oidc_signin_popup]
34+
}
35+
36+
function oidcSignInRedirect(provider) {
37+
// [START auth_oidc_signin_redirect]
38+
const { getAuth, signInWithRedirect } = require("firebase/auth");
39+
40+
const auth = getAuth();
41+
signInWithRedirect(auth, provider);
42+
// [END auth_oidc_signin_redirect]
43+
}
44+
45+
function oidcSignInRedirectResult(provider) {
46+
// [START auth_oidc_signin_redirect_result]
47+
const { getAuth, getRedirectResult, OAuthProvider } = require("firebase/auth");
48+
49+
const auth = getAuth();
50+
getRedirectResult(auth)
51+
.then((result) => {
52+
// User is signed in.
53+
const credential = OAuthProvider.credentialFromResult(result);
54+
// This gives you an access token for the OIDC provider. You can use it to directly interact with that provider
55+
})
56+
.catch((error) => {
57+
// Handle Errors here.
58+
const errorCode = error.code;
59+
const errorMessage = error.message;
60+
// The email of the user's account used.
61+
const email = error.email;
62+
// The AuthCredential type that was used.
63+
const credential = OAuthProvider.credentialFromError(error);
64+
// Handle / display error.
65+
// ...
66+
});
67+
// [END auth_oidc_signin_redirect_result]
68+
}
69+
70+
function oidcDirectSignIn(provider, oidcIdToken) {
71+
// [START auth_oidc_direct_sign_in]
72+
const { getAuth, OAuthProvider, signInWithCredential } = require("firebase/auth");
73+
74+
const auth = getAuth();
75+
const credential = provider.credential({
76+
idToken: oidcIdToken,
77+
});
78+
signInWithCredential(auth, credential)
79+
.then((result) => {
80+
// User is signed in.
81+
const newCredential = OAuthProvider.credentialFromResult(result);
82+
// This gives you a new access token for the OIDC provider. You can use it to directly interact with that provider.
83+
})
84+
.catch((error) => {
85+
// Handle Errors here.
86+
const errorCode = error.code;
87+
const errorMessage = error.message;
88+
// The email of the user's account used.
89+
const email = error.email;
90+
// The AuthCredential type that was used.
91+
const credential = OAuthProvider.credentialFromError(error);
92+
// Handle / display error.
93+
// ...
94+
});
95+
// [END auth_oidc_direct_sign_in]
96+
}

auth-next/saml.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// [SNIPPET_REGISTRY disabled]
2+
// [SNIPPETS_SEPARATION enabled]
3+
4+
function samlProvider() {
5+
// [START auth_saml_provider_create]
6+
const { SAMLAuthProvider } = require("firebase/auth");
7+
8+
const provider = new SAMLAuthProvider("saml.myProvider");
9+
// [END auth_saml_provider_create]
10+
}
11+
12+
function samlSignInPopup(provider) {
13+
// [START auth_saml_signin_popup]
14+
const { getAuth, signInWithPopup, SAMLAuthProvider } = require("firebase/auth");
15+
16+
const auth = getAuth();
17+
signInWithPopup(auth, provider)
18+
.then((result) => {
19+
// User is signed in.
20+
// Provider data available from the result.user.getIdToken()
21+
// or from result.user.providerData
22+
}).catch((error) => {
23+
// Handle Errors here.
24+
const errorCode = error.code;
25+
const errorMessage = error.message;
26+
// The email of the user's account used.
27+
const email = error.email;
28+
// The AuthCredential type that was used.
29+
const credential = SAMLAuthProvider.credentialFromError(error);
30+
// Handle / display error.
31+
// ...
32+
});
33+
// [END auth_saml_signin_popup]
34+
}
35+
36+
function samlSignInRedirect(provider) {
37+
// [START auth_saml_signin_redirect]
38+
const { getAuth, signInWithRedirect } = require("firebase/auth");
39+
40+
const auth = getAuth();
41+
signInWithRedirect(auth, provider);
42+
// [END auth_saml_signin_redirect]
43+
}
44+
45+
function samlSignInRedirectResult(provider) {
46+
// [START auth_saml_signin_redirect_result]
47+
const { getAuth, getRedirectResult, SAMLAuthProvider } = require("firebase/auth");
48+
49+
const auth = getAuth();
50+
getRedirectResult(auth)
51+
.then((result) => {
52+
// User is signed in.
53+
// Provider data available from the result.user.getIdToken()
54+
// or from result.user.providerData
55+
})
56+
.catch((error) => {
57+
// Handle Errors here.
58+
const errorCode = error.code;
59+
const errorMessage = error.message;
60+
// The email of the user's account used.
61+
const email = error.email;
62+
// The AuthCredential type that was used.
63+
const credential = SAMLAuthProvider.credentialFromError(error);
64+
// Handle / display error.
65+
// ...
66+
});
67+
// [END auth_saml_signin_redirect_result]
68+
}

auth/oidc.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
// [SNIPPET_REGISTRY disabled]
8+
// [SNIPPETS_SEPARATION enabled]
9+
10+
function oidcProvider() {
11+
// [START auth_oidc_provider_create]
12+
const provider = new firebase.auth.OAuthProvider('oidc.myProvider');
13+
// [END auth_oidc_provider_create]
14+
}
15+
16+
function oidcSignInPopup(provider) {
17+
// [START auth_oidc_signin_popup]
18+
firebase.auth().signInWithPopup(provider)
19+
.then((result) => {
20+
// User is signed in.
21+
// result.credential is a firebase.auth().OAuthCredential object.
22+
// result.credential.providerId is equal to 'oidc.myProvider'.
23+
// result.credential.idToken is the OIDC provider's ID token.
24+
})
25+
.catch((error) => {
26+
// Handle error.
27+
});
28+
// [END auth_oidc_signin_popup]
29+
}
30+
31+
function oidcSignInRedirect(provider) {
32+
// [START auth_oidc_signin_redirect]
33+
firebase.auth().signInWithRedirect(provider).catch((error) => {
34+
// Handle error.
35+
});
36+
// [END auth_oidc_signin_redirect]
37+
}
38+
39+
function oidcSignInRedirectResult(provider) {
40+
// [START auth_oidc_signin_redirect_result]
41+
// On return.
42+
firebase.auth().getRedirectResult()
43+
.then((result) => {
44+
// User is signed in.
45+
// result.credential is a firebase.auth().OAuthCredential object.
46+
// result.credential.providerId is equal to 'oidc.myProvider'.
47+
// result.credential.idToken is the OIDC provider's ID token.
48+
})
49+
.catch((error) => {
50+
// Handle / display error.
51+
// ...
52+
});
53+
// [END auth_oidc_signin_redirect_result]
54+
}
55+
56+
function oidcDirectSignIn(provider, oidcIdToken) {
57+
// [START auth_oidc_direct_sign_in]
58+
const credential = provider.credential(oidcIdToken, null);
59+
60+
firebase.auth().signInWithCredential(credential)
61+
.then((result) => {
62+
// User is signed in.
63+
// User now has a odic.myProvider UserInfo in providerData.
64+
})
65+
.catch((error) => {
66+
// Handle / display error.
67+
// ...
68+
});
69+
// [END auth_oidc_direct_sign_in]
70+
}
71+

auth/saml.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
// [SNIPPET_REGISTRY disabled]
8+
// [SNIPPETS_SEPARATION enabled]
9+
10+
function samlProvider() {
11+
// [START auth_saml_provider_create]
12+
const provider = new firebase.auth.SAMLAuthProvider('saml.myProvider');
13+
// [END auth_saml_provider_create]
14+
}
15+
16+
function samlSignInPopup(provider) {
17+
// [START auth_saml_signin_popup]
18+
firebase.auth().signInWithPopup(provider)
19+
.then((result) => {
20+
// User is signed in.
21+
// Identity provider data available in result.additionalUserInfo.profile,
22+
// or from the user's ID token obtained from result.user.getIdToken()
23+
// as an object in the firebase.sign_in_attributes custom claim
24+
// This is also available from result.user.getIdTokenResult()
25+
// idTokenResult.claims.firebase.sign_in_attributes.
26+
})
27+
.catch((error) => {
28+
// Handle / display error.
29+
// ...
30+
});
31+
// [END auth_saml_signin_popup]
32+
}
33+
34+
function samlSignInRedirect(provider) {
35+
// [START auth_saml_signin_redirect]
36+
firebase.auth().signInWithRedirect(provider);
37+
// [END auth_saml_signin_redirect]
38+
}
39+
40+
function samlSignInRedirectResult(provider) {
41+
// [START auth_saml_signin_redirect_result]
42+
firebase.auth().getRedirectResult()
43+
.then((result) => {
44+
// User is signed in.
45+
// Provider data available in result.additionalUserInfo.profile,
46+
// or from the user's ID token obtained from result.user.getIdToken()
47+
// as an object in the firebase.sign_in_attributes custom claim
48+
// This is also available from result.user.getIdTokenResult()
49+
// idTokenResult.claims.firebase.sign_in_attributes.
50+
}).catch((error) => {
51+
// Handle / display error.
52+
// ...
53+
});
54+
// [END auth_saml_signin_redirect_result]
55+
}
56+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// This snippet file was generated by processing the source file:
2+
// ./auth-next/oidc.js
3+
//
4+
// To update the snippets in this file, edit the source and then run
5+
// 'npm run snippets'.
6+
7+
// [START auth_oidc_direct_sign_in_modular]
8+
import { getAuth, OAuthProvider, signInWithCredential } from "firebase/auth";
9+
10+
const auth = getAuth();
11+
const credential = provider.credential({
12+
idToken: oidcIdToken,
13+
});
14+
signInWithCredential(auth, credential)
15+
.then((result) => {
16+
// User is signed in.
17+
const newCredential = OAuthProvider.credentialFromResult(result);
18+
// This gives you a new access token for the OIDC provider. You can use it to directly interact with that provider.
19+
})
20+
.catch((error) => {
21+
// Handle Errors here.
22+
const errorCode = error.code;
23+
const errorMessage = error.message;
24+
// The email of the user's account used.
25+
const email = error.email;
26+
// The AuthCredential type that was used.
27+
const credential = OAuthProvider.credentialFromError(error);
28+
// Handle / display error.
29+
// ...
30+
});
31+
// [END auth_oidc_direct_sign_in_modular]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// This snippet file was generated by processing the source file:
2+
// ./auth-next/oidc.js
3+
//
4+
// To update the snippets in this file, edit the source and then run
5+
// 'npm run snippets'.
6+
7+
// [START auth_oidc_provider_create_modular]
8+
import { OAuthProvider } from "firebase/auth";
9+
10+
const provider = new OAuthProvider("oidc.myProvider");
11+
// [END auth_oidc_provider_create_modular]
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// This snippet file was generated by processing the source file:
2+
// ./auth-next/oidc.js
3+
//
4+
// To update the snippets in this file, edit the source and then run
5+
// 'npm run snippets'.
6+
7+
// [START auth_oidc_signin_popup_modular]
8+
import { getAuth, signInWithPopup, OAuthProvider } from "firebase/auth";
9+
10+
const auth = getAuth();
11+
signInWithPopup(auth, provider)
12+
.then((result) => {
13+
// User is signed in.
14+
const credential = OAuthProvider.credentialFromResult(result);
15+
// This gives you an access token for the OIDC provider. You can use it to directly interact with that provider
16+
}).catch((error) => {
17+
// Handle Errors here.
18+
const errorCode = error.code;
19+
const errorMessage = error.message;
20+
// The email of the user's account used.
21+
const email = error.email;
22+
// The AuthCredential type that was used.
23+
const credential = OAuthProvider.credentialFromError(error);
24+
// Handle / display error.
25+
// ...
26+
});
27+
// [END auth_oidc_signin_popup_modular]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// This snippet file was generated by processing the source file:
2+
// ./auth-next/oidc.js
3+
//
4+
// To update the snippets in this file, edit the source and then run
5+
// 'npm run snippets'.
6+
7+
// [START auth_oidc_signin_redirect_modular]
8+
import { getAuth, signInWithRedirect } from "firebase/auth";
9+
10+
const auth = getAuth();
11+
signInWithRedirect(auth, provider);
12+
// [END auth_oidc_signin_redirect_modular]

0 commit comments

Comments
 (0)