Created
April 16, 2016 22:31
-
-
Save AlphaNerd/46b0325e59edbaeaa7c22f5e966c4539 to your computer and use it in GitHub Desktop.
Simple mockup of Angularjs user authentication service/factory
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
.factory('$authservice', ['$window', '$localstorage', '$state', function($window, $localstorage, $state) { | |
// Access local storage for existing user info | |
var SHIFT = $localstorage.getObject("Shift") | |
// Does session exist? | |
if (SHIFT && SHIFT.session.token) { | |
console.log("$authservice: Existing Session found: ", [SHIFT]) | |
} else { | |
console.log("$authservice: No session found.") | |
SHIFT = { | |
session: { | |
token: null | |
}, | |
user: { | |
isAuthenticated: false | |
}, | |
} | |
$localstorage.setObject("Shift", SHIFT); | |
// $state.go("login"); | |
} | |
return { | |
isAuthenticated: function() { | |
if (SHIFT.user.isAuthenticated) { | |
// console.log("User is authenticated.") | |
return true; | |
} else { | |
// console.log("User is NOT authenticated.") | |
return false; | |
} | |
}, | |
login: function(username, password) { | |
if (username === "a@a.com" && password === "111") { | |
SHIFT = { | |
session: { | |
token: "6677v7b8vddtvdd8s7dff6sf5d74fdf76v9cv7ds98xf76d8sx5f67s8d9cuc89ds67d8", | |
expire: "Jan 1, 2017" | |
}, | |
user: { | |
isAuthenticated: true | |
}, | |
} | |
$localstorage.setObject("Shift", SHIFT); | |
return SHIFT; | |
} else { | |
return false; | |
} | |
}, | |
logout: function() { | |
SHIFT = { | |
session: { | |
token: null | |
}, | |
user: { | |
isAuthenticated: false | |
}, | |
} | |
$localstorage.setObject("Shift", SHIFT); | |
$state.go("login"); | |
} | |
} | |
}]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment