-
Drupal 7 authentication with javascript using Phone gap or Sencha
over 9 years ago
-
over 9 years ago
Drupal's Service module provides resources to implement these kind of APIs. You can use Service module and enable its in-build login/logout resource.
Please follow these steps to implement login validation using JS from Phonegap, Sencha or any other remote application:
1: Install and enable Service module.
2: Create a service endpoint and add its path.
3: In resource tab select Checkbox for User (login and logout).
4: Enable REST Server Response Formatters & Request Parsing
5: For testing a Resource from Drupal Services, install poster in firefox and check your added service:- Tool->poster
URL: http://localhost/my_drupal_site/?q=
/system/connect.json Content Type: application/x-www-form-urlencoded
Actions: POST
6: If connection establish then process to step 7 otherwise check you connection.
7: Create the simple php login page in remote application and call following javascript to make an ajax call:
$('#page_dashboard').live('pageshow',function(){ try { $.ajax({ url: "http://10.0.2.2/my_drupal_site/?q=my_services/system/connect.json", type: 'post', dataType: 'json', error: function (XMLHttpRequest, textStatus, errorThrown) { alert('page_dashboard - failed to system connect'); console.log(JSON.stringify(XMLHttpRequest)); console.log(JSON.stringify(textStatus)); console.log(JSON.stringify(errorThrown)); }, success: function (data) { var drupal_user = data.user; if (drupal_user.uid == 0) { // user is not logged in, show the login button, hide the logout button $('#button_login').show(); $('#button_logout').hide(); } else { // user is logged in, hide the login button, show the logout button $('#button_login').hide(); $('#button_logout').show(); } } }); } catch (error) { alert("page_dashboard - " + error); } });
This is just a jQuery code and can be used in any remote application which use jQuery. I hope this will help you.
1 Answer(s)