Skip to content

Commit 178406b

Browse files
committed
Add OIDC provider snippets.
This is a Google Cloud Identity Platform provider.
1 parent 07ae800 commit 178406b

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed

auth-next/oidc.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
}).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+
// ...
29+
});
30+
// [END auth_oidc_signin_popup]
31+
}
32+
33+
function oidcSignInRedirect(provider) {
34+
// [START auth_oidc_signin_redirect]
35+
const { getAuth, signInWithRedirect } = require("firebase/auth");
36+
37+
const auth = getAuth();
38+
signInWithRedirect(auth, provider);
39+
// [END auth_oidc_signin_redirect]
40+
}
41+
42+
function oidcSignInRedirectResult(provider) {
43+
// [START auth_oidc_signin_redirect_result]
44+
import { getAuth, getRedirectResult, OAuthProvider } from "firebase/auth";
45+
46+
const auth = getAuth();
47+
getRedirectResult(auth)
48+
.then((result) => {
49+
credential = OAuthProvider.credentialFromResult(result);
50+
// ...
51+
})
52+
.catch((error) => {
53+
// Handle Errors here.
54+
const errorCode = error.code;
55+
const errorMessage = error.message;
56+
// The email of the user's account used.
57+
const email = error.email;
58+
// The AuthCredential type that was used.
59+
const credential = OAuthProvider.credentialFromError(error);
60+
console.log(error);
61+
console.log(credential);
62+
// ...
63+
});
64+
// [END auth_oidc_signin_redirect_result]
65+
}
66+
67+
function oidcDirectSignIn(provider, oidcIdToken) {
68+
// [START auth_oidc_direct_sign_in]
69+
import { getAuth, OAuthProvider } from "firebase/auth";
70+
71+
const auth = getAuth();
72+
const credential = provider.credential({
73+
idToken: oidcIdToken,
74+
})
75+
signInWithCredential(auth, credential)
76+
.then((result) => {
77+
credential = OAuthProvider.credentialFromResult(result);
78+
// ...
79+
})
80+
.catch((error) => {
81+
// Handle Errors here.
82+
const errorCode = error.code;
83+
const errorMessage = error.message;
84+
// The email of the user's account used.
85+
const email = error.email;
86+
// The AuthCredential type that was used.
87+
const credential = OAuthProvider.credentialFromError(error);
88+
console.log(error);
89+
console.log(credential);
90+
// ...
91+
});
92+
// [END auth_oidc_direct_sign_in]
93+
}

auth/oidc.js

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

0 commit comments

Comments
 (0)