Skip to content

Instantly share code, notes, and snippets.

@ThomasBurleson
Created May 16, 2014 16:29
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThomasBurleson/7d8cb63302990e71e146 to your computer and use it in GitHub Desktop.
Save ThomasBurleson/7d8cb63302990e71e146 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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

@anilsingh581
Copy link

Awesome post!!

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

@krinaAkhani
Copy link

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