Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AngularJS SPA example of using a Session model with Authenticator service
/**
* AngularJS SPA Sample using Authenticator service with Session model
*
* @author Thomas Burleson
*
*/
(function( angular ){
"use strict";
/**
* Register classes and modules with AngularJS
*/
angular.module('myApp.Authentication' , ['ngCookies'])
.value( 'session' , Session())
.factory( 'authenticator' , [ 'session', '$http', '$log', Authenticator] )
.controller( 'SessionController', [ 'session', '$cookieStore' , SessionController]);
angular.module( 'myApp', [ 'myApp.Authentication' ]);
// **********************************************************
// Application Classes
// **********************************************************
/**
* Create a controller-independent instance of model data
* @returns {{sessionID: string, user: {email: string, password: string}}}
* @constructor
*/
function Session()
{
var session;
// Return session instance (model data only)
return session = {
sessionID: "",
user: {
email: "",
password: ""
},
/**
* Simple check to see if we have a valid SessionID
* @return {Boolean}
*/
isLoggedIn : function() {
return (session.sessionID != null);
}
};
}
/**
* Place SessionController on root DIV with ng-app
*
* @param session
* @param $cookieStore
* @constructor
*/
function SessionController( session, $cookieStore )
{
// Inject functionality
session.saveUser = saveUser();
// **********************************************************
// Internal Methods
// **********************************************************
function loadLastUser()
{
// Get currentUser from cookie
session.user = $cookieStore.get('user') || null;
$cookieStore.remove('user');
}
function saveUser(email, password)
{
session.user.email = email;
session.user.password = password;
$cookieStore.put('user', session.user );
}
}
/**
* Authenticator service to login/logout users
* @param session
* @param $http
* @param $log
* @returns {{login: loginUser, logout: logoutUser}}
* @constructor
*/
function Authenticator( session, $http, $log )
{
var URLS = {
LOGIN : "authenticateUser?&email={email}&password={password}",
LOGOUT: "clearSession"
};
return {
login : loginUser,
logout: logoutUser
};
/**
* Authenticate user
* @return {Promise}
*/
function loginUser(email, password)
{
session.sessionID = null; // clear current sessionID
return $http.get( supplant( URLS.LOGIN, {
email:email,
password:password
}) )
.then( onAuthenticated )
.catch( onRejectedRequest );
// **********************************************************
// Internal handlers
// **********************************************************
function onAuthenticated( results )
{
session.sessionID = results.sessionID;
session.saveUser( email, password );
return session.user;
}
}
/**
* Unauthenticate user
*
* @return {Promise}
*/
function logoutUser()
{
return $http.get( URLS.LOGOUT)
.then(function(){
session.sessionID = null;
return true;
})
.catch( onRejectedRequest) ;
}
/**
* Shared rejection handler... Simply log the error
* and rethrow...
* @param fault
*/
function onRejectedRequest( fault )
{
$log.debug( fault.reason );
throw fault; // rethrow to continue propagation
}
}
})( window.angular );
@ThomasBurleson

This comment has been minimized.

Copy link
Owner Author

@ThomasBurleson ThomasBurleson commented May 16, 2014

For completeness the supplant() is a global function that you must load before your application code: supplant.js

@anilsingh581

This comment has been minimized.

Copy link

@anilsingh581 anilsingh581 commented Mar 3, 2015

Awesome post!!

It might help for AngularJs
http://www.code-sample.com/2014/09/angularjs-documentation.html

@krinaAkhani

This comment has been minimized.

Copy link

@krinaAkhani krinaAkhani commented Jan 5, 2017

what about sessionController? where is the code for taht?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.