forked from firebase/snippets-web
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SAML provider snippets. (firebase#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
- Loading branch information
1 parent
fdc51dd
commit 5062c76
Showing
22 changed files
with
614 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// [SNIPPET_REGISTRY disabled] | ||
// [SNIPPETS_SEPARATION enabled] | ||
|
||
function oidcProvider() { | ||
// [START auth_oidc_provider_create] | ||
const { OAuthProvider } = require("firebase/auth"); | ||
|
||
const provider = new OAuthProvider("oidc.myProvider"); | ||
// [END auth_oidc_provider_create] | ||
} | ||
|
||
function oidcSignInPopup(provider) { | ||
// [START auth_oidc_signin_popup] | ||
const { getAuth, signInWithPopup, OAuthProvider } = require("firebase/auth"); | ||
|
||
const auth = getAuth(); | ||
signInWithPopup(auth, provider) | ||
.then((result) => { | ||
// User is signed in. | ||
const credential = OAuthProvider.credentialFromResult(result); | ||
// This gives you an access token for the OIDC provider. You can use it to directly interact with that provider | ||
}).catch((error) => { | ||
// Handle Errors here. | ||
const errorCode = error.code; | ||
const errorMessage = error.message; | ||
// The email of the user's account used. | ||
const email = error.email; | ||
// The AuthCredential type that was used. | ||
const credential = OAuthProvider.credentialFromError(error); | ||
// Handle / display error. | ||
// ... | ||
}); | ||
// [END auth_oidc_signin_popup] | ||
} | ||
|
||
function oidcSignInRedirect(provider) { | ||
// [START auth_oidc_signin_redirect] | ||
const { getAuth, signInWithRedirect } = require("firebase/auth"); | ||
|
||
const auth = getAuth(); | ||
signInWithRedirect(auth, provider); | ||
// [END auth_oidc_signin_redirect] | ||
} | ||
|
||
function oidcSignInRedirectResult(provider) { | ||
// [START auth_oidc_signin_redirect_result] | ||
const { getAuth, getRedirectResult, OAuthProvider } = require("firebase/auth"); | ||
|
||
const auth = getAuth(); | ||
getRedirectResult(auth) | ||
.then((result) => { | ||
// User is signed in. | ||
const credential = OAuthProvider.credentialFromResult(result); | ||
// This gives you an access token for the OIDC provider. You can use it to directly interact with that provider | ||
}) | ||
.catch((error) => { | ||
// Handle Errors here. | ||
const errorCode = error.code; | ||
const errorMessage = error.message; | ||
// The email of the user's account used. | ||
const email = error.email; | ||
// The AuthCredential type that was used. | ||
const credential = OAuthProvider.credentialFromError(error); | ||
// Handle / display error. | ||
// ... | ||
}); | ||
// [END auth_oidc_signin_redirect_result] | ||
} | ||
|
||
function oidcDirectSignIn(provider, oidcIdToken) { | ||
// [START auth_oidc_direct_sign_in] | ||
const { getAuth, OAuthProvider, signInWithCredential } = require("firebase/auth"); | ||
|
||
const auth = getAuth(); | ||
const credential = provider.credential({ | ||
idToken: oidcIdToken, | ||
}); | ||
signInWithCredential(auth, credential) | ||
.then((result) => { | ||
// User is signed in. | ||
const newCredential = OAuthProvider.credentialFromResult(result); | ||
// This gives you a new access token for the OIDC provider. You can use it to directly interact with that provider. | ||
}) | ||
.catch((error) => { | ||
// Handle Errors here. | ||
const errorCode = error.code; | ||
const errorMessage = error.message; | ||
// The email of the user's account used. | ||
const email = error.email; | ||
// The AuthCredential type that was used. | ||
const credential = OAuthProvider.credentialFromError(error); | ||
// Handle / display error. | ||
// ... | ||
}); | ||
// [END auth_oidc_direct_sign_in] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// [SNIPPET_REGISTRY disabled] | ||
// [SNIPPETS_SEPARATION enabled] | ||
|
||
function samlProvider() { | ||
// [START auth_saml_provider_create] | ||
const { SAMLAuthProvider } = require("firebase/auth"); | ||
|
||
const provider = new SAMLAuthProvider("saml.myProvider"); | ||
// [END auth_saml_provider_create] | ||
} | ||
|
||
function samlSignInPopup(provider) { | ||
// [START auth_saml_signin_popup] | ||
const { getAuth, signInWithPopup, SAMLAuthProvider } = require("firebase/auth"); | ||
|
||
const auth = getAuth(); | ||
signInWithPopup(auth, provider) | ||
.then((result) => { | ||
// User is signed in. | ||
// Provider data available from the result.user.getIdToken() | ||
// or from result.user.providerData | ||
}).catch((error) => { | ||
// Handle Errors here. | ||
const errorCode = error.code; | ||
const errorMessage = error.message; | ||
// The email of the user's account used. | ||
const email = error.email; | ||
// The AuthCredential type that was used. | ||
const credential = SAMLAuthProvider.credentialFromError(error); | ||
// Handle / display error. | ||
// ... | ||
}); | ||
// [END auth_saml_signin_popup] | ||
} | ||
|
||
function samlSignInRedirect(provider) { | ||
// [START auth_saml_signin_redirect] | ||
const { getAuth, signInWithRedirect } = require("firebase/auth"); | ||
|
||
const auth = getAuth(); | ||
signInWithRedirect(auth, provider); | ||
// [END auth_saml_signin_redirect] | ||
} | ||
|
||
function samlSignInRedirectResult(provider) { | ||
// [START auth_saml_signin_redirect_result] | ||
const { getAuth, getRedirectResult, SAMLAuthProvider } = require("firebase/auth"); | ||
|
||
const auth = getAuth(); | ||
getRedirectResult(auth) | ||
.then((result) => { | ||
// User is signed in. | ||
// Provider data available from the result.user.getIdToken() | ||
// or from result.user.providerData | ||
}) | ||
.catch((error) => { | ||
// Handle Errors here. | ||
const errorCode = error.code; | ||
const errorMessage = error.message; | ||
// The email of the user's account used. | ||
const email = error.email; | ||
// The AuthCredential type that was used. | ||
const credential = SAMLAuthProvider.credentialFromError(error); | ||
// Handle / display error. | ||
// ... | ||
}); | ||
// [END auth_saml_signin_redirect_result] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// These samples are intended for Web so this import would normally be | ||
// done in HTML however using modules here is more convenient for | ||
// ensuring sample correctness offline. | ||
import firebase from "firebase/app"; | ||
import "firebase/auth"; | ||
|
||
// [SNIPPET_REGISTRY disabled] | ||
// [SNIPPETS_SEPARATION enabled] | ||
|
||
function oidcProvider() { | ||
// [START auth_oidc_provider_create] | ||
const provider = new firebase.auth.OAuthProvider('oidc.myProvider'); | ||
// [END auth_oidc_provider_create] | ||
} | ||
|
||
function oidcSignInPopup(provider) { | ||
// [START auth_oidc_signin_popup] | ||
firebase.auth().signInWithPopup(provider) | ||
.then((result) => { | ||
// User is signed in. | ||
// result.credential is a firebase.auth().OAuthCredential object. | ||
// result.credential.providerId is equal to 'oidc.myProvider'. | ||
// result.credential.idToken is the OIDC provider's ID token. | ||
}) | ||
.catch((error) => { | ||
// Handle error. | ||
}); | ||
// [END auth_oidc_signin_popup] | ||
} | ||
|
||
function oidcSignInRedirect(provider) { | ||
// [START auth_oidc_signin_redirect] | ||
firebase.auth().signInWithRedirect(provider).catch((error) => { | ||
// Handle error. | ||
}); | ||
// [END auth_oidc_signin_redirect] | ||
} | ||
|
||
function oidcSignInRedirectResult(provider) { | ||
// [START auth_oidc_signin_redirect_result] | ||
// On return. | ||
firebase.auth().getRedirectResult() | ||
.then((result) => { | ||
// User is signed in. | ||
// result.credential is a firebase.auth().OAuthCredential object. | ||
// result.credential.providerId is equal to 'oidc.myProvider'. | ||
// result.credential.idToken is the OIDC provider's ID token. | ||
}) | ||
.catch((error) => { | ||
// Handle / display error. | ||
// ... | ||
}); | ||
// [END auth_oidc_signin_redirect_result] | ||
} | ||
|
||
function oidcDirectSignIn(provider, oidcIdToken) { | ||
// [START auth_oidc_direct_sign_in] | ||
const credential = provider.credential(oidcIdToken, null); | ||
|
||
firebase.auth().signInWithCredential(credential) | ||
.then((result) => { | ||
// User is signed in. | ||
// User now has a odic.myProvider UserInfo in providerData. | ||
}) | ||
.catch((error) => { | ||
// Handle / display error. | ||
// ... | ||
}); | ||
// [END auth_oidc_direct_sign_in] | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// These samples are intended for Web so this import would normally be | ||
// done in HTML however using modules here is more convenient for | ||
// ensuring sample correctness offline. | ||
import firebase from "firebase/app"; | ||
import "firebase/auth"; | ||
|
||
// [SNIPPET_REGISTRY disabled] | ||
// [SNIPPETS_SEPARATION enabled] | ||
|
||
function samlProvider() { | ||
// [START auth_saml_provider_create] | ||
const provider = new firebase.auth.SAMLAuthProvider('saml.myProvider'); | ||
// [END auth_saml_provider_create] | ||
} | ||
|
||
function samlSignInPopup(provider) { | ||
// [START auth_saml_signin_popup] | ||
firebase.auth().signInWithPopup(provider) | ||
.then((result) => { | ||
// User is signed in. | ||
// Identity provider data available in result.additionalUserInfo.profile, | ||
// or from the user's ID token obtained from result.user.getIdToken() | ||
// as an object in the firebase.sign_in_attributes custom claim | ||
// This is also available from result.user.getIdTokenResult() | ||
// idTokenResult.claims.firebase.sign_in_attributes. | ||
}) | ||
.catch((error) => { | ||
// Handle / display error. | ||
// ... | ||
}); | ||
// [END auth_saml_signin_popup] | ||
} | ||
|
||
function samlSignInRedirect(provider) { | ||
// [START auth_saml_signin_redirect] | ||
firebase.auth().signInWithRedirect(provider); | ||
// [END auth_saml_signin_redirect] | ||
} | ||
|
||
function samlSignInRedirectResult(provider) { | ||
// [START auth_saml_signin_redirect_result] | ||
firebase.auth().getRedirectResult() | ||
.then((result) => { | ||
// User is signed in. | ||
// Provider data available in result.additionalUserInfo.profile, | ||
// or from the user's ID token obtained from result.user.getIdToken() | ||
// as an object in the firebase.sign_in_attributes custom claim | ||
// This is also available from result.user.getIdTokenResult() | ||
// idTokenResult.claims.firebase.sign_in_attributes. | ||
}).catch((error) => { | ||
// Handle / display error. | ||
// ... | ||
}); | ||
// [END auth_saml_signin_redirect_result] | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// This snippet file was generated by processing the source file: | ||
// ./auth-next/oidc.js | ||
// | ||
// To update the snippets in this file, edit the source and then run | ||
// 'npm run snippets'. | ||
|
||
// [START auth_oidc_direct_sign_in_modular] | ||
import { getAuth, OAuthProvider, signInWithCredential } from "firebase/auth"; | ||
|
||
const auth = getAuth(); | ||
const credential = provider.credential({ | ||
idToken: oidcIdToken, | ||
}); | ||
signInWithCredential(auth, credential) | ||
.then((result) => { | ||
// User is signed in. | ||
const newCredential = OAuthProvider.credentialFromResult(result); | ||
// This gives you a new access token for the OIDC provider. You can use it to directly interact with that provider. | ||
}) | ||
.catch((error) => { | ||
// Handle Errors here. | ||
const errorCode = error.code; | ||
const errorMessage = error.message; | ||
// The email of the user's account used. | ||
const email = error.email; | ||
// The AuthCredential type that was used. | ||
const credential = OAuthProvider.credentialFromError(error); | ||
// Handle / display error. | ||
// ... | ||
}); | ||
// [END auth_oidc_direct_sign_in_modular] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// This snippet file was generated by processing the source file: | ||
// ./auth-next/oidc.js | ||
// | ||
// To update the snippets in this file, edit the source and then run | ||
// 'npm run snippets'. | ||
|
||
// [START auth_oidc_provider_create_modular] | ||
import { OAuthProvider } from "firebase/auth"; | ||
|
||
const provider = new OAuthProvider("oidc.myProvider"); | ||
// [END auth_oidc_provider_create_modular] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// This snippet file was generated by processing the source file: | ||
// ./auth-next/oidc.js | ||
// | ||
// To update the snippets in this file, edit the source and then run | ||
// 'npm run snippets'. | ||
|
||
// [START auth_oidc_signin_popup_modular] | ||
import { getAuth, signInWithPopup, OAuthProvider } from "firebase/auth"; | ||
|
||
const auth = getAuth(); | ||
signInWithPopup(auth, provider) | ||
.then((result) => { | ||
// User is signed in. | ||
const credential = OAuthProvider.credentialFromResult(result); | ||
// This gives you an access token for the OIDC provider. You can use it to directly interact with that provider | ||
}).catch((error) => { | ||
// Handle Errors here. | ||
const errorCode = error.code; | ||
const errorMessage = error.message; | ||
// The email of the user's account used. | ||
const email = error.email; | ||
// The AuthCredential type that was used. | ||
const credential = OAuthProvider.credentialFromError(error); | ||
// Handle / display error. | ||
// ... | ||
}); | ||
// [END auth_oidc_signin_popup_modular] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// This snippet file was generated by processing the source file: | ||
// ./auth-next/oidc.js | ||
// | ||
// To update the snippets in this file, edit the source and then run | ||
// 'npm run snippets'. | ||
|
||
// [START auth_oidc_signin_redirect_modular] | ||
import { getAuth, signInWithRedirect } from "firebase/auth"; | ||
|
||
const auth = getAuth(); | ||
signInWithRedirect(auth, provider); | ||
// [END auth_oidc_signin_redirect_modular] |
Oops, something went wrong.