[go: nahoru, domu]

Skip to content

Commit

Permalink
Add SAML provider snippets. (firebase#246)
Browse files Browse the repository at this point in the history
* 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
chronologos committed Oct 15, 2021
1 parent fdc51dd commit 5062c76
Show file tree
Hide file tree
Showing 22 changed files with 614 additions and 0 deletions.
96 changes: 96 additions & 0 deletions auth-next/oidc.js
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]
}
68 changes: 68 additions & 0 deletions auth-next/saml.js
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]
}
71 changes: 71 additions & 0 deletions auth/oidc.js
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]
}

56 changes: 56 additions & 0 deletions auth/saml.js
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]
}

31 changes: 31 additions & 0 deletions snippets/auth-next/oidc/auth_oidc_direct_sign_in.js
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]
11 changes: 11 additions & 0 deletions snippets/auth-next/oidc/auth_oidc_provider_create.js
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]
27 changes: 27 additions & 0 deletions snippets/auth-next/oidc/auth_oidc_signin_popup.js
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]
12 changes: 12 additions & 0 deletions snippets/auth-next/oidc/auth_oidc_signin_redirect.js
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]
Loading

0 comments on commit 5062c76

Please sign in to comment.