1

I'm trying to have an OAuth2 implemented for Ionic application This is my login for:

<ion-view view-title="Login" align-title="left">
<ion-nav-bar class="bar-positive">
    <ion-nav-back-button class="button-clear icon ion-ios7-arrow-back"> 
    </ion-nav-back-button>
</ion-nav-bar>
<ion-content class="padding">
    <img src="img/Exact_Logo.jpg" border="0"></a><br/>
    <a class="button icon-right ion-chevron-right" ng-click="loginToFoo()">Sign in with Foo Account</a>
  </div>
</ion-content>

and this is my login controller:

'use strict';

angular.module('fooapp')
.controller('loginCtrl', function($scope, $state, $http) {

$scope.signIn = function(user) {
 console.log('Sign-In', user);
 $state.go('tab.home');
}

$scope.loginToFoo = function() {

var URL_AUTH = 'https://foo.com/api/oauth2/auth';
var RESPONSE_TYPE_CODE = 'code';
var CLIENT_ID = '{xxxxxx-xxxxx-xxxxx-xxxxx}';
var REDIRECT_URL = 'http://localhost/callback';


var loginUrl = URL_AUTH + '?client_id=' + CLIENT_ID + 
              '&redirect_uri=' + REDIRECT_URL + 
              '&force_login=1&response_type=' + RESPONSE_TYPE_CODE ;

window.open(loginUrl, "_system", "location=yes"); 
};
})

I have also added these lines to config.xml:

<feature name="InAppBrowser">
  <param name="android-package" value="org.apache.cordova.InAppBrowser"/>
</feature>
<feature name="InAppBrowser">
  <param name="ios-package" value="CDVInAppBrowser" />
</feature>

Although I have already added cordova-plugin-inappbrowser, as I use 'ionic serve', I can see the correct result in a browser but as I use 'ionic emulate android' or run it on device, after login I receive following error: enter image description here

I understand that "http://localhost" is not the device root for application but I don't know I should change it to what exactly.

Afflatus
  • 933
  • 1
  • 12
  • 39
  • Any reason you don't want to use ng-cordova-oauth in your app? It will take all the pain out of what you're trying to do. – Nic Raboy Oct 22 '15 at 04:07
  • @NicRaboy because I couldn't get ng-cordova-oauth work in testing on Ionic view – Afflatus Oct 25 '15 at 06:06
  • ng-cordova-oauth doesn't work in ionic view for the same reason you are getting your current error. It has to do with ionic view and the InAppBrowser plugin. – Nic Raboy Oct 26 '15 at 12:16
  • yeah, indeed. Thanks Nic. I will use emulator for testing – Afflatus Oct 27 '15 at 13:41

1 Answers1

0

I think the URL has to be a valid URL. It did not work if I use a "localhost URL" when I run on the phone, but worked fine if I use a URL with a domain. Example http://domain-name/auth/callback/ instead of http://localhost/auth/callback. You can check this answer for a working configuration with keycloak with InAppBrowser.

Hope it helps.

NehaM
  • 1,272
  • 1
  • 18
  • 32