Created
May 10, 2016 08:59
-
-
Save erezsob/69fd81f49cb9348f4ff3fbb24f988e07 to your computer and use it in GitHub Desktop.
Password forgotten
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
angular.module('app') | |
.config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) { | |
$stateProvider | |
.state('mainNotAuthorized.passwordForgotten', { | |
url: 'password-forgotten/:errorStatus', | |
views: { | |
'data': { | |
templateUrl: 'base/password-forgotten/password-forgotten-login.template.html', | |
controller: 'PasswordForgottenCtrl' | |
} | |
} | |
}) | |
.state('mainNotAuthorized.passwordForgottenSuccess', { | |
url: 'password-forgotten-success/:email', | |
views: { | |
'data': { | |
templateUrl: 'base/password-forgotten/password-forgotten-success.html', | |
controller: 'PasswordForgottenSuccessCtrl' | |
} | |
} | |
}) | |
.state('mainNotAuthorized.passwordForgottenChange', { | |
url: 'password-forgotten-change', | |
views: { | |
'data': { | |
templateUrl: 'base/password-forgotten/password-forgotten-change.html', | |
controller: 'PasswordForgottenChangeCtrl' | |
} | |
} | |
}) | |
}]) | |
.controller('PasswordForgottenCtrl', ['$scope', '$location', 'FishUser', '$state', '$stateParams', function($scope, $location, FishUser, $state, $stateParams) { | |
$scope.values = {'email':''}; | |
$scope.errorMessage = undefined; | |
if ($stateParams.errorStatus !== '') { | |
$scope.errorMessage = "Reset link is expired. Please try again."; | |
} | |
$scope.resetPassword = function() { | |
if ($scope.values.email === '') { | |
$scope.errorMessage = 'Please enter an Email-Address'; | |
return; | |
} else if (!$scope.values.email) { | |
$scope.errorMessage = 'Please enter a valid Email' | |
return; | |
} | |
FishUser.requestPasswordReset($scope.values.email) | |
.then( | |
function(data) { | |
$scope.successMessage = 'Email got sent' | |
$state.go('mainNotAuthorized.passwordForgottenSuccess', {email: $scope.values.email}); | |
}, function(error) { | |
$scope.errorMessage = 'There was an error' | |
} | |
); | |
}; | |
}]) | |
.controller('PasswordForgottenChangeCtrl', ['$scope', '$state', 'FishUser', 'LoopBackAuth', '$location', '$timeout', '$stateParams', function($scope, $state, FishUser, LoopBackAuth, $location, $timeout, $stateParams) { | |
var queryParameters = $location.search(); | |
$scope.init = function() { | |
if (!queryParameters.hasOwnProperty('access_token')) { | |
$scope.errorMessage = 'Reset link is expired. Please try again.' | |
} | |
} | |
$scope.password1 = ''; | |
$scope.password2 = ''; | |
$scope.errorMessage = undefined; | |
$scope.successMessage = undefined; | |
$scope.passwordsValid = function() { | |
if ($scope.password1 === '') { | |
$scope.errorMessage = ''; | |
return false; | |
} | |
var validPass = FishUser.passwordValidation($scope.password1, $scope.password2); | |
if (validPass.valid === false) { | |
$scope.errorMessage = validPass.error; | |
return false; | |
} | |
$scope.errorMessage = ''; | |
return true; | |
} | |
$scope.changePassword = function() { | |
var validPass = FishUser.passwordValidation($scope.password1, $scope.password2); | |
if (validPass.valid === true) { | |
LoopBackAuth.accessTokenId = queryParameters.access_token; | |
FishUser.passwordForgottenChange($scope.password1) | |
.then( | |
function(status) { | |
$scope.successMessage = 'Your password has been reset successfully! You\'re being redirected to the Login Page'; | |
LoopBackAuth.accessTokenId = null; | |
$timeout(function() { | |
$location.path('/'); | |
}, 3000); | |
}, function(error) { | |
LoopBackAuth.accessTokenId = null; | |
//Token has expired - redirect to enter user's email | |
if (error.status === 401) { | |
$state.go('mainNotAuthorized.passwordForgotten', {errorStatus: error.status}); | |
} else { | |
$scope.errorMessage = "There was an error" | |
} | |
} | |
); | |
} | |
}; | |
$scope.init(); | |
}]) | |
.controller('PasswordForgottenSuccessCtrl', ['$scope', '$stateParams', function($scope, $stateParams) { | |
$scope.email = $stateParams.email; | |
} | |
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="main-small-box-center"> | |
<div class="password-forgotten-info"> | |
<h1>Success</h1> | |
<p>We check if an account with the email address <strong>{{ email }}</strong> exist.</p> | |
<p>For privacy reasons do we not confirm the existence of any email address.</p> | |
<p>If the email address exists in our database, instructions about how to reset the password will be send to it.</p> | |
</div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="main-small-box-center password-forgotten-page"> | |
<div class="container-fluid"> | |
<h3>ENTER NEW PASSWORD</h3> | |
<form id="loginform" role="form" name="lForm" novalidate > | |
<div ng-show="!successMessage"> | |
<div class="input-group form-group center-block"> | |
<i class="glyphicon glyphicon-lock"></i> | |
<input ng-model="password1" type="password" class="form-control" placeholder="enter password" autofocus /> | |
</div> | |
<div class="input-group form-group center-block"> | |
<i class="glyphicon glyphicon-lock"></i> | |
<input ng-model="password2" type="password" class="form-control" id="password_forgot_change_2" placeholder="re-enter password" autofocus /> | |
</div> | |
<div class="text-center"> | |
<button class="full-width-button" ng-click="changePassword()" class="btn btn-success" ng-disabled="!passwordsValid()">Change Password</button> | |
</div> | |
</div> | |
<div class="alert alert-danger text-center" role="alert" ng-if="errorMessage">{{ errorMessage }}</div> | |
<div class="alert alert-success text-center" role="alert" ng-if="successMessage"><strong>{{ successMessage }}</strong></div> | |
</form> | |
</div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
user.beforeRemote('*', function(ctx, unused, next) { | |
// Everybody can use the login & create method so we skip in that case | |
Add a comment to this line | |
if (['user.create', 'user.login', 'user.requestPasswordReset'].indexOf(ctx.methodString) != -1) { | |
next(); | |
return; | |
} | |
} | |
user.requestPasswordReset = function(email, req, cb) { | |
user.resetPassword({ | |
email: email | |
}, function(error, success) { | |
console.log("error"); | |
console.log(error); | |
console.log("success"); | |
console.log(success); | |
if (error) { | |
user.app.additions.returnError(500, error, cb); | |
return; | |
} | |
cb(null, 'test: ' + email); | |
}) | |
} | |
/** | |
* Send password reset link when requested | |
*/ | |
user.on('resetPasswordRequest', function(info) { | |
var url = 'http://test.link.fish:8000/#/password-forgotten-change'; | |
var html = 'Click <a href="' + url + '?access_token=' + | |
info.accessToken.id + '">here</a> to reset your password'; | |
user.app.models.Email.send({ | |
to: info.email, | |
from: info.email, | |
subject: 'Password reset', | |
html: html | |
}, function(err) { | |
if (err) return console.log('> error sending password reset email'); | |
console.log('> sending password reset email to:', info.email); | |
}); | |
}); | |
/** | |
* Reset the user's password | |
*/ | |
user.passwordForgottenChange = function(password, req, cb) { | |
var userId = req.accessToken.userId; | |
user.__changePassword(userId, password).then( | |
function(data) { | |
cb(null, true); | |
}, function(error) { | |
user.app.additions.returnError(500, error, cb); | |
} | |
); | |
} | |
user.remoteMethod( | |
'requestPasswordReset', | |
{ | |
description: 'Request password to be reset', | |
accepts: [{arg: 'email', type: 'string', required: true}, { arg: 'req', type: 'object', http: { source: 'req' } }], | |
returns: {arg: 'result', type: 'string'}, | |
http: {verb: 'POST', path: '/requestPasswordReset'} | |
} | |
); | |
user.remoteMethod( | |
'passwordForgottenChange', | |
{ | |
description: 'Reset the password that been requested', | |
accepts: [{arg: 'password', type: 'string', required: true}, { arg: 'req', type: 'object', http: { source: 'req' } }], | |
returns: {arg: 'result', type: 'string'}, | |
http: {verb: 'POST', path: '/passwordForgottenChange'} | |
} | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment