Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Twitter.factory('AuthService', AuthService);
- AuthService.$inject = ['$cookies', '$http'];
- function AuthService($cookies, $http) {
- var AuthService = {
- getAuthenticatedAccount: getAuthenticatedAccount,
- isAuthenticated: isAuthenticated,
- login: login,
- logout: logout,
- register: register,
- setAuthenticatedAccount: setAuthenticatedAccount,
- unauthenticate: unauthenticate
- };
- return AuthService;
- function getAuthenticatedAccount() {
- if (!$cookies.authenticatedAccount) {
- return;
- }
- return JSON.parse($cookies.authenticatedAccount);
- }
- function isAuthenticated() {
- return !!$cookies.authenticatedAccount;
- }
- function login(email, password) {
- return $http.post('/api/auth/login/', {
- email: email,
- password: password
- }).then(loginSuccessFn, loginErrorFn);
- function loginSuccessFn(data, status, headers, config) {
- AuthService.setAuthenticatedAccount(data.data);
- window.location = '/';
- }
- function loginErrorFn(data, status, headers, config) {
- console.log(data.data);
- }
- }
- function logout() {
- return $http.post('/api/auth/logout/')
- .then(logoutSuccessFn, logoutErrorFn);
- function logoutSuccessFn(data, status, headers, config) {
- AuthService.unauthenticate();
- window.location = '/';
- }
- function logoutErrorFn(data, status, headers, config) {
- alert("Logout error.");
- }
- }
- function register(email, password, username) {
- return $http.post('/api/accounts/', {
- email: email,
- password: password,
- username: username,
- }).then(registerSuccessFn, registerErrorFn);
- function registerSuccessFn(data, status, headers, config) {
- AuthService.login(email, password);
- }
- function registerErrorFn(data, status, headers, config) {
- alert("Registration error.");
- }
- }
- function setAuthenticatedAccount(account) {
- $cookies.authenticatedAccount = JSON.stringify(account);
- }
- function unauthenticate() {
- delete $cookies.authenticatedAccount;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement