-
Notifications
You must be signed in to change notification settings - Fork 262
Add SAML provider snippets. #246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
07ae800
Add SAML provider snippets.
chronologos 178406b
Add OIDC provider snippets.
chronologos 9716f4d
Fixes based on comments
chronologos 16a8574
lint fix: remove non-top level import
chronologos cf9e205
lint fix 2
chronologos 6699a53
lint fix 3
chronologos 93aca4c
add generated snippets
chronologos 421a532
remove extra snippet
chronologos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.