|
| 1 | +<!DOCTYPE html> |
| 2 | +<!-- |
| 3 | +Copyright (c) 2019 Google Inc. |
| 4 | +
|
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | +
|
| 9 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +--> |
| 17 | +<html> |
| 18 | +<head> |
| 19 | + <meta charset=utf-8 /> |
| 20 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 21 | + <title>Microsoft Sign In Example</title> |
| 22 | + |
| 23 | + <!-- Material Design Theming --> |
| 24 | + <link rel="stylesheet" href="https://code.getmdl.io/1.1.3/material.orange-indigo.min.css"> |
| 25 | + <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons"> |
| 26 | + <script defer src="https://code.getmdl.io/1.1.3/material.min.js"></script> |
| 27 | + |
| 28 | + <link rel="stylesheet" href="main.css"> |
| 29 | + |
| 30 | + <!-- Import and configure the Firebase SDK --> |
| 31 | + <!-- These scripts are made available when the app is served or deployed on Firebase Hosting --> |
| 32 | + <!-- If you do not serve/host your project using Firebase Hosting see https://firebase.google.com/docs/web/setup --> |
| 33 | + <script src="/__/firebase/5.0.0/firebase-app.js"></script> |
| 34 | + <script src="/__/firebase/5.0.0/firebase-auth.js"></script> |
| 35 | + <script src="/__/firebase/init.js"></script> |
| 36 | + |
| 37 | + <script type="text/javascript"> |
| 38 | + |
| 39 | + /** |
| 40 | + * Function called when clicking the Login/Logout button. |
| 41 | + */ |
| 42 | + // [START buttoncallback] |
| 43 | + function toggleSignIn() { |
| 44 | + if (!firebase.auth().currentUser) { |
| 45 | + // [START createprovider] |
| 46 | + var provider = new firebase.auth.OAuthProvider('microsoft.com'); |
| 47 | + // [END createprovider] |
| 48 | + // [START addscopes] |
| 49 | + provider.addScope('User.Read'); |
| 50 | + // [END addscopes] |
| 51 | + // [START signin] |
| 52 | + firebase.auth().signInWithRedirect(provider); |
| 53 | + // [END signin] |
| 54 | + } else { |
| 55 | + // [START signout] |
| 56 | + firebase.auth().signOut(); |
| 57 | + // [END signout] |
| 58 | + } |
| 59 | + // [START_EXCLUDE] |
| 60 | + document.getElementById('quickstart-sign-in').disabled = true; |
| 61 | + // [END_EXCLUDE] |
| 62 | + } |
| 63 | + // [END buttoncallback] |
| 64 | + |
| 65 | + /** |
| 66 | + * initApp handles setting up UI event listeners and registering Firebase auth listeners: |
| 67 | + * - firebase.auth().onAuthStateChanged: This listener is called when the user is signed in or |
| 68 | + * out, and that is where we update the UI. |
| 69 | + * - firebase.auth().getRedirectResult(): This promise completes when the user gets back from |
| 70 | + * the auth redirect flow. It is where you can get the OAuth access token from the IDP. |
| 71 | + */ |
| 72 | + function initApp() { |
| 73 | + // Result from Redirect auth flow. |
| 74 | + // [START getidptoken] |
| 75 | + firebase.auth().getRedirectResult().then(function(result) { |
| 76 | + if (result.credential) { |
| 77 | + // This gives you a Microsoft Access Token. You can use it to access the Microsoft API. |
| 78 | + var token = result.credential.accessToken; |
| 79 | + // [START_EXCLUDE] |
| 80 | + document.getElementById('quickstart-oauthtoken').textContent = token; |
| 81 | + } else { |
| 82 | + document.getElementById('quickstart-oauthtoken').textContent = 'null'; |
| 83 | + // [END_EXCLUDE] |
| 84 | + } |
| 85 | + // The signed-in user info. |
| 86 | + var user = result.user; |
| 87 | + }).catch(function(error) { |
| 88 | + // Handle Errors here. |
| 89 | + var errorCode = error.code; |
| 90 | + var errorMessage = error.message; |
| 91 | + // The email of the user's account used. |
| 92 | + var email = error.email; |
| 93 | + // The firebase.auth.AuthCredential type that was used. |
| 94 | + var credential = error.credential; |
| 95 | + // [START_EXCLUDE] |
| 96 | + if (errorCode === 'auth/account-exists-with-different-credential') { |
| 97 | + alert('You have already signed up with a different auth provider for that email.'); |
| 98 | + // If you are using multiple auth providers on your app you should handle linking |
| 99 | + // the user's accounts here. |
| 100 | + } else { |
| 101 | + console.error(error); |
| 102 | + } |
| 103 | + // [END_EXCLUDE] |
| 104 | + }); |
| 105 | + // [END getidptoken] |
| 106 | + |
| 107 | + // Listening for auth state changes. |
| 108 | + // [START authstatelistener] |
| 109 | + firebase.auth().onAuthStateChanged(function(user) { |
| 110 | + if (user) { |
| 111 | + // User is signed in. Note that unlike other providers supported by Firebase Auth, Microsoft does |
| 112 | + // not provide a profile photo so user.photoURL will be null. However, it can be queried using |
| 113 | + // the Microsoft Graph API: https://docs.microsoft.com/en-us/graph/api/profilephoto-get |
| 114 | + var displayName = user.displayName; |
| 115 | + var email = user.email; |
| 116 | + var emailVerified = user.emailVerified; |
| 117 | + var isAnonymous = user.isAnonymous; |
| 118 | + var uid = user.uid; |
| 119 | + var providerData = user.providerData; |
| 120 | + // [START_EXCLUDE] |
| 121 | + document.getElementById('quickstart-sign-in-status').textContent = 'Signed in'; |
| 122 | + document.getElementById('quickstart-sign-in').textContent = 'Log out'; |
| 123 | + document.getElementById('quickstart-account-details').textContent = JSON.stringify(user, null, ' '); |
| 124 | + // [END_EXCLUDE] |
| 125 | + } else { |
| 126 | + // User is signed out. |
| 127 | + // [START_EXCLUDE] |
| 128 | + document.getElementById('quickstart-sign-in-status').textContent = 'Signed out'; |
| 129 | + document.getElementById('quickstart-sign-in').textContent = 'Log in with Microsoft'; |
| 130 | + document.getElementById('quickstart-account-details').textContent = 'null'; |
| 131 | + document.getElementById('quickstart-oauthtoken').textContent = 'null'; |
| 132 | + // [END_EXCLUDE] |
| 133 | + } |
| 134 | + // [START_EXCLUDE] |
| 135 | + document.getElementById('quickstart-sign-in').disabled = false; |
| 136 | + // [END_EXCLUDE] |
| 137 | + }); |
| 138 | + // [END authstatelistener] |
| 139 | + |
| 140 | + document.getElementById('quickstart-sign-in').addEventListener('click', toggleSignIn, false); |
| 141 | + } |
| 142 | + |
| 143 | + window.onload = function() { |
| 144 | + initApp(); |
| 145 | + }; |
| 146 | + </script> |
| 147 | +</head> |
| 148 | +<body> |
| 149 | +<div class="demo-layout mdl-layout mdl-js-layout mdl-layout--fixed-header"> |
| 150 | + |
| 151 | + <!-- Header section containing title --> |
| 152 | + <header class="mdl-layout__header mdl-color-text--white mdl-color--light-blue-700"> |
| 153 | + <div class="mdl-cell mdl-cell--12-col mdl-cell--12-col-tablet mdl-grid"> |
| 154 | + <div class="mdl-layout__header-row mdl-cell mdl-cell--12-col mdl-cell--12-col-tablet mdl-cell--8-col-desktop"> |
| 155 | + <a href="/"><h3>Firebase Authentication</h3></a> |
| 156 | + </div> |
| 157 | + </div> |
| 158 | + </header> |
| 159 | + |
| 160 | + <main class="mdl-layout__content mdl-color--grey-100"> |
| 161 | + <div class="mdl-cell mdl-cell--12-col mdl-cell--12-col-tablet mdl-grid"> |
| 162 | + |
| 163 | + <!-- Container for the demo --> |
| 164 | + <div class="mdl-card mdl-shadow--2dp mdl-cell mdl-cell--12-col mdl-cell--12-col-tablet mdl-cell--12-col-desktop"> |
| 165 | + <div class="mdl-card__title mdl-color--light-blue-600 mdl-color-text--white"> |
| 166 | + <h2 class="mdl-card__title-text">Microsoft Authentication with Redirect</h2> |
| 167 | + </div> |
| 168 | + <div class="mdl-card__supporting-text mdl-color-text--grey-600"> |
| 169 | + <p>Log in with your Microsoft account below.</p> |
| 170 | + |
| 171 | + <!-- Button that handles sign-in/sign-out --> |
| 172 | + <button disabled class="mdl-button mdl-js-button mdl-button--raised" id="quickstart-sign-in">Log in with Microsoft</button> |
| 173 | + |
| 174 | + <!-- Container where we'll display the user details --> |
| 175 | + <div class="quickstart-user-details-container"> |
| 176 | + Firebase sign-in status: <span id="quickstart-sign-in-status">Unknown</span> |
| 177 | + <div>Firebase auth <code>currentUser</code> object value:</div> |
| 178 | + <pre><code id="quickstart-account-details">null</code></pre> |
| 179 | + <div>Microsoft OAuth Access Token:</div> |
| 180 | + <pre><code id="quickstart-oauthtoken">null</code></pre> |
| 181 | + </div> |
| 182 | + </div> |
| 183 | + </div> |
| 184 | + </div> |
| 185 | + </main> |
| 186 | +</div> |
| 187 | +</body> |
| 188 | +</html> |
0 commit comments