3

I have a forgot password form, on which when the user enters an email it hits the backend and sends out an email link to user as below.

enter image description here

The user clicks on the link, which invokes the back-end service. How can i control this url via angular? So, basically this calls a back-end resouce, but i want this url to be handled in front-end too.

If this question is not so clear, can anyone show me an example of forgot password implementation in AngularJS and NodJs or any backend.

Shane
  • 5,517
  • 15
  • 49
  • 79
  • Why don't you change the link in the following way:http://localhost:3000/#/resetpassword/55356236t663623/ And have route for this with routeProvider or stateProvider – Raju Bera Oct 03 '15 at 04:09
  • Why don't you change the link in the following way: http://localhost:3000/#/resetpassword/55356236t663623 And have route for this in your angularjs app with routeProvider or stateProvider – Raju Bera Oct 03 '15 at 04:13
  • You should have a "forgot password" page like this: `https://example.com/forgotpw/?token=xxxxxxxxx`, then that page should provide a front-end experience to the user, get the new password, and send an AJAX request to your backend script. – Maximillian Laumeister Oct 03 '15 at 04:20

1 Answers1

5

if you can change the link in the email then change it in the following way:

http://localhost:3000/#/resetpassword/<token>

Now in your angular route you need to listen to this route as following:

angular.module('myApp', ['ngRoute'])

 .controller('ResetPasswordController', function($scope, $route, $routeParams, $location) {
     //password resetting functionality
 })
.config(function($routeProvider, $locationProvider) {
  $routeProvider
   .when('/resetpassword/:token', {
    templateUrl: 'reset.html',
    controller: 'ResetPasswordController',
    resolve: {
      // Call the backend service to check if the token is valid
      verifyToken: function($q, $route) {
        var deferred = $q.defer();
        $http({
          method: 'GET',
          url: '/reset/' + $route.current.params.token
        }).success(function (data) {
           deferred.resolve(data);
        }).error(function (msg) {
          deferred.reject(msg);
        });

        return deferred.promise;
      }
    }
  })
});
Raju Bera
  • 948
  • 8
  • 14
  • Lets take this fails and i get a deferred.reject(msg), can i handle this in my controller by any chance? – Shane Oct 03 '15 at 04:54
  • 1
    In angular route's resolves are executed before the route hash changes and they can be passed to the controller as service using the same name. Now if a resolve returns a promise then angular will wait for the promise to get resolved then it will continue the flow. If the promise get rejected then the route change will not happen. In your case if you want to still do some function from the controller then instead of reject you need to resolve the promise with proper argument. – Raju Bera Oct 03 '15 at 05:52
  • 1
    Did that answer help you someway? – Raju Bera Oct 27 '15 at 11:57