Giter VIP home page Giter VIP logo

meanjs-token-auth's Introduction

Stories in Ready Circle CI Dependencies Status

This is a modification to the MEAN stack generated by the Yeoman MEANjs generator version 0.1.12 which was provided by MEANjs.org. This modification adds local token authentication capabilities as an alternative to local cookie based authentication using sessions.

Welcome

Welcome to Whitehall Technologies LLC's open source project adding token based local authentication to the base MEANjs.org deployment of the MEAN.js stack. Please visit our site for more info and follow us on Twitter and GitHub for live updates.

Token Authentication

Token authentication is an alternative to cookie based authentication and is has a number of benefits.

  • Cross-domain / CORS: cookies + CORS don't play well across different domains. A token-based approach allows you to make AJAX calls to any server, on any domain because you use an HTTP header to transmit the user information.

  • Stateless (a.k.a. Server side scalability): there is no need to keep a session store, the token is a self-contanined entity that conveys all the user information. The rest of the state lives in cookies or local storage on the client side.

  • CDN: you can serve all the assets of your app from a CDN (e.g. javascript, HTML, images, etc.), and your server side is just the API.

  • Decoupling: you are not tied to a particular authentication scheme. The token might be generated anywhere, hence your API can be called from anywhere with a single way of authenticating those calls.

  • Mobile ready: when you start working on a native platform (iOS, Android, Windows 8, etc.) cookies are not ideal when consuming a secure API (you have to deal with cookie containers). Adopting a token-based approach simplifies this a lot.

(Taken from Alberto Pose's article)

Build Instructions

This guide assumes you have a MongoDB running locally on port 27017. If not, follow these instructions.

  • Update the local package list and download Git, Node.js, and the Node Package Manager
sudo apt-get update
sudo apt-get install -y git
sudo apt-get install -y nodejs
sudo apt-get install -y npm
  • Create a symbolic link between nodejs and node (some older programs that use node call it through 'node' instead of 'nodejs')
sudo ln -s /usr/bin/nodejs /usr/bin/node
  • Install Grunt Task Runner
npm install -g grunt-cli
  • Clone the project and navigate into it
git clone https://github.com/castlewhitehall/meanjs-with-token-auth.git
cd meanjs-with-token-auth/
  • Install Node packages
npm install

Deployment and Use

  • Run the following to deploy the server using nodemon. If files are changed, then it will automatically restart.
grunt
  • tests
grunt test

Additions and Modifications

Same as https://github.com/castlewhitehall/meanjs-with-token-auth but working

  • Added sign out
Users.sign_out(function(successResponse){
        Authentication.user = null;
        localStorage.removeItem('a');
        delete $http.defaults.headers.common.Authentication;
        // Redirect to signin page
        $location.path('/signin');
    }, function(errorResponse){
        $scope.error = errorResponse.message;
});
  • Socket.js
var socketio = req.app.get('socketio');
socketio.sockets.in(utils.get_id(req.user)).emit('notification', 'Hello');
  • VK-passport

  • Angular side token consumption

request: function(config){
    var auth = localStorage.getItem('a');
    if(auth){
        config.headers.Authentication = auth;
    }
    return config;
}

Credits

Inspired by and built upon the great work of MEANjs.org The MEAN name was coined by Valeri Karpov

License

(The MIT License)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.