Commit a9de2b4b authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Refactor Auth controller login/logout methods.

Rename several methods:

- loggedIn -> setLoggedIn
- loggedOut -> setLoggedOut
  For setting the state of login form.

- getSession -> retreveSession
  "get" is a bad start for method that doesn't return a value.

- showLogin -> showLoginForm
  Previously it was hard to distinguish it from showLoggedIn.

- loggedIn -> isLoggedIn
  Now the method for checking logged in status is named the same
  way in both Auth and AuthHelpers.  This also differenciates it
  from the event "loggedIn".
parent 26d23734
Loading
Loading
Loading
Loading
+16 −10
Original line number Diff line number Diff line
@@ -44,7 +44,7 @@ Ext.define('Docs.controller.Auth', {
            'authentication': {
                afterrender: function(cmp) {
                    cmp.el.addListener('click', function(e, el) {
                        cmp.showLogin();
                        cmp.showLoginForm();
                    }, this, {
                        preventDefault: true,
                        delegate: '.login'
@@ -57,16 +57,18 @@ Ext.define('Docs.controller.Auth', {
                        delegate: '.logout'
                    });

                    this.getSession();
                    this.retrieveSession();
                }
            }
        });
    },

    /**
     * Checks if a user is logged in server side and sets up a local session if they are.
     * Checks if a user is logged in server side and sets up a local
     * session if they are.
     * @private
     */
    getSession: function() {
    retrieveSession: function() {
        Ext.Ajax.request({
            url: Docs.baseUrl + '/session',
            params: { sid: this.sid },
@@ -77,9 +79,9 @@ Ext.define('Docs.controller.Auth', {
                    this.currentUser = JSON.parse(response.responseText);
                    this.fireEvent('available');
                    if (this.currentUser) {
                        this.loggedIn();
                        this.setLoggedIn();
                    } else {
                        this.loggedOut();
                        this.setLoggedOut();
                    }
                }
            },
@@ -93,6 +95,7 @@ Ext.define('Docs.controller.Auth', {
     * @param {String} password
     * @param {Boolean} remember
     * @param {Ext.Element} submitEl
     * @private
     */
    login: function(username, password, remember, submitEl) {
        Ext.Ajax.request({
@@ -108,7 +111,7 @@ Ext.define('Docs.controller.Auth', {
                if (data.success) {
                    this.currentUser = data;
                    this.setSid(data.sessionID, { remember: remember });
                    this.loggedIn();
                    this.setLoggedIn();
                } else {
                    if (this.errorTip) {
                        this.errorTip.update(data.reason);
@@ -130,6 +133,7 @@ Ext.define('Docs.controller.Auth', {

    /**
     * Logs out a user
     * @private
     */
    logout: function() {
        Ext.Ajax.request({
@@ -137,7 +141,7 @@ Ext.define('Docs.controller.Auth', {
            method: 'POST',
            cors: true,
            callback: function(){
                this.loggedOut();
                this.setLoggedOut();
            },
            scope: this
        });
@@ -145,8 +149,9 @@ Ext.define('Docs.controller.Auth', {

    /**
     * Marks the user as logged in.
     * @private
     */
    loggedIn: function() {
    setLoggedIn: function() {
        if (this.currentUser) {
            this.getAuth().showLoggedIn(this.currentUser.userName);
            this.fireEvent('loggedIn');
@@ -155,8 +160,9 @@ Ext.define('Docs.controller.Auth', {

    /**
     * Marks a user as logged out.
     * @private
     */
    loggedOut: function(user) {
    setLoggedOut: function(user) {
        this.currentUser = {};
        this.setSid(null);
        this.getAuth().showLoggedOut();
+4 −1
Original line number Diff line number Diff line
@@ -29,7 +29,10 @@ Ext.define('Docs.controller.AuthHelpers', {
        return url + (url.match(/\?/) ? '&' : '?') + 'sid=' + sid;
    },

    loggedIn: function() {
    /**
     * Returns true if user is logged in.
     */
    isLoggedIn: function() {
        return this.getController('Auth').isLoggedIn();
    }

+7 −7
Original line number Diff line number Diff line
@@ -187,7 +187,7 @@ Ext.define('Docs.controller.Comments', {
    },

    postComment: function(cmp, el) {
        if (!this.loggedIn()) {
        if (!this.isLoggedIn()) {
            return false;
        }

@@ -299,7 +299,7 @@ Ext.define('Docs.controller.Comments', {
     * Sends a delete comment request to the server.
     */
    deleteComment: function(cmp, el) {
        if (!this.loggedIn()) {
        if (!this.isLoggedIn()) {
            return;
        }

@@ -332,7 +332,7 @@ Ext.define('Docs.controller.Comments', {
     * Sends an undo request to the server.
     */
    undoDeleteComment: function(cmp, el) {
        if (!this.loggedIn()) {
        if (!this.isLoggedIn()) {
            return;
        }

@@ -394,7 +394,7 @@ Ext.define('Docs.controller.Comments', {
    },

    updateComment: function(cmp, el) {
        if (!this.loggedIn()) {
        if (!this.isLoggedIn()) {
            return false;
        }

@@ -479,7 +479,7 @@ Ext.define('Docs.controller.Comments', {
     * @private
     */
    vote: function(direction, el) {
        if (!this.loggedIn()) {
        if (!this.isLoggedIn()) {
            this.notify('Please login to vote on this comment', el);
            return false;
        }
@@ -623,7 +623,7 @@ Ext.define('Docs.controller.Comments', {
            comments.addCls('hideCommentForm');
        } else if (!comments.hasCls('hideCommentForm')) {
            var commentWrap = comments.down('.new-comment-wrap');
            if (this.loggedIn()) {
            if (this.isLoggedIn()) {

                var formData = Ext.apply(this.getController('Auth').currentUser, {
                    userSubscribed: Docs.commentSubscriptions[id]
@@ -644,7 +644,7 @@ Ext.define('Docs.controller.Comments', {
    },

    toggleNewComment: function(cmp, el) {
        if (!this.loggedIn()) {
        if (!this.isLoggedIn()) {
            return;
        }

+12 −2
Original line number Diff line number Diff line
@@ -21,15 +21,25 @@ Ext.define('Docs.view.auth.Login', {
        this.callParent(arguments);
    },

    showLogin: function() {
    /**
     * Shows login form.
     */
    showLoginForm: function() {
        this.update(this.loginTpl.apply());
    },

    /**
     * Shows message about who's logged in.
     * @param {String} username
     */
    showLoggedIn: function(username) {
        this.update('Welcome, ' + username + ' | <a href="#" class="logout">Logout</a>');
    },

    showLoggedOut: function(username) {
    /**
     * Shows message about being currently logged out.
     */
    showLoggedOut: function() {
        this.update('<a href="#" class="login">Sign in / Register</a>');
    }