clemens-tolboom / drupal-8-rest-angularjs

Test angularjs frontend for Drupal 8 ReST
MIT License
48 stars 16 forks source link

Questions and problems regarding this demo #24

Open nicholas12345 opened 9 years ago

nicholas12345 commented 9 years ago

Hi, first let me thank you for this demonstration. It is very useful to understand and try out communication and possibilities that Drupal 8 and nodejs give.

Now, I stumbled across a few problems while testing out this demo. First, logging in doesn't seem to work? I do not see any kind of POST or anything sent towards headless Drupal 8 when trying to log in. Second, since I cannot login, I also cannot update nodes from lifecycle or anything. What I can do is to give anonymous all needed permissions to view, create and delete nodes for example and that seems to work, but everything is saved as Anonymous user. Does this demo support all those basic operations as a logged in user? Do I have to set anything particular to make basic authentication work? And also, I noticed few times in Firebug login link trying to hit /user_login instead of /user/login. Why so? It is even in code druaplservice.js defined as user_login. Third, does cookie login work? I tried it as well, but failed to make it work. It is important to note that environment is CORS friendly, meaning that no CORS is on the way that might stop some features from working (checked as well with Postman / DHC). I used symlink you wrote in README as well as tuned htaccess to support all the necessary headers and requests.

My idea is to make this demo work, learn how D8/nodejs combo should be done (as a good reference point) and try to upgrade some parts of this demo as I progress in learning. So, any additional tips or tricks or resource that you also consider useful, is very welcome!

Thanks in advance!

nicholas12345 commented 9 years ago

Here is an update: login works via Cookie method (core patch plus some missing permissions). It sets a cookie in a browser and creates an entry in sessions table in DB. Cookie is IMHO more convenient as it doesn't authenticate user for every request. However, Angular app has no idea about that cookie; if I go to add new article or Lifecycle, app has no idea I am logged into Drupal (cookie). I cannot find if Drupal returned anything to app except maybe a flag; it definitely returned set cookie header which creates a cookie. So I am now looking for a way to let Anglar app knows about my Drupal cookie. I believe that functionality isn't complete in this example (correct me if I am wrong) and I am a little stuck to understand that communication part to be able to "fix" it. Thanks in advance!

nicholas12345 commented 9 years ago

Okay I am stuck in implementing withCredentials in this example. For some reason, whenever I define $httpProivder along with $RouterProvider in .config, it throws various errors. Any hints how to bundle withCredentials in this demo, please?

clemens-tolboom commented 9 years ago

I hope to help after august 27 due to hollidays

nicholas12345 notifications@github.com schreef op 13 juli 2015 19:18:22 CEST:

Okay I am stuck in implementing withCredentials in this example. For some reason, whenever I define $httpProivder along with $RouterProvider in .config, it throws various errors. Any hints how to bundle withCredentials in this demo, please?


Reply to this email directly or view it on GitHub: https://github.com/clemens-tolboom/drupal-8-rest-angularjs/issues/24#issuecomment-120997226

Sent from my Android device with K-9 Mail. Please excuse my brevity.

nicholas12345 commented 9 years ago

Thanks for your response, I'll continue with research into this, and re-sync with you after August 27th

laradevitt commented 9 years ago

@nicholas12345 Could your problems be related to Issue #2403307: Create a User session cookie login resource? I'm still trying to wrap my head around all of this myself.

clemens-tolboom commented 9 years ago

As this project was chasing Drupal 8 head is it quite broken. Both basic_auth and token based login broke. I tried to manage lots of issues through [meta] REST et al but got too busy to get back on both Drupal 8 as this project. My plan is to try to do DrupalCon sprint weekends to fix some core issues.

nicholas12345 commented 9 years ago

okay, thanks for update!

clemens-tolboom commented 9 years ago

I've fixed #26 which was vital and added #27 which could use PR's for other situations.

I can now get the frontpage node(s) but the user is broken on hal+json

So please test too using the json by hand edit app/drupalservice.js:36 setting mode to 'json'

nicholas12345 commented 8 years ago

Okay, so I updated this demo (including manual change in drupalservice.js), and updated Drupal core to rc2. When I try to log in, it appears to work, but I do not see session entry created in database? Also when I try to logout, it looks like it logged me out until I refresh page. Then it shows again I am logged in.

Front page node listing works. But Add article doesn't, it reports Unabled to create node (400: Bad Request: Entity type parameter must be included in context.) Also on first page I see in Firebug following: TypeError: Cannot read property 'uid' of undefined. I am not sure, am I missing out something? I set in configuration for everything JSON (not with HAL) and Basic_Auth. Good news is that first D8 release is coming out November 19th, so it will be more stable so we can finish this demo.

clemens-tolboom commented 8 years ago

My last commits esp bf0965cbd9b5e0bad24c1ed07b9e106a5311ac19 (on develop branch) made session based cookie the default.

Login/out is still in need of a patch :( ... guess the message but be changed to hint for login/out.

I believe the adding a node works for hal+json not sure as last weekend was a lot of ftw's (because BASIC_AUTH showed /node only for anonymous.)

Patch did not work even though I was admin. All fields from GET were submitted from. Checking with documentation on https://www.drupal.org/documentation/modules/rest fails too on how to do this.

Unabled to create node (400: Bad Request: Entity type parameter must be included in context.)

sounds familiar to an issue with I did not found back. Have you searched d.o on the text?

clemens-tolboom commented 8 years ago

@nicholas12345 the app working for me on /node/1 and /node reading user info and taxonomy terms. Hope the new messages and links help.

clemens-tolboom commented 8 years ago

And node comments :)

And you don't have to edit anything from the app. Just check the config.js.dist file

clemens-tolboom commented 8 years ago

My app/config.js

document.config = {
    'SERVER': {
        'scheme': 'http',
        'host': 'drupal.d8',
        'port': '8080',
        'user': '',
        'pass': '',
        'path': '',
        'query': '',
        'fragment': ''
    }
    ,
    USER : {
        username: 'admin',
        password: 'admin',
        authMethod: 'COOKIE'
    },
    MODE: 'hal+json'
};
nicholas12345 commented 8 years ago

Thanks for update! I pulled new changes. I didn't use configured user and pass in config.js, but used logging in interface in your example. I now added to config file.

I am using json only, still. Regarding error I reported, I did search and found this issue: https://www.drupal.org/node/1964034 Will see to check out your the latest patch there and apply it on rc D8. Will report about that. What is still confusing me is that I still get in console error that uid is undefined, even though I set in config.js and demo shows me logged in. If patch helps me fix the problem with entity type parameter, I'd be able to provide more detailed report to you.

Just to make sure we are on the same page regarding all necessary configuration, I am posting below my config.js file:

document.config = {
    'SERVER': {
        'scheme': 'http',
        'host': 'headless-drupal8.dev/drupal3',
        'port': '80',
        'user': '',
        'pass': '',
        'path': '',
        'query': '',
        'fragment': ''
    },
    USER : {
        // We target cookie but that needs token or login
        authMethod: 'COOKIE',
        // Use BASIC_AUTH for now
        //authMethod: 'BASIC_AUTH',
        // default username not for production
        username: 'admin',
        // default password not for production
        password: 'abc',
    },
    MODE: 'json'

};
clemens-tolboom commented 8 years ago

Trouble with using json now is the fromServer / toServer are probably not transforming correctly. I haven't testing the json forever :( One reason was hal+json had/has(?) better ID looking like uid.

Best is to config/use hal+json first to weed out other errors instead testing my ill-supported json.

What happens for ie the frontpage is:

nicholas12345 commented 8 years ago

