/** * Created by Benny on 30.03.2016. */ var app = app || {}; app.ctrl = app.ctrl|| {}; app.ctrl.Auth = { // index eq. login index : function( params ) { if ( true === app.core.User.isLoggedIn() ) { app.core.Controller.redirect( 'dashboard' ); return; } app.core.View.showLogin(); $( 'button[data-id="btn-login"]').click( function() { app.core.Rest.post( 'session', { credential : app.core.View.$body.find( 'input[name="email"]').val(), password : app.core.View.$body.find( 'input[name="password"]').val() }, function( data ) { if ( _.isObject( data ) && data.hasOwnProperty( 'session_token' ) ) { app.core.G.set( 'sessionId', data.session_token ); app.core.View.hideLogin(); app.core.Controller.redirect( 'dashboard' ); } else { app.gui.Toast.show( 'Something went wrong. Please try again later.', app.gui.Toast.TYPE_ERROR ); } }, function( xhr, res ) { console.log( xhr ); switch ( xhr.status ) { case 400: app.gui.Toast.show( 'Invalid credentials', app.gui.Toast.TYPE_ERROR ); break; default: app.gui.Toast.show( 'Login failed. Please try again later.', app.gui.Toast.TYPE_ERROR ); } } ); return false; }); } , logout : function( params ) { app.core.User.logout(); app.core.Rest.delete( 'session', function( res ) { app.core.Controller.redirect( 'auth' ); } , function() { app.core.Controller.redirect( 'auth' ); } ); } };