Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- app.factory("FlashService", function($rootScope) {
- return {
- show: function(message) {
- $rootScope.flash = message;
- },
- clear: function() {
- $rootScope.flash = "";
- }
- }
- });
- app.factory("SessionService", function() {
- return {
- get: function(key) {
- return sessionStorage.getItem(key);
- },
- set: function(key, val) {
- return sessionStorage.setItem(key, val);
- },
- unset: function(key) {
- return sessionStorage.removeItem(key);
- }
- }
- });
- app.factory("AuthenticationService", function($http, $sanitize, $rootScope, SessionService, theUser, FlashService, CSRF_TOKEN) {
- var cacheSession = function() {
- SessionService.set('authenticated', true);
- SessionService.set('username', 'terje');
- };
- var uncacheSession = function() {
- SessionService.unset('authenticated');
- SessionService.unset('username');
- };
- var loginError = function(response) {
- FlashService.show(response.flash);
- };
- var sanitizeCredentials = function(credentials) {
- return {
- email: $sanitize(credentials.email),
- password: $sanitize(credentials.password),
- csrf_token: CSRF_TOKEN
- };
- };
- return {
- login: function(credentials) {
- var login = $http.post("auth/login", sanitizeCredentials(credentials)).success(function(data) {
- theUser = data;
- $rootScope.theUser = theUser;
- // console.log(theUser);
- });
- login.success(cacheSession);
- login.success(FlashService.clear);
- login.error(loginError);
- return login;
- },
- logout: function() {
- var logout = $http.get("auth/logout");
- logout.success(uncacheSession);
- theUser = [];
- delete $rootScope.theUser;
- return logout;
- },
- isLoggedIn: function() {
- return SessionService.get('authenticated');
- }
- };
- });
- app.controller("LogoutController", function($scope, $location, AuthenticationService) {
- console.log("Logged out");
- AuthenticationService.logout().success(function() {
- $location.path('/test');
- });
- });
- app.controller("LoginController", function($scope, $location, AuthenticationService) {
- $scope.credentials = { email: "", password: "" };
- $scope.login = function() {
- AuthenticationService.login($scope.credentials).success(function() {
- $location.path('/test');
- });
- };
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement