Get route params from url

Growler

I am trying to retrieve a reset pw token from a link in an reset pw email. The link sends the user back to my node/angular app with the token which I am trying to get.

Laravel API: email template

            <td class="content">
               <a href="http://localhost:3000/reset-password?token={{$token}}">Reset Your Password</a>
            </td>

Node/Angular app: ResetPassword.ejs template: I am using an angular controller:

<div ng-controller="resetPasswordCtrl">
    ...stuff
</div>

reset password Controller:

'use strict';

angular
    .module('myApp')
    .controller('resetPasswordCtrl', ['$scope', '$routeParams', '$location', function($scope, $routeParams, $location) {

        console.log('ROUTE PARAMS', $routeParams); //object with a bunch of getters/setters
        console.log('ROUTE PARAMS', $routeParams.token); //undefined
        console.log('Location', $location.search('token'));  //LocationHashbangUrl
        console.log('Location', $location.search().token);  //true
        console.log('Location', $location.search()['token']); //true
        console.log('Route current params', $route); //empty route object 

    }]);

For the LocationHashbangUrl ($location.search('token')), I know I am getting the correct url with the token because the $$absUrl shows it.

enter image description here

Why am I unable to retrieve the token param using one of those methods shown in the controller?

Growler

Turns out without using html5/angular routing, the typical methods

$location.search()
$routeParams

will not work.

Since I am passing the params and accessing my node app externally (from the e-mail link distributed from laravel), I needed to parse the URI using javascript.

I found this resource which makes it easy. So, the following works:

'use strict';

angular
    .module('myApp')
    .controller('resetPasswordCtrl', ['$scope', '$window', function($scope, $route, $window) {

        var getURIParams = function(variable) {
            var query = $window.location.search.substring(1);
            var vars = query.split('&');
            for (var i = 0; i < vars.length; i++) {
                var pair = vars[i].split('=');
                if (decodeURIComponent(pair[0]) == variable) {
                    return decodeURIComponent(pair[1]);
                }
            }
            console.log('Query variable %s not found', variable);
        };
        console.log('VAR', getURIParams('token')); //my token param
    }]);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related