<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Alfresco Auth Router and React &amp;quot;create-react-app&amp;quot; in Alfresco Archive</title>
    <link>https://connect.hyland.com/t5/alfresco-archive/alfresco-auth-router-and-react-quot-create-react-app-quot/m-p/160329#M114305</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;PRE __default_attr="info" __jive_macro_name="alert" alert="info" class="jive_text_macro jive_macro_alert"&gt;&lt;SPAN style="color: #0f7198; background-color: #ffffff;"&gt;This is a personal blog post that is primarily intended for tracking my own learning rather than provided to the Alfresco Community for educational purposes. However if you find it useful, informative or have any comments on it then please comment below.&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;At the end of &lt;A _jive_internal="true" href="https://community.alfresco.com/people/ddraper/blog/2016/12/28/my-first-npm-package" rel="nofollow noopener noreferrer"&gt;my last blog&lt;/A&gt; I'd just published my first &lt;A href="https://www.npmjs.com/package/alfresco-auth-router" rel="nofollow noopener noreferrer"&gt;NPM package&lt;/A&gt;&amp;nbsp;that provided some basic Express middleware configuration for &lt;A href="http://passportjs.org/" rel="nofollow noopener noreferrer"&gt;Passport.j&lt;/A&gt;s and &lt;A href="https://expressjs.com/en/guide/routing.html" rel="nofollow noopener noreferrer"&gt;Express Router&lt;/A&gt;. Having got this working successfully with the &lt;A href="https://github.com/vuejs/vue-cli" rel="nofollow noopener noreferrer"&gt;Vue.js CLI&lt;/A&gt; I decided to see if it could just as easily be applied to a &lt;A href="https://facebook.github.io/react/" rel="nofollow noopener noreferrer"&gt;React&lt;/A&gt; development environment.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Something I frequently &lt;A href="https://medium.com/@paramsingh_66174/angularjs-vs-reactjs-e651a194dfcb#.i98km7hql" rel="nofollow noopener noreferrer"&gt;read&lt;/A&gt; when people are discussing the differences between &lt;A href="https://angular.io/" rel="nofollow noopener noreferrer"&gt;Angular&lt;/A&gt; and React is that the former is a "framework" and the latter is a "library". The suggestion being that Angular comes with everything you need to develop an application whereas React is something that you can plug into any number of different environments. This suggestion is also frequently debunked as there are now well established patterns for building React applications however it was pretty late in providing it's own standard application development template with the "&lt;A href="https://github.com/facebookincubator/create-react-app" rel="nofollow noopener noreferrer"&gt;create-react-app&lt;/A&gt;" project.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I was hoping that this would be just as simple to plug the Alfresco Auth Router into as the Vue CLI but there were a couple of hoops that needed to be jumped through first.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The main issue was that all of the server configuration was part of a separate NPM package called "&lt;A href="https://www.npmjs.com/package/react-scripts" rel="nofollow noopener noreferrer"&gt;react-scripts&lt;/A&gt;" that setup a &lt;A href="https://webpack.github.io/docs/webpack-dev-server.html" rel="nofollow noopener noreferrer"&gt;webpack-dev-server&lt;/A&gt; for serving the web application. It is however possible to "eject" from the standard project to get access to the full server configuration, which can be done by running:&lt;/P&gt;&lt;PRE class="language-none line-numbers"&gt;&lt;CODE&gt;npm run eject‍‍‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;...you then get access to the various scripts and configuration files for setting up the server (however I later discovered that this actually isn't required).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The "scripts/start.js" file provides a "runDevServer" function in which it is possible to add in the required dependencies (after installing them)...&lt;/P&gt;&lt;PRE class="language-none line-numbers"&gt;&lt;CODE&gt;npm install --save-dev passport flash express-session alfresco-auth-router‍‍‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;However the problem I was finding was that all my XHR requests were getting routed back to "/index.html" and reloading the page.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It seems that the approach recommended for handling API calls in an application built with "create-react-app" is to &lt;A href="https://www.fullstackreact.com/articles/using-create-react-app-with-a-server/" rel="nofollow noopener noreferrer"&gt;configure a proxy&lt;/A&gt;&amp;nbsp;to handle the requests (interestingly I found this is also &lt;A href="https://github.com/aurelia/cli/issues/256" rel="nofollow noopener noreferrer"&gt;the approach&lt;/A&gt; taken with the &lt;A href="http://aurelia.io/" rel="nofollow noopener noreferrer"&gt;Aurelia&lt;/A&gt;&amp;nbsp;as both use &lt;A href="https://www.npmjs.com/package/http-proxy-middleware" rel="nofollow noopener noreferrer"&gt;http-proxy-middleware&lt;/A&gt;).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Whereas my Vue.js project had separate pages for login and content the approach taken with "create-react-app" is around Single Page Applications (SPAs). This meant that when authentication failed I was always going back to the same page - and authentication was failing when I was trying to login!&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;With an SPA for React the solution seemed to be to use &lt;A href="https://github.com/reacttraining/react-router" rel="nofollow noopener noreferrer"&gt;react-router&lt;/A&gt; to set up path based routes. An example React authentication flow is provided &lt;A href="https://github.com/ReactTraining/react-router/tree/master/examples/auth-flow" rel="nofollow noopener noreferrer"&gt;here&lt;/A&gt;. In this scenario it would seem that my "alfresco-auth-router" solution is not applicable. Instead the approach to use is to configure a proxy to your Alfresco Repository in the "package.json" file and manage the session token and requests within the application itself.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Working with JSX meant it was necessary to configure Sublime Text with additional syntax handling. The recommended package appears to be "&lt;A href="https://github.com/babel/babel-sublime" rel="nofollow noopener noreferrer"&gt;babel-sublime&lt;/A&gt;".&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I was able to get back REST API calls working and rendering the contents of company home.... the next stage is to break this into re-usable components that can be used across multiple UI frameworks.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 05 Jan 2017 13:20:41 GMT</pubDate>
    <dc:creator>ddraper</dc:creator>
    <dc:date>2017-01-05T13:20:41Z</dc:date>
    <item>
      <title>Alfresco Auth Router and React "create-react-app"</title>
      <link>https://connect.hyland.com/t5/alfresco-archive/alfresco-auth-router-and-react-quot-create-react-app-quot/m-p/160329#M114305</link>
      <description>This is a personal blog post that is primarily intended for tracking my own learning rather than provided to the Alfresco Community for educational purposes. However if you find it useful, informative or have any comments on it then please comment below.At the end of my last blog I'd just published</description>
      <pubDate>Thu, 05 Jan 2017 13:20:41 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/alfresco-archive/alfresco-auth-router-and-react-quot-create-react-app-quot/m-p/160329#M114305</guid>
      <dc:creator>ddraper</dc:creator>
      <dc:date>2017-01-05T13:20:41Z</dc:date>
    </item>
  </channel>
</rss>