Okay let me sum up everything done when setting up this example and necessary Drupal parts.

  1. Git clone this repository
  2. Inside it download Drupal 8 (8.0.0-beta12 at the moment)
  3. In Drupal, configure Taxonomy, vocabulary name Tags and in it add a couple of items
  4. Create content Type Article and add few nodes
  5. Create new authenticated user, and set him correct permissions
  6. With admin and new account, add few comments on created nodes in step 4
  7. Go to views and set up REST export displays for Comments by node, Frontpage, Taxonomy Terms
  8. In user permissions, make sure authenticated users can work with content type, and RESTful Web Services interaction (GET, POST, PATCH)
  9. Configure REST Resources to use supported formats hal+json and json with auth provider set to basic_auth and cookie (so we can test both). Make sure to have following configured: Content, Taxonomy vocabulary (?), User (?), View, View Mode (?)
  10. Edit app/config.js to look something like:
// http://stackoverflow.com/questions/22825706/angularjs-load-config-on-app-start
// http://php.net/manual/en/function.parse-url.php
document.config = {
    'SERVER': {
        'scheme': 'http',
        'host': 'headless-drupal8.dev/drupal3',
        'port': '80',
        'user': '',
        'pass': '',
        'path': '',
        'query': '',
        'fragment': ''
    },
    USER : {
        // We target cookie but that needs token or login
        authMethod: 'COOKIE',
        // Use BASIC_AUTH for now
        //authMethod: 'BASIC_AUTH',
        // default username not for production
        username: 'authuser',
        // default password not for production
        password: 'abc',
    },
    MODE: 'hal+json'

};

In config.js above, auth type is set to Cookie. When we want to switch auth type, we change there from COOKIE to BASIC_AUTH.

It is recommended to use hal+json as json isn't working properly yet. Anything else I've missed?

And here is a result of such setup:

TypeError: Cannot read property 'uid' of undefined
    at $scope.nodes.Node.query.message.text (node.js:120)
    at angular-resource.js:587
    at deferred.promise.then.wrappedCallback (angular.js:11682)
    at angular.js:11768
    at Scope.$get.Scope.$eval (angular.js:12811)
    at Scope.$get.Scope.$digest (angular.js:12623)
    at Scope.$get.Scope.$apply (angular.js:12915)
    at done (angular.js:8450)
    at completeRequest (angular.js:8664)
    at XMLHttpRequest.xhr.onreadystatechange (angular.js:8603)

even though example under User "block" shows "Logout" option instead login form. What I can see in Network tab is loaded following:

app.css 
bootstrap.css   
angular.js  
angular-route.js    
angular-resource.js 
app.js  
config.js   
drupalservice.js    
cacheservice.js 
node_lifecycle.js   
node.js 
node_nid.js 
node_add.js 
taxonomy_term.js    
node.html   
list?_format=json   
token   
node?_format=json   
glyphicons-halflings-regular.woff   

I see there token, which returns token string, but shouldn't this example then retrieve cookie from cookie authentication using token, username and password in login REST request? At least that is how works on Drupal 7 Services module. Maybe that part is missing and that is why it returns me uid of undefined?

So then I applied patch from issue above: https://www.drupal.org/files/issues/pass_entity_type_into-1964034-35.patch and flushed cache. That patch fixed te error reported for missing parameter entity type. But, I got another one: Unabled to create node (500: Internal Server Error). Okay, I checked apache error log and got this:

[Fri Nov 06 17:31:22.434082 2015] [:error] [pid 2692] [client 127.0.0.1:38187] Uncaught PHP Exception Drupal\\Core\\Entity\\EntityStorageException: "Missing bundle for entity type node" at /var/www/headless-drupal8/drupal3/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php line 63, referer: http://localhost:8000/app/

So, it seems patch didn't work. And I stopped there as I beleive I cannot provide accurate feedback what works and what not until you agree with my setup and until we don't figure out why it throws this error. I am curious, if you maybe did your example on some virtual machine, can you export it, compress and upload somewhere so I can take a look? It puzzles me why I have problems with my instance. :/

clemens-tolboom commented 8 years ago

Nice report ... will check hopefully this weekend.

I use https://github.com/build2be/drupal-rest-test to install and configure Drupal

./rest.sh full-install

then add rest export display to /node.