-
Notifications
You must be signed in to change notification settings - Fork 253
/
Copy pathmicrosoft-oauth.js
134 lines (113 loc) · 4.32 KB
/
microsoft-oauth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// [SNIPPET_REGISTRY disabled]
// [SNIPPETS_SEPARATION enabled]
// Docs: https://source.corp.google.com/piper///depot/google3/third_party/devsite/firebase/en/docs/auth/web/microsoft-oauth.md
function msftCreateProvider() {
// [START auth_msft_create_provider]
const { OAuthProvider } = require("firebase/auth");
const provider = new OAuthProvider('microsoft.com');
// [END auth_msft_create_provider]
// [START auth_msft_provider_scopes]
provider.addScope('mail.read');
provider.addScope('calendars.read');
// [END auth_msft_provider_scopes]
// [START auth_msft_provider_params]
provider.setCustomParameters({
// Force re-consent.
prompt: 'consent',
// Target specific email with login hint.
login_hint: '[email protected]'
});
// [END auth_msft_provider_params]
// [START auth_msft_provider_params_tenant]
provider.setCustomParameters({
// Optional "tenant" parameter in case you are using an Azure AD tenant.
// eg. '8eaef023-2b34-4da1-9baa-8bc8c9d6a490' or 'contoso.onmicrosoft.com'
// or "common" for tenant-independent tokens.
// The default value is "common".
tenant: 'TENANT_ID'
});
// [END auth_msft_provider_params_tenant]
}
function msftSignInPopup(provider) {
// [START auth_msft_signin_popup]
const { getAuth, signInWithPopup, OAuthProvider } = require("firebase/auth");
const auth = getAuth();
signInWithPopup(auth, provider)
.then((result) => {
// User is signed in.
// IdP data available in result.additionalUserInfo.profile.
// Get the OAuth access token and ID Token
const credential = OAuthProvider.credentialFromResult(result);
const accessToken = credential.accessToken;
const idToken = credential.idToken;
})
.catch((error) => {
// Handle error.
});
// [END auth_msft_signin_popup]
}
function msftSignInRedirect(provider) {
// [START auth_msft_signin_redirect]
const { getAuth, signInWithRedirect } = require("firebase/auth");
const auth = getAuth();
signInWithRedirect(auth, provider);
// [END auth_msft_signin_redirect]
}
function msftSignInRedirectResult() {
// [START auth_msft_signin_redirect_result]
const { getAuth, getRedirectResult, OAuthProvider } = require("firebase/auth");
const auth = getAuth();
getRedirectResult(auth)
.then((result) => {
// User is signed in.
// IdP data available in result.additionalUserInfo.profile.
// Get the OAuth access token and ID Token
const credential = OAuthProvider.credentialFromResult(result);
const accessToken = credential.accessToken;
const idToken = credential.idToken;
})
.catch((error) => {
// Handle error.
});
// [END auth_msft_signin_redirect_result]
}
function msftLinkWithPopup() {
// [START auth_msft_link_popup]
const { getAuth, linkWithPopup, OAuthProvider } = require("firebase/auth");
const provider = new OAuthProvider('microsoft.com');
const auth = getAuth();
linkWithPopup(auth.currentUser, provider)
.then((result) => {
// Microsoft credential is linked to the current user.
// IdP data available in result.additionalUserInfo.profile.
// Get the OAuth access token and ID Token
const credential = OAuthProvider.credentialFromResult(result);
const accessToken = credential.accessToken;
const idToken = credential.idToken;
})
.catch((error) => {
// Handle error.
});
// [END auth_msft_link_popup]
}
function msftReauthPopup() {
// [START auth_msft_reauth_popup]
const { getAuth, reauthenticateWithPopup, OAuthProvider } = require("firebase/auth");
const provider = new OAuthProvider('microsoft.com');
const auth = getAuth();
reauthenticateWithPopup(auth.currentUser, provider)
.then((result) => {
// User is re-authenticated with fresh tokens minted and
// should be able to perform sensitive operations like account
// deletion and email or password update.
// IdP data available in result.additionalUserInfo.profile.
// Get the OAuth access token and ID Token
const credential = OAuthProvider.credentialFromResult(result);
const accessToken = credential.accessToken;
const idToken = credential.idToken;
})
.catch((error) => {
// Handle error.
});
// [END auth_msft_reauth_popup]
}