Adal.js Example

by dshilkret

HTML

<!DOCTYPE html>
<html>
	<head>
		<title>Authenticate an Office 365 user with ADAL JS</title>
		<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
		<style>body{font:normal normal normal 14px/1.5em "Century Gothic", sans-serif;}</style>
		<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
		<script src="https://secure.aadcdn.microsoftonline-p.com/lib/1.0.0/js/adal.js"></script>
		<script type="text/javascript">
			$(document).ready(function() {
				"use strict";
				
				// Assign variables
				var variables = {
					// Domain of Azure AD tenant
					azureAD: "dtecho365.onmicrosoft.com",
					// ClientId of Azure AD application principal
					clientId: "9624a483-5297-45f7-91da-a8ec0e75798b",
					// GUID of SharePoint list
					listId: "729c8285-506d-409d-8ca6-023a69795b88",
					// Name of SharePoint tenant
					sharePointTenant: "dtecho365"
				}
				
				// Create config and get AuthenticationContext
				window.config = {
					tenant: variables.azureAD,
					clientId: variables.clientId,
					postLogoutRedirectUri: window.location.origin,
					endpoints: {
						graphApiUri: "https://graph.microsoft.com",
						sharePointUri: "https://" + variables.sharePointTenant + ".sharepoint.com",
					},
					cacheLocation: "localStorage"
				};
				var authContext = new AuthenticationContext(config);
				
				var isCallback = authContext.isCallback(window.location.hash);
				authContext.handleWindowCallback();
				
				if (isCallback && !authContext.getLoginError()) {
					window.location = authContext._getItem(authContext.CONSTANTS.STORAGE.LOGIN_REQUEST);
				}
				
				var user = authContext.getCachedUser();
				if (!user) {
					authContext.login();
				}
				
				// Get OneDrive documents of current user with AuthenticationContext of Graph API 
				authContext.acquireToken(config.endpoints.graphApiUri, function (error, token) {
					if (error || !token) {
						console.log("ADAL error occurred: " +...