How to catch and get value from redirected url in angular.js?

I am using angular.js as front end and node.js as backend. Now I am invoking the URL specified below in the click event on my home page.

Example URL:

https: // locahost: 3000 / auth / authorize? response_type = code & client_id = abzefcbbkjajsdd & scope = userfetchit & redirect_uri = localhost & nonce = 32324

When I call the above URL, it shows the login page in a new window, and after I gave the email address and password, it is redirected to another URL, which is listed below

Redirected URL:

https: // localhost: 3000 / auth / localhost? code = 8dacae52ee284db3dc776aa6d3563912

Expected Result:

Now, I want to get the code value "8dacae52ee284db3dc776aa6d3563912" from the above url in angular.js.

HTML code:

<html ng-app='app' ng-controller='LoginController'>

<head>
</head>

<body>
    <div class="container">

        <div class="jumbotron">
            <button type="button"

                class="btn btn-primary btn-lg"  ng-click="tokenLogin()">
                     New Login 
                </button>
        </div>
    </div>
    <pre>{{ cleanData | json}}</pre>
</body>

</html>

Controller Code:

angular.module('app', [])

.controller('LoginController', ['$location','$window','$scope', '$filter', function($location,$window,$scope, $filter) {
$scope.tokenLogin = function() {

            var url = 'https://localhost:3000/auth/authorize?response_type=code&client_id=abzefcbbkjajsdd&scope=userfetchit&redirect_uri=localhost&nonce=32324';
            $window.open(url);

        };

}]);
+4
source share
1 answer

First of all, you need to add the code parameter to your $stateProvideras follows:

.state('yourstate', {
  url: '/localhost?:code',
  templateUrl: 'localhost.html'
});

Then in your controller you can call:

console.log($stateParams.code);

What is it. You can set this value to a scope variable or whatever.

+1
source

All Articles