-
-
Save marty-wang/e541ae15c784e4ce29542ecb17519bb1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createContext (config, onLoginError) { | |
var defaultConfig = { | |
postLogoutRedirectUri: window.location.origin, | |
}; | |
var finalConfig = Object.keys(config).reduce(function(configCopy, curKey) { | |
configCopy[curKey] = config[curKey]; | |
return configCopy; | |
}, defaultConfig); | |
var authContext = new AuthenticationContext(finalConfig); | |
var isCallback = authContext.isCallback(window.location.hash); | |
authContext.handleWindowCallback(); | |
var loginError = authContext.getLoginError(); | |
if (loginError) { | |
onLoginError && onLoginError(loginError); | |
} | |
if (isCallback && !loginError) { | |
window.location = authContext._getItem(authContext.CONSTANTS.STORAGE.LOGIN_REQUEST); | |
} | |
return authContext; | |
} | |
// === use === | |
const config = { | |
// required | |
tenant: '[Enter your tenant here, e.g. contoso.onmicrosoft.com]', | |
clientId: '[Enter your client_id here, e.g. g075edef-0efa-453b-997b-de1337c29185]', | |
// optional | |
endpoints: { | |
// for example | |
"armApiUri": "https://management.azure.com" | |
} | |
} | |
const authContext = createContext(config); | |
// login | |
authContext.config.redirectUri = window.location.href; // optional | |
authContext.login(); | |
// logout | |
authContext.logOut(); | |
// check if user is logged | |
const user = authContext.getCachedUser(); | |
const isUserLoggedIn = !!user; | |
// acquireToken for given uri | |
authContext.acquireToken(uri, function (error, token) { | |
}); | |
// === Web app === | |
1. log in https://manage.windowsazure.com | |
2. Add new app for selected directory | |
3. In the dialog choose "Add application my organization is developing" | |
4. In the next dialog, give an app name and choose "Native client application" | |
5. In the next dialog, give redirect uri "http://localhost:3000", or whatever local dev uri is | |
6. Once the app is created, in the config tab, download the manifest, open it, set oauth2AllowImplicitFlow to true and upload it back. | |
7. Add applications which the web application is allowed to access. | |
8. Copy the client id and tenant name(can be found in the url). They will be used to create authentication context. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment