forked from linnovate/mean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharticles.js
92 lines (80 loc) · 2.95 KB
/
articles.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
'use strict';
//Setting up route
angular.module('mean.articles').config(['$stateProvider', '$urlRouterProvider',
function($stateProvider, $urlRouterProvider) {
//================================================
// Check if the user is connected
//================================================
var checkLoggedin = function($q, $timeout, $http, $location) {
// Initialize a new promise
var deferred = $q.defer();
// Make an AJAX call to check if the user is logged in
$http.get('/loggedin').success(function(user) {
// Authenticated
if (user !== '0')
$timeout(deferred.resolve, 0);
// Not Authenticated
else {
$timeout(function() {
deferred.reject();
}, 0);
$location.url('/login');
}
});
return deferred.promise;
};
//================================================
// Check if the user is not conntect
//================================================
var checkLoggedOut = function($q, $timeout, $http, $location) {
// Initialize a new promise
var deferred = $q.defer();
// Make an AJAX call to check if the user is logged in
$http.get('/loggedin').success(function(user) {
// Authenticated
if (user !== '0') {
$timeout(function() {
deferred.reject();
}, 0);
$location.url('/login');
}
// Not Authenticated
else {
$timeout(deferred.resolve, 0);
}
});
return deferred.promise;
};
//================================================
// states for my app
$stateProvider
.state('all articles', {
url: '/articles',
templateUrl: 'public/articles/views/list.html',
resolve: {
loggedin: checkLoggedin
}
})
.state('create article', {
url: '/articles/create',
templateUrl: 'public/articles/views/create.html',
resolve: {
loggedin: checkLoggedin
}
})
.state('edit article', {
url: '/articles/:articleId/edit',
templateUrl: 'public/articles/views/edit.html',
resolve: {
loggedin: checkLoggedin
}
})
.state('article by id', {
url: '/articles/:articleId',
templateUrl: 'public/articles/views/view.html',
resolve: {
loggedin: checkLoggedin
}
})
}
])