Ionic modal return promise after close

I have a login modal that opens up every time an unlogged user navigates to a page that requires login.

angular.module('petSpot', ['ionic', 'ngCordova', 'ngMaterial', 'ngMessages', 'ngTouch', 'ui.bootstrap', 'petSpot.controllers', 'petSpot.directives', ''])

.run(function ($ionicPlatform, $cordovaStatusbar, $rootScope, $state, $ionicModal, Session, loginModal) {
    $ionicPlatform.ready(function() {
        //Build login modal
        $ionicModal.fromTemplateUrl('templates/login.html', {
            scope: $rootScope
        }).then(function (modal) {
            $rootScope.loginModal = modal;

        $rootScope.$on('$stateChangeStart', function (event, toState, toParams) {
            var requireLogin =;

            if (requireLogin && typeof $rootScope.currentUser === 'undefined') {

                  .then(function () {
                      return $state.go(, toParams);
                  .catch(function () {
                      // do nothing

I want that If the modal closes without the user logging in, a rejected promise will be returned and the navigation will not be approved. If the user successfuly logs in, I want the modal to close and to return a resolved promise.

This is my login modal service:

    .service('loginModal', function ($ionicModal, $rootScope, $q, $resource) {

        function assignCurrentUser (user) {
            $rootScope.currentUser = user;
            return user;

        return function () {
            var defer = $q.defer();

            $ () { // THIS IS NO GOOD
            .catch(function () {
            return defer.promise;


Any help would be very appreciated!