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+ }
0 commit comments