A little necromancy update for the above JS for 2016 and nodebb 1.2.1:
<script>
$(window).on('action:ajaxify.end', function(event, data) {
if (!app.user.uid && ajaxify.currentPage !== 'login' && ajaxify.currentPage !== 'register') {
ajaxify.go('login');
}
});
</script>