-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finalise API and create Angular service
- Loading branch information
1 parent
279f0ad
commit 58dbcf0
Showing
10 changed files
with
169 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,18 @@ | ||
var mongoose = require('mongoose'); | ||
var User = mongoose.model('User'); | ||
|
||
var sendJSONresponse = function(res, status, content) { | ||
res.status(status); | ||
res.json(content); | ||
}; | ||
|
||
|
||
module.exports.profileRead = function(req, res) { | ||
console.log("Reading profile ID: " + req.params.userid); | ||
res.status(200); | ||
res.json({ | ||
"message" : "Profile read: " + req.params.userid | ||
}); | ||
}; | ||
|
||
module.exports.profileUpdate = function(req, res) { | ||
console.log("Updating profile ID: " + req.params.userid); | ||
res.status(200); | ||
res.json({ | ||
"message" : "Profile updated for " + req.params.userid | ||
}); | ||
if (!req.payload._id) { | ||
res.status(401).json({ | ||
"message" : "UnauthorizedError: private profile" | ||
}); | ||
} else { | ||
User | ||
.findById(req.params.userid) | ||
.exec(function(err, user) { | ||
res.status(200).json(user); | ||
}); | ||
} | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
(function () { | ||
|
||
angular | ||
.module('meanApp') | ||
.service('authentication', authentication); | ||
|
||
authentication.$inject = ['$http', '$window']; | ||
function authentication ($http, $window) { | ||
|
||
var saveToken = function (token) { | ||
$window.localStorage['mean-token'] = token; | ||
}; | ||
|
||
var getToken = function () { | ||
return $window.localStorage['mean-token']; | ||
}; | ||
|
||
var isLoggedIn = function() { | ||
var token = getToken(); | ||
var payload; | ||
|
||
if(token){ | ||
payload = token.split('.')[1]; | ||
payload = $window.atob(payload); | ||
payload = JSON.parse(payload); | ||
|
||
return payload.exp > Date.now() / 1000; | ||
} else { | ||
return false; | ||
} | ||
}; | ||
|
||
var currentUser = function() { | ||
if(isLoggedIn()){ | ||
var token = getToken(); | ||
var payload = token.split('.')[1]; | ||
payload = $window.atob(payload); | ||
payload = JSON.parse(payload); | ||
return { | ||
email : payload.email, | ||
name : payload.name | ||
}; | ||
} | ||
}; | ||
|
||
register = function(user) { | ||
return $http.post('/api/register', user).success(function(data){ | ||
saveToken(data.token); | ||
}); | ||
}; | ||
|
||
login = function(user) { | ||
return $http.post('/api/login', user).success(function(data) { | ||
saveToken(data.token); | ||
}); | ||
}; | ||
|
||
logout = function() { | ||
$window.localStorage.removeItem('mean-token'); | ||
}; | ||
|
||
return { | ||
currentUser : currentUser, | ||
saveToken : saveToken, | ||
getToken : getToken, | ||
isLoggedIn : isLoggedIn, | ||
register : register, | ||
login : login, | ||
logout : logout | ||
}; | ||
} | ||
|
||
|
||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.