Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. var Client = function Client() {
  2. this.authClient = null;
  3.  
  4. this._setupAuth();
  5. this._setupEventHandlers();
  6. }
  7.  
  8. Client.prototype._login = function _login() {
  9. var self = this;
  10.  
  11. $.ajax({
  12. url: "/login",
  13. method: "POST",
  14. data: {
  15. credentials: {
  16. username: 'user@user.com',
  17. password: '1234'
  18. }
  19. },
  20. statusCode: {
  21. 200: function(response) {
  22. if (response.accessToken === undefined) {
  23. alert('Something went wrong');
  24. } else {
  25. self.authClient.login(response.accessToken, response.accessTokenExpiration);
  26. }
  27. },
  28. 401: function() {
  29. alert('Login failed');
  30. }
  31. }
  32. });
  33. }
  34.  
  35. Client.prototype._logout = function _logout() {
  36. this.authClient.logout();
  37. }
  38.  
  39. Client.prototype._request = function _request() {
  40. var resource = $.ajax({
  41. url: "/api/resource",
  42. statusCode: {
  43. 400: function() {
  44. alert('Since we did not send an access token we get client error');
  45. },
  46. 401: function() {
  47. alert('You are not authenticated, if a refresh token is present will attempt to refresh access token');
  48. }
  49. }
  50. })
  51. .done(function(data) {
  52. alert(JSON.stringify(data));
  53. });
  54. }
  55.  
  56. Client.prototype._setupAuth = function _setupAuth() {
  57. var self = this;
  58.  
  59. this.authClient = new jqOAuth({
  60. events: {
  61. login: function() {
  62. alert("You are now authenticated.");
  63. },
  64. logout: function() {
  65. alert("You are now logged out.");
  66. },
  67. tokenExpiration: function() {
  68. return $.post("/refresh-token").success(function(response){
  69. self.authClient.setAccessToken(response.accessToken, response.accessTokenExpiration);
  70. });
  71. }
  72. }
  73. });
  74. }
  75.  
  76. Client.prototype._setupEventHandlers = function _setupEventHandlers() {
  77. $("#login").click(this._login.bind(this));
  78. $("#request").click(this._request.bind(this));
  79. $("#logout").click(this._logout.bind(this));
  80. }
  81.  
  82. $(document).ready(function() {
  83. var client = new Client;
  84. });
  85.  
  86. var myApp = new Framework7();
  87. var $$ = Dom7;
  88.  
  89. var mainView = myApp.addView('.view-main');
  90.  
  91. myApp.onPageInit('login-screen', function (page) {
  92. var pageContainer = $$(page.container);
  93. pageContainer.find('.list-button').on('click', function () {
  94. var username = pageContainer.find('input[name="username"]').val();
  95. var password = pageContainer.find('input[name="password"]').val();
  96. // Handle username and password
  97. myApp.alert('Username: ' + username + ', Password: ' + password, function () {
  98. mainView.goBack();
  99. });
  100. });
  101. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement