Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 

81 righe
2.3 KiB

  1. /**
  2. * Created by Benny on 30.03.2016.
  3. */
  4. var app = app || {};
  5. app.ctrl = app.ctrl|| {};
  6. app.ctrl.Auth = {
  7. // index eq. login
  8. index : function( params )
  9. {
  10. if ( true === app.core.User.isLoggedIn() )
  11. {
  12. app.core.Controller.redirect( 'dashboard' );
  13. return;
  14. }
  15. app.core.View.showLogin();
  16. $( 'button[data-id="btn-login"]').click( function()
  17. {
  18. app.core.Rest.post(
  19. 'session',
  20. {
  21. credential : app.core.View.$body.find( 'input[name="email"]').val(),
  22. password : app.core.View.$body.find( 'input[name="password"]').val()
  23. },
  24. function( data )
  25. {
  26. if ( _.isObject( data ) && data.hasOwnProperty( 'session_token' ) )
  27. {
  28. app.core.G.set( 'sessionId', data.session_token );
  29. app.core.View.hideLogin();
  30. app.core.Controller.redirect( 'dashboard' );
  31. }
  32. else
  33. {
  34. app.gui.Toast.show( 'Something went wrong. Please try again later.', app.gui.Toast.TYPE_ERROR );
  35. }
  36. },
  37. function( xhr, res )
  38. {
  39. console.log( xhr );
  40. switch ( xhr.status )
  41. {
  42. case 400:
  43. app.gui.Toast.show( 'Invalid credentials', app.gui.Toast.TYPE_ERROR );
  44. break;
  45. default:
  46. app.gui.Toast.show( 'Login failed. Please try again later.', app.gui.Toast.TYPE_ERROR );
  47. }
  48. }
  49. );
  50. return false;
  51. });
  52. }
  53. ,
  54. logout : function( params )
  55. {
  56. app.core.User.logout();
  57. app.core.Rest.delete(
  58. 'session',
  59. function( res )
  60. {
  61. app.core.Controller.redirect( 'auth' );
  62. }
  63. ,
  64. function()
  65. {
  66. app.core.Controller.redirect( 'auth' );
  67. }
  68. );
  69. }
  70. };