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 17 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/sign-up.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
37 changes: 36 additions & 1 deletion 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($stateParams) {
return null;
}
}
})
.state('sign-up', {
Copy link
Member

Choose a reason for hiding this comment

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

camelCase (signup, verifyEmail)

url: '/sign-up',
templateUrl: 'views/sign-up.html',
controller: 'SignUpCtrl'
})
.state('verify-email', {
url: '/verify-email/{accessToken:string}',
templateUrl: 'views/login.html',
controller: 'LoginCtrl',
resolve: {
accessToken: function($stateParams) {
return $stateParams.accessToken;
}
}
})
.state('forgot-password', {
url: '/forgot-password',
templateUrl: 'views/forgot-password.html',
controller: 'ForgotPasswordCtrl'
})
.state('reset-password', {
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
27 changes: 27 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,27 @@
'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() {
Auth.resetPassword($scope.email).then(function () {
$scope.passwordResetSent = true;
},
function(response) {
// Too many requests.
if (response.status == 429) {
$scope.ErrorMsg = 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 (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
35 changes: 35 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,35 @@
'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, localStorageService, accessToken) {

// If 'access-token' is not provided as a URL parameter, 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');
}

$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) {
localStorageService.set('access_token', accessToken);
Copy link
Member

Choose a reason for hiding this comment

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

Auth.setToken()

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

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

/**
* Send a password reset link.
*/
$scope.signUp = function(user) {

// Reset the flags before each request.
$scope.emailNotAvailable = false;
$scope.usernameNotAvailable = false;

Auth.usernameAvailable(user.username).then(function(response) {
Copy link
Member

Choose a reason for hiding this comment

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

Auth.usernameAvailable()
  .then(function(data) {
    return ...
  })
  .then(function(data) {
  })
  .catch(function() {
  });

read http://pouchdb.com/2015/05/18/we-have-a-problem-with-promises.html

if (response.data.data.available) {
Auth.emailAvailable(user.email).then(function(response) {
if (response.data.data.available) {
// Email is available.
Auth.signUp(user).then(function() {
// User registered successfully.
$scope.signedUp = true;
});
}
else {
// Email is not available.
$scope.emailNotAvailable = true;
}
});
}
else {
// Username is not available.
$scope.usernameNotAvailable = true;
}
});
};
});
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, $log, localStorageService) {

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

/**
* Verify a user.
*
* @param token
* @returns {*}
*/
this.verifyEmail = function(token) {
localStorageService.set('access_token', token);
Copy link
Member

Choose a reason for hiding this comment

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

Auth.setToken()


// 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/users-update/' + user.id,
data: {status: 1}
});
});
};

/**
* Save meters in cache, and broadcast en event to inform that the meters data changed.
*
Expand Down
76 changes: 76 additions & 0 deletions app/templates/client/app/scripts/services/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,82 @@ angular.module('clientApp')
});
};

/**
* If email is available then send to it an email with instructions.
*
* @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/users-update/' + uid,
data: {password: password}
});
};

/**
* Checks if email is available.
*
* @param email
* @returns {*}
*/
this.emailAvailable = function(email) {
return $injector.get('$http')({
method: 'POST',
url: Config.backend + '/api/email-available',
data: {email: email}
});
};

/**
* Checks if username is available.
*
* @param username
* @returns {*}
*/
this.usernameAvailable = function(username) {
return $injector.get('$http')({
method: 'POST',
url: Config.backend + '/api/username-available',
data: {username: username}
});
};

/**
* Sign Up new user.
*
* @param data
* @returns {*}
*/
this.signUp = function(data) {
return $injector.get('$http')({
method: 'POST',
url: Config.backend + '/api/users-update',
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>
If the username is registered in our system we'll send you an email with instructions.
</div>

<div class="alert alert-danger fade in" ng-show="ErrorMsg">
<i class="fa fa-times-circle fa-fw fa-lg"></i>
Request failed.
Copy link
Member

Choose a reason for hiding this comment

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

improve, there's request fail, and there's can't login due to too many attempts

</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" 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 || ErrorMsg" 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>
19 changes: 19 additions & 0 deletions app/templates/client/app/views/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,20 @@ <h1>Skeleton</h1>

<div class="signin-body">
<div class="container">

<div class="row vertical-space">
<div class="col-xs-6">
<a ui-sref="sign-up">Sing up</a>
</div>
</div>

<div class="alert alert-success fade in" ng-show="emailVerified">
<i class="fa fa-check-circle fa-fw fa-lg"></i>
Your email has been verified successfully.
</div>

<div class="form-container">

<form class="form-horizontal" id="login" ng-submit="login(user)">
<fieldset>
<div class="form-group">
Expand All @@ -34,6 +47,12 @@ <h1>Skeleton</h1>
<input ng-disabled="!loginButtonEnabled" type="submit" class="btn btn-primary btn-lg btn-block text-center" id="submit" value="Log in" />
</div>

<div id="remember-me-wrapper" class="row">
<div class="col-xs-6">
<a ui-sref="forgot-password" id="login-forget-link">Forgot your password?</a>
</div>
</div>

<div class="divider divider-xl"></div>

<div class="message error">
Expand Down
Loading