Advertisement
Guest User

aham

a guest
Jul 26th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const BASE_URL = 'https://console.kinvey.com/apps/ads-39358/environments/kid_B1T1TDv4X/data/collection-settings/ads?section=permissions';
  2. const APP_KEY = 'kid_B1T1TDv4X';
  3. const APP_SECRET = 'b742efcadd0d4dd983c515fdf275ec81';
  4. const AUTH_HEADERS = {'Authorization': "Basic " + btoa(APP_KEY + ":" + APP_SECRET)};
  5.  
  6. function startApp() {
  7.     showHideMenuLinks();
  8.     showView('viewHome');
  9.     // attachAllEvents();
  10. }
  11.  
  12. function showView(viewName) {
  13.     $('main > section').hide();
  14.     $('#' + viewName).show();
  15. }
  16.  
  17. function showHideMenuLinks() {
  18.     $('#linkHome').show();
  19.     if (localStorage.getItem('authToken') === null){
  20.         $('#linkLogin').show();
  21.         $('#linkRegister').show();
  22.         $('#linkHome').hide();
  23.         $('#linkListAds').hide();
  24.         $('#linkCreateAd').hide();
  25.         $('#linkLogout').hide();
  26.         $('#linkLogin').on('click', showLoginView);
  27.         $('#linkRegister').on('click', showRegisterView);
  28.     } else {
  29.         $('#linkLogin').hide();
  30.         $('#linkRegister').hide();
  31.         $('#linkHome').show();
  32.         $('#linkListAds').show();
  33.         $('#linkCreateAd').show();
  34.         $('#linkLogout').show();
  35.         $('#loggedInUser').show().text("Welcome, " + localStorage.getItem('username') + "!");
  36.     }
  37. }
  38.  
  39. function showLoginView() {
  40.     showView('viewLogin');
  41.     $('#formLogin').trigger('reset');
  42. }
  43.  
  44. function showRegisterView() {
  45.     showView('viewRegister');
  46.     $('#formRegister').trigger('reset')
  47. }
  48.  
  49. function showHomeView() {
  50.     console.log('showHomeView');
  51.     showView('viewHome');
  52. }
  53.  
  54. $('#linkHome').on('click', showHomeView);
  55.  
  56. $('#buttonRegisterUser').on('click', registerUser);
  57.  
  58. $('#buttonLoginUser').on('click', function () {
  59.     let username = $('#formLogin input[name="username"]').val();
  60.     let password = $('#formLogin input[name="passwd"]').val();
  61.     $.ajax({
  62.         method: 'POST',
  63.         url: BASE_URL + 'user/' + APP_KEY + '/login',
  64.         headers: AUTH_HEADERS,
  65.         data: {username, password}
  66.     }).then(function (res) {
  67.         signInUser(res, 'Login successful.')
  68.     }).catch(handleAjaxError)
  69. });
  70.  
  71. function registerUser() {
  72.     console.log('here');
  73.     let username = $('#formRegister input[name="username"]').val()
  74.     let password = $('#formRegister input[name="passwd"]').val()
  75.  
  76.     $.ajax({
  77.         method: "POST",
  78.         url: BASE_URL + 'user/' + APP_KEY + '/',
  79.         headers: AUTH_HEADERS,
  80.         data: {username, password}
  81.     }).then(function (res) {
  82.         signInUser(res, 'Registration successful.')
  83.     }).catch(handleAjaxError)
  84. }
  85.  
  86. function signInUser(res, message) {
  87.     saveAuthInSession(res);
  88.     showHideMenuLinks();
  89.     showHomeView();
  90.     $('#loggedInUser').text("Hello " + res.username + "!");
  91.     showInfo(message)
  92. }
  93.  
  94. function saveAuthInSession(userInfo) {
  95.     localStorage.setItem('authToken', userInfo._kmd.authtoken);
  96.     localStorage.setItem('username', userInfo.username);
  97.     localStorage.setItem('userId', userInfo._id);
  98. }
  99.  
  100. function handleAjaxError(response) {
  101.     let errorMsg = JSON.stringify(response);
  102.     if (response.readyState === 0)
  103.         errorMsg = "Cannot connect due to network error.";
  104.     if (response.responseJSON && response.responseJSON.description)
  105.         errorMsg = response.responseJSON.description;
  106.     showError(errorMsg)
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement