Angularjs - 如果用户停用帐户,则为一次性页面

时间:2015-08-23 19:46:57

标签: angularjs

我想实现一项功能,如果用户停用他/她的帐户,它会将其重定向到一次性页面,表明他们的帐户已停用。如果用户删除了帐户,我希望它将用户路由到www.mybusiness.com/account/deactivated,但如果用户输入该帐户,我也不希望将此路由定向到。

我想知道他实现这一目标的最佳方式。或者说明如何一次显示页面的来源。

这是我的代码:

routes.js

.when('/:username/settings', {
        title:'Resonance Inn',
        controller: 'ProfileSettingsController',
        controllerAs: 'vm',
        templateUrl: '/static/javascripts/profiles/settings.html'
})
    .when('/:username/settings/confirm_deactivation', { //After user deletes account off this page, redirect to page for one time view showing your account has been deactivated
        title: 'Resonance Inn - We would hate to see you go...',
        controller: 'ProfileSettingsController',
        controllerAs: 'vm',
        templateUrl: '/static/javascripts/delete-account/delete-account.html' 
}).otherwise('/');

ProfileSettingsController.js

(function () {
'use strict';

angular
    .module ('app.profiles.controllers')
    .controller('ProfileSettingsController', ProfileSettingsController);

ProfileSettingsController.$inject = ['$location', '$routeParams', 'Authentication', 'Profile', 'Snackbar'];

function ProfileSettingsController($location, $routeParams, Authentication, Profile, Snackbar) {
    var vm = this;

    vm.destroy = destroy;
    vm.update = update;

    activate();

    function activate() {
        var authenticatedAccount = Authentication.getAuthenticatedAccount();
        var username = $routeParams.username.substr(1);

        if (!authenticatedAccount) {
            $location.url('/');
            Snackbar.error('You are not authorized to view this page.');
        }
        else {
            if(authenticatedAccount.username !== username) {
                $location.url('/');
                Snackbar.error('You are not Authorized to view this page.');
            }
        }

        Profile.get(username).then(profileSuccessFn, profileErrorFn);

        function profileSuccessFn(data, status, headers, config) {
            vm.profile = data.data; 
        }

        function profileErrorFn(data, status, headers, config) {
            $location.url('/');
            Snackbar.error('That user does not exist.');
        }
    }

    function destroy() {
        Profile.destroy(vm.profile.username).then(profileSuccessFn, profileErrorFn);

        function profileSuccessFn(data, status, headers, config) {
            Authentication.unauthenticate();
            window.location = '/'; // I want user to be redirected to one time page...But how can I make it so you can only view it once?
            Snackbar.show('Your account has been deleted.');
        }

        function profileErrorFn(data, status, headers, config) {
            Snackbar.error(data.error);
        }
    }

    function update() {
        Profile.update(vm.profile).then(profileSuccessFn, profileErrorFn);

        function profileSuccessFn(data, status, headers, config) {
            Snackbar.show('Your profile has been updated!');
        }

        function profileErrorFn(data, status, headers, config) {
            Snackbar.error(data.error);
        }
    }
  }
})();

0 个答案:

没有答案