Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Registration & Reset password #68

Open
wants to merge 46 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
52a306a
Merge pull request #1 from ordavidil/update-gitignore
ordavidil Jun 16, 2015
bce2ec0
Adding "Reset Password" message.
Jun 18, 2015
d8be92d
Adding "Reset Password" endpoint.
Jun 18, 2015
feb0c2d
Adding "Forgot Password" flow on the client.
Jun 18, 2015
7236eb4
Adding "Save new password" flow to the client.
Jun 18, 2015
309cb12
WIP - Adding an endpoint to update a user entity.
Jun 18, 2015
8da77cf
Changing resource name to prevent a clash with the 'users' resource.
Jun 22, 2015
70595ce
WIP - Adding sign up page.
Jun 23, 2015
2f3c7eb
WIP - Adding email availability endpoint.
Jun 23, 2015
70b62d9
WIP - Adding username availability endpoint.
Jun 23, 2015
701a538
Adding "Verify Email" message.
Jun 24, 2015
ed95f95
Fixing email & username availability endpoints
Jun 24, 2015
c081b8c
Adding the option of creating new user through endpoint.
Jun 24, 2015
40ce87c
Adding the "sign up" function to the client.
Jun 24, 2015
48f8f68
Resetting the flags before each request.
Jun 25, 2015
285b6e9
Adding email verification mechanism.
Jun 25, 2015
3673dcc
Prevent from drupal to notify users about activated account.
Jun 25, 2015
a03bfa0
Changing to camelCase.
Jul 5, 2015
70b5b56
Improved the error msg.
Jul 5, 2015
cb081e4
Adding a setter for the access token.
Jul 5, 2015
6a38f14
Adding comment.
Jul 5, 2015
a5d061c
Removing unnecessary variable.
Jul 5, 2015
3a094b9
Cleanup files.
Jul 5, 2015
7174bd6
Adding RESTful empty response.
Jul 6, 2015
bf0b5f8
Fixing err msgs on "Forgot Password" page.
Jul 6, 2015
fe3cf58
Deny access to password field on GET request.
Jul 7, 2015
9337802
Adding "Users Availability".
Jul 7, 2015
942507a
Sending verification mail via "hook_user_insert".
Jul 9, 2015
e811c6d
Changing back the class to extend from.
Jul 9, 2015
d52578c
Removing "SkeletonTokenNotifierResource" class.
Jul 9, 2015
245e436
Checking the site settings before sending a verification mail.
Jul 9, 2015
21dc4a7
Checking the site settings before allowing a user to register.
Jul 9, 2015
90a7201
Removing unnecessary name/mail setters.
Jul 12, 2015
1afa24f
Cleanup.
Jul 12, 2015
b51a293
Cleanup.
Jul 14, 2015
b3af456
Cleanup.
Jul 15, 2015
a5883a1
Verify the email on the state resolve.
Aug 18, 2015
ae513bc
Cleaning up the "Reset Password" controller.
Aug 19, 2015
159671d
Fixing .travis.tml
Aug 20, 2015
237c8a7
Fixing .travis.yml round #2
Aug 20, 2015
2377d6f
Fixing .travis.yml round #3
Aug 20, 2015
b2804c1
Updating angular version.
Aug 20, 2015
32ea7af
Fixing .travis.yml round #4
Aug 20, 2015
d4969d7
Fixing travis.yml round #5
Sep 1, 2015
d9c2023
Update angular minor version.
Sep 1, 2015
5292ce9
Fixing travis.yml round #6
Sep 1, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/templates/client/app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ <h3>Login with demo / 1234</h3>
<script src="scripts/controllers/companies.js"></script>
<script src="scripts/controllers/events.js"></script>
<script src="scripts/controllers/login.js"></script>
<script src="scripts/controllers/signup.js"></script>
<script src="scripts/controllers/forgot-password.js"></script>
<script src="scripts/controllers/reset-password.js"></script>
<script src="scripts/services/auth.js"></script>
<script src="scripts/services/utils.js"></script>
<script src="scripts/services/events.js"></script>
Expand Down
39 changes: 37 additions & 2 deletions app/templates/client/app/scripts/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,42 @@ angular
.state('login', {
url: '/login',
templateUrl: 'views/login.html',
controller: 'LoginCtrl'
controller: 'LoginCtrl',
resolve: {
accessToken: function() {
return null;
}
}
})
.state('signup', {
url: '/signup',
templateUrl: 'views/signup.html',
controller: 'SignUpCtrl'
})
.state('verifyEmail', {
url: '/verify-email/{accessToken:string}',
templateUrl: 'views/login.html',
controller: 'LoginCtrl',
resolve: {
accessToken: function($stateParams) {
return $stateParams.accessToken;
}
}
})
.state('forgotPassword', {
url: '/forgot-password',
templateUrl: 'views/forgot-password.html',
controller: 'ForgotPasswordCtrl'
})
.state('resetPassword', {
url: '/reset-password/{accessToken:string}',
templateUrl: 'views/reset-password.html',
controller: 'ResetPasswordCtrl',
resolve: {
accessToken: function($stateParams) {
return $stateParams.accessToken;
}
}
})
.state('dashboard', {
abstract: true,
Expand Down Expand Up @@ -172,7 +207,7 @@ angular

'response': function(result) {
if (result.data.access_token) {
localStorageService.set('access_token', result.data.access_token);
Auth.setAccessToken(result.data.access_token);
}
return result;
},
Expand Down
33 changes: 33 additions & 0 deletions app/templates/client/app/scripts/controllers/forgot-password.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

/**
* @ngdoc function
* @name clientApp.controller:ForgotPasswordCtrl
* @description
* # ForgotPasswordCtrl
* Controller of the clientApp
*/
angular.module('clientApp')
.controller('ForgotPasswordCtrl', function ($scope, Auth) {

/**
* Send a password reset link.
*/
$scope.forgotPassword = function() {
// Reset the error message for each request.
$scope.ErrorMsg = false;

Auth.resetPassword($scope.email).then(function () {
$scope.passwordResetSent = true;
},
function(response) {
$scope.ErrorMsg = response.data.title;

// Too many requests.
if (response.status == 429) {
$scope.ErrorMsg = response.statusText;
$scope.TooManyRequests = true;
}
});
};
});
9 changes: 8 additions & 1 deletion app/templates/client/app/scripts/controllers/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@
* Controller of the clientApp
*/
angular.module('clientApp')
.controller('LoginCtrl', function ($scope, Auth, $state) {
.controller('LoginCtrl', function ($scope, Auth, $state, Account, accessToken) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ordavidil Please remove this injection of accessToken (Not needed) and replace it with the emailVerified.


// Try to verify the email when access token is being passed.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ordavidil

  • Set the access_token using the Auth.setAccessToken($stateParams.accessToken)
  • Please move this to thestate resolve method and return the result of Account.verifyEmail().
// Try to verify the email when access token is being passed.
if ($state.current.name == 'verifyEmail' && accessToken) {
Account.verifyEmail(accessToken).then(function () {
$scope.emailVerified = true;
 });
 }

if ($state.current.name == 'verifyEmail' && accessToken) {
Account.verifyEmail(accessToken).then(function () {
$scope.emailVerified = true;
});
}

// Will be FALSE during login GET period - will cause the login button to be
// disabled.
Expand Down
37 changes: 37 additions & 0 deletions app/templates/client/app/scripts/controllers/reset-password.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

/**
* @ngdoc function
* @name clientApp.controller:ResetPasswordCtrl
* @description
* # ResetPasswordCtrl
* Controller of the clientApp
*/
angular.module('clientApp')
.controller('ResetPasswordCtrl', function ($scope, Auth, $state, $location, Account, accessToken) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this injection of accessToken (Not needed).


// If 'access-token' is not provided redirect to login.
if (!accessToken) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ordavidil Please move $state.go('login') to the state onEnter() function

$state.go('login');
}

// Determine if password was reset successfully.
$scope.passwordSaved = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// Determine if password was reset successfully.


/**
* Setting the access token in the localStorage so we can get the account
* information and pull out the user ID from it to PATCH the user entity.
*
* @param password
* The new password.
*/
$scope.saveNewPassword = function(password) {
Auth.setAccessToken(accessToken);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ordavidil Please remove Auth.setAccessToken(accessToken) and also move it to the state onEnter() function


Account.get().then(function(user) {
Auth.savePassword(user.id, password).then(function() {
$scope.passwordSaved = true;
});
});
};
});
39 changes: 39 additions & 0 deletions app/templates/client/app/scripts/controllers/signup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';

/**
* @ngdoc function
* @name clientApp.controller:SignUpCtrl
* @description
* # SignUpCtrl
* Controller of the clientApp
*/
angular.module('clientApp')
.controller('SignUpCtrl', function ($scope, Auth) {

// Reset the flags.
$scope.emailAvailable = true;
$scope.usernameAvailable = true;

/**
* Send a password reset link.
*/
$scope.signUp = function(user) {
// Clear the error before each request.
$scope.signupError = undefined;

Auth.usersAvailability(user).then(function(response) {
$scope.usernameAvailable = response.data.data.available.name;
$scope.emailAvailable = response.data.data.available.mail;

if ($scope.emailAvailable && $scope.usernameAvailable) {
Auth.signUp(user).then(function() {
// User registered successfully.
$scope.signedUp = true;
}, function (response) {
// Error trying to register the user.
$scope.signupError = response.data.detail;
});
}
});
};
});
22 changes: 21 additions & 1 deletion app/templates/client/app/scripts/services/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Service in the clientApp.
*/
angular.module('clientApp')
.service('Account', function ($q, $http, $timeout, Config, $rootScope, $log) {
.service('Account', function ($q, $http, $timeout, Config, $rootScope, Auth) {

// A private cache key.
var cache = {};
Expand Down Expand Up @@ -43,6 +43,26 @@ angular.module('clientApp')
return deferred.promise;
}

/**
* Verify a user.
*
* @param accessToken
* @returns {*}
*/
this.verifyEmail = function(accessToken) {
Auth.setAccessToken(accessToken);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ordavidil Please remove this param accessToken and alsoAuth.setAccessToken(accessToken) .
You all ready get the access_token form the request.


// After setting the access token in the local storage, try to get the
// user account from the data, if succeed then change its status.
return getDataFromBackend().then(function(user) {
return $http({
method: 'PATCH',
url: Config.backend + '/api/v1.1/users/' + user.id,
data: {status: 1}
});
});
};

/**
* Save meters in cache, and broadcast en event to inform that the meters data changed.
*
Expand Down
72 changes: 72 additions & 0 deletions app/templates/client/app/scripts/services/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
angular.module('clientApp')
.service('Auth', function ($injector, $rootScope, Utils, localStorageService, Config) {

/**
* An access token setter.
*
* @param accessToken
*/
this.setAccessToken = function(accessToken) {
localStorageService.set('access_token', accessToken);
};

/**
* Login by calling the Drupal REST server.
*
Expand All @@ -29,6 +38,69 @@ angular.module('clientApp')
});
};

/**
* Trigger a `reset password` action on the server for this email.
*
* @param email
* The email of the user.
*
* @returns {*}
*/
this.resetPassword = function(email) {
return $injector.get('$http')({
method: 'POST',
url: Config.backend + '/api/reset-password',
data: {email: email}
});
};

/**
* Save new password for a user.
*
* @param uid
* User id.
* @param password
* A new password to set.
*
* @returns {*}
*/
this.savePassword = function(uid, password) {
return $injector.get('$http')({
method: 'PATCH',
url: Config.backend + '/api/v1.1/users/' + uid,
data: {password: password}
});
};

/**
* Checks users availability.
*
* @param user
* @returns {*}
*/
this.usersAvailability = function(user) {
var params = 'name=' + user.name + '&mail=' + user.mail;

return $injector.get('$http')({
method: 'GET',
url: Config.backend + '/api/users-availability?' + params
});
};

/**
* Sign Up new user.
*
* @param data
* @returns {*}
*/
this.signUp = function(data) {
return $injector.get('$http')({
method: 'POST',
url: Config.backend + '/api/v1.1/users',
data: data
});
};

/**
* Logout current user.
*
Expand Down
4 changes: 4 additions & 0 deletions app/templates/client/app/styles/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@
min-height: 500px;
}

.vertical-space {
margin-top: 15px;
margin-bottom: 15px;
}
53 changes: 53 additions & 0 deletions app/templates/client/app/views/forgot-password.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<div id="dashboard-forgot-password">
<div class="page-forgot-password">
<div class="forgot-password-header">
<section class="logo text-center">
<h1>Skeleton</h1>
</section>
</div>

<div class="forgot-password-body">
<div class="container">

<div class="alert alert-success fade in" ng-show="passwordResetSent">
<i class="fa fa-check-circle fa-fw fa-lg"></i>
An email with instructions has been sent to "{{ email }}".
</div>

<div class="alert alert-danger fade in" ng-show="ErrorMsg">
<i class="fa fa-times-circle fa-fw fa-lg"></i>
{{ ErrorMsg }}
</div>

<h4>Forgot your password?</h4>
<p>Enter your email to recover your password.</p>

<div class="form-container">
<form class="form-horizontal" id="login" ng-submit="forgotPassword()">
<fieldset>
<div class="form-group">
<div class="input-group input-group-first">
<span class="input-group-addon">
<span class="ti-email"></span>
</span>
<input ng-disabled="passwordResetSent || TooManyRequests" ng-model="email" name="email" type="text" class="form-control input-lg" placeholder="Email" required>
</div>
</div>

<div class="form-group">
<input ng-disabled="passwordResetSent || TooManyRequests" type="submit" class="btn btn-primary btn-lg btn-block text-center" id="submit" value="Recover Password" />
</div>

<div class="row">
<div class="col-xs-6">
<a ui-sref="login">Back to login</a>
</div>
</div>
</fieldset>
</form>
</div>
</div>
</div>

</div>
</div>
Loading