-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAuthService.js
More file actions
48 lines (43 loc) · 1.45 KB
/
AuthService.js
File metadata and controls
48 lines (43 loc) · 1.45 KB
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
/**
* Created by Sahil Jain on 16/08/2014.
*/
var app = angular.module("mainApp");
app.service('Auth', ['API', '$cookieStore', '$location', function (API, $cookieStore, $location) {
var token = "";
var firstName = "";
this.storeToken = function (token) {
$cookieStore.put('token', token);
};
this.storeFirstName = function (firstName) {
$cookieStore.put('firstName', firstName);
};
this.setToken = function (username, password, done) {
API.getToken(username, password).success(function (data) {
token = data.token;
console.log(token);
$cookieStore.put('token', token);
API.getUser(token).success(function (data) {
firstName = data.firstName;
$cookieStore.put('firstName', firstName);
return done();
}).error(function (data) {
$cookieStore.remove('token');
$cookieStore.remove('firstName');
return done(data.error);
});
}).error(function (data) {
return done(data.error);
});
};
this.getToken = function () {
return $cookieStore.get('token') || "";
};
this.getFirstName = function () {
return $cookieStore.get('firstName') || "";
};
this.logout = function () {
$cookieStore.remove('token');
$cookieStore.remove('firstName');
$location.path("/login");
};
}]);