Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- So I have a view, it's the end of a login page, 'login' being my controller
- <!-- ... -->
- <button type="submit" class="col-sm-6 col-sm-12" ng-disabled="login_form.$invalid" class="btn btn-primary">Log in</button>
- <div class="col-sm-12 alert alert-warning" ng-show="login.failed()">
- Bad username, email or password
- </div>
- <div class="col-sm-12 alert alert-warning" ng-show="login.error()">
- Something went wrong... The guru's probably meditating.
- </div>
- /* Login Controller definition */
- /* Here I'm injecting an 'auth' service to implement RESTAPI calls */
- ar.module('frontendApp')
- .controller('LoginCtrl', ['$scope', 'auth', function ($scope, auth) {
- var self = this;
- this._response = undefined;
- this.remember_me = false;
- this._error = false;
- /* Called on a button click */
- this.login = function () {
- self._response = undefined;
- self._error = false;
- /* I'm trying to auth, then set _response with the answer */
- auth.login(self.user, self.remember_me).then(function (answer) {
- self._response = answer;
- }, function (data) {
- console.error('LoginCtrl: error caught. Something went wrong.');
- console.error('data', data);
- self._error = true;
- });
- };
- /* These are the methods called by warnings, they use self._response */
- this.failed = function () {
- return self._response != undefined && !self._response.authentified;
- };
- this.error = function () {
- return self._error;
- };
- }]);
- /* This is my 'auth' service */
- angular.module('frontendApp')
- .service('auth', ['$http', function ($http) {
- var self = this;
- this.login = function (user, remember_me) {
- remember_me = remember_me === undefined ? false : remember_me;
- user.username = user.login; /* TODO: useless once converted to express - test only */
- /* Return a promise that is solved if the response from the server makes sense. */
- return new Promise(function (accept, reject) {
- try {
- $http.post(SERVER_PREFIX + 'login', user).then(function (data) { // On accept
- expect(data.data).to.contain.keys('id', 'userId');
- accept({
- authentified: true,
- user: user,
- answer: data.data
- });
- }, function (data) { // On rejected
- expect(data.data).to.contain.keys('error');
- expect(data.data.error).to.contain.keys('status', 'code');
- expect(data.data.error.code).to.equal('LOGIN_FAILED');
- accept({
- authentified: false,
- user: user,
- answer: data.data
- });
- });
- }
- catch (error) {
- if (error instanceof AssertionError) reject(error.data); // Bad data sent by the server
- else throw error;
- }
- });
- };
- }]);
Advertisement
Add Comment
Please, Sign In to add comment