/
app.js
62 lines (55 loc) · 1.79 KB
/
app.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
var app = angular.module("App", ["tokenAuthResource", "ngMockE2E"])
.value("tokenAuthParams", {
sessionDuration: 0,
idleTime: 0,
accessTokenKey: "accessToken",
jwt: false,
refreshToken: true,
refreshTokenRequest: {
url: "/refreshToken",
method: "POST",
data: function(refreshToken) {
return {
refresh_token: refreshToken
}
}
}
});
/* Backend mock */
app.run(function($httpBackend) {
var refreshToken = "refreshIt";
$httpBackend.whenPOST("/login").respond(200, {access_token: "a1", expires_in: 75, refresh_token: refreshToken});
$httpBackend.whenPOST("/refreshToken", {refresh_token: refreshToken}).respond(200, {access_token: "b2", refresh_token: refreshToken});
$httpBackend.whenGET("/privatePosts").respond(401, {error: "You are not authorized to view the posts."});
$httpBackend.whenGET("/privatePosts?accessToken=a1").respond(200, [{body: "This is a private post."}, {body: "This is another."}, {body: "etc."}]);
$httpBackend.whenGET("/privatePosts?accessToken=b2").respond(200, [{body: "Token successfully refreshed"}]);
});
/* - */
app.factory("privatePosts", function(tokenAuthResource) {
return tokenAuthResource("/privatePosts");
});
app.controller("MainCtrl", function($scope, $http, privatePosts, sessionHandler) {
function queryPosts() {
$scope.error = "";
$scope.posts = [];
privatePosts.query(function(data) {
$scope.posts = data;
}, function(data) {
$scope.error = data.data.error;
});
}
$scope.login = function() {
$http({
method: "POST",
url: "/login"
}).success(function(data) {
sessionHandler.setAccessToken(data);
queryPosts();
});
}
$scope.logout = function() {
sessionHandler.clean();
queryPosts();
}
queryPosts();
});