// Path: src/main/java/com/pugnascotia/reactdemo/config/ajax/AjaxLogoutSuccessHandler.java
// @Component
// public class AjaxLogoutSuccessHandler implements LogoutSuccessHandler {
// 
// 	/**
// 	 * On a successful AJAX logout, send a redirect to /api/account. The response body
// 	 * won't contain anything particularly useful, but the response headers will contain
// 	 * the latest session and security (CSRF) tokens, meaning that subsequent POST requests
// 	 * can work.
// 	 */
// 	@Override
// 	public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
// 		response.sendRedirect("/api/account");
// 	}
// }
// 
// Path: src/main/java/com/pugnascotia/reactdemo/utils/State.java
// public final class State {
// 
// 	/** Populates standard parts of the shared client/server model into the Spring {@link Model}.
// 	 * Values prefixed with "__" will be made available to the JavaScript
// 	 * render function only. All other values will be passed in the client's state object.
// 	 */
// 	public static void populateModel(Model model, HttpServletRequest request) {
// 		model.addAttribute("__requestPath", getRequestPath(request));
// 		model.addAttribute("auth", getAuthState(request));
// 	}
// 
// 	/**
// 	 * Returns the request string, including the query fragment, so that it can be made available
// 	 * during server-side react-router rendering.
// 	 */
// 	private static String getRequestPath(HttpServletRequest request) {
// 		String queryString = request.getQueryString();
// 		return request.getRequestURI() + (queryString == null ? "" : "?" + queryString);
// 	}
// 
// 	/**
// 	 * Returns a representation of the user's authentication state, in the shape expected by the client.
// 	 */
// 	public static Map<String, Object> getAuthState(HttpServletRequest request) {
// 		Optional<List<String>> optionalRoles = getRoles(request);
// 
// 		return optionalRoles.map(roles -> {
// 			Map<String, Object> authState = new HashMap<>();
// 			authState.put("signedIn", !roles.contains("ROLE_ANONYMOUS"));
// 			authState.put("roles", roles);
// 
// 			return authState;
// 		})
// 		.orElseGet(() -> {
// 			Map<String, Object> authState = new HashMap<>();
// 			authState.put("signedIn", false);
// 			authState.put("roles", Collections.singletonList("ROLE_ANONYMOUS"));
// 
// 			return authState;
// 		});
// 	}
// 
// 	/**
// 	 * Return a list of the current user's roles. If they are not authenticated then they will
// 	 * have "ROLE_ANONYMOUS".
// 	 */
// 	private static Optional<List<String>> getRoles(HttpServletRequest request) {
// 		return getAuthentication(request)
// 			.map(a -> Functions.map(a.getAuthorities(), GrantedAuthority::getAuthority));
// 	}
// 
// 	/**
// 	 * Getting the current authentication object ought to be easy, by getting a context with
// 	 * {@link SecurityContextHolder#getContext()}, then calling {@link SecurityContext#getAuthentication()}.
// 	 * However there are circumstances where that doesn't work reliably, such as when handling
// 	 * exceptions. This method makes several attempts to get an {@link Authentication} instance.
// 	 */
// 	private static Optional<Authentication> getAuthentication(HttpServletRequest request) {
// 		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
// 
// 		if (authentication == null) {
// 			RequestAttributes requestAttributes = new ServletRequestAttributes(request);
// 			SecurityContext securityContext = (SecurityContext) requestAttributes.getAttribute("SPRING_SECURITY_CONTEXT", RequestAttributes.SCOPE_SESSION);
// 			if (securityContext == null) {
// 				securityContext = (SecurityContext) requestAttributes.getAttribute("SPRING_SECURITY_CONTEXT", RequestAttributes.SCOPE_GLOBAL_SESSION);
// 			}
// 			if (securityContext != null) {
// 				authentication = securityContext.getAuthentication();
// 			}
// 		}
// 
// 		return Optional.ofNullable(authentication);
// 	}
// }


import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import com.pugnascotia.reactdemo.config.ajax.AjaxLogoutSuccessHandler;
import com.pugnascotia.reactdemo.utils.State;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
package com.pugnascotia.reactdemo.account;





/**
 * Returns the user's current authentication status.
 *
 * @see AjaxLogoutSuccessHandler for how this is used.
 */

@RestController
@RequestMapping(value = "/api", produces = APPLICATION_JSON_VALUE)
public class AccountResource {

	@RequestMapping("/account")
	public Map<String,Object> getAccountStatus(HttpServletRequest request) {
// Path: src/main/java/com/pugnascotia/reactdemo/config/ajax/AjaxLogoutSuccessHandler.java
// @Component
// public class AjaxLogoutSuccessHandler implements LogoutSuccessHandler {
// 
// 	/**
// 	 * On a successful AJAX logout, send a redirect to /api/account. The response body
// 	 * won't contain anything particularly useful, but the response headers will contain
// 	 * the latest session and security (CSRF) tokens, meaning that subsequent POST requests
// 	 * can work.
// 	 */
// 	@Override
// 	public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
// 		response.sendRedirect("/api/account");
// 	}
// }
// 
// Path: src/main/java/com/pugnascotia/reactdemo/utils/State.java
// public final class State {
// 
// 	/** Populates standard parts of the shared client/server model into the Spring {@link Model}.
// 	 * Values prefixed with "__" will be made available to the JavaScript
// 	 * render function only. All other values will be passed in the client's state object.
// 	 */
// 	public static void populateModel(Model model, HttpServletRequest request) {
// 		model.addAttribute("__requestPath", getRequestPath(request));
// 		model.addAttribute("auth", getAuthState(request));
// 	}
// 
// 	/**
// 	 * Returns the request string, including the query fragment, so that it can be made available
// 	 * during server-side react-router rendering.
// 	 */
// 	private static String getRequestPath(HttpServletRequest request) {
// 		String queryString = request.getQueryString();
// 		return request.getRequestURI() + (queryString == null ? "" : "?" + queryString);
// 	}
// 
// 	/**
// 	 * Returns a representation of the user's authentication state, in the shape expected by the client.
// 	 */
// 	public static Map<String, Object> getAuthState(HttpServletRequest request) {
// 		Optional<List<String>> optionalRoles = getRoles(request);
// 
// 		return optionalRoles.map(roles -> {
// 			Map<String, Object> authState = new HashMap<>();
// 			authState.put("signedIn", !roles.contains("ROLE_ANONYMOUS"));
// 			authState.put("roles", roles);
// 
// 			return authState;
// 		})
// 		.orElseGet(() -> {
// 			Map<String, Object> authState = new HashMap<>();
// 			authState.put("signedIn", false);
// 			authState.put("roles", Collections.singletonList("ROLE_ANONYMOUS"));
// 
// 			return authState;
// 		});
// 	}
// 
// 	/**
// 	 * Return a list of the current user's roles. If they are not authenticated then they will
// 	 * have "ROLE_ANONYMOUS".
// 	 */
// 	private static Optional<List<String>> getRoles(HttpServletRequest request) {
// 		return getAuthentication(request)
// 			.map(a -> Functions.map(a.getAuthorities(), GrantedAuthority::getAuthority));
// 	}
// 
// 	/**
// 	 * Getting the current authentication object ought to be easy, by getting a context with
// 	 * {@link SecurityContextHolder#getContext()}, then calling {@link SecurityContext#getAuthentication()}.
// 	 * However there are circumstances where that doesn't work reliably, such as when handling
// 	 * exceptions. This method makes several attempts to get an {@link Authentication} instance.
// 	 */
// 	private static Optional<Authentication> getAuthentication(HttpServletRequest request) {
// 		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
// 
// 		if (authentication == null) {
// 			RequestAttributes requestAttributes = new ServletRequestAttributes(request);
// 			SecurityContext securityContext = (SecurityContext) requestAttributes.getAttribute("SPRING_SECURITY_CONTEXT", RequestAttributes.SCOPE_SESSION);
// 			if (securityContext == null) {
// 				securityContext = (SecurityContext) requestAttributes.getAttribute("SPRING_SECURITY_CONTEXT", RequestAttributes.SCOPE_GLOBAL_SESSION);
// 			}
// 			if (securityContext != null) {
// 				authentication = securityContext.getAuthentication();
// 			}
// 		}
// 
// 		return Optional.ofNullable(authentication);
// 	}
// }


// Path: src/main/java/com/pugnascotia/reactdemo/account/AccountResource.java
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import com.pugnascotia.reactdemo.config.ajax.AjaxLogoutSuccessHandler;
import com.pugnascotia.reactdemo.utils.State;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

package com.pugnascotia.reactdemo.account;





/**
 * Returns the user's current authentication status.
 *
 * @see AjaxLogoutSuccessHandler for how this is used.
 */

@RestController
@RequestMapping(value = "/api", produces = APPLICATION_JSON_VALUE)
public class AccountResource {

	@RequestMapping("/account")
	public Map<String,Object> getAccountStatus(HttpServletRequest request) {
		return State.getAuthState(request);