Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const BASE_URL = 'https://console.kinvey.com/apps/ads-39358/environments/kid_B1T1TDv4X/data/collection-settings/ads?section=permissions';
- const APP_KEY = 'kid_B1T1TDv4X';
- const APP_SECRET = 'b742efcadd0d4dd983c515fdf275ec81';
- const AUTH_HEADERS = {'Authorization': "Basic " + btoa(APP_KEY + ":" + APP_SECRET)};
- function startApp() {
- showHideMenuLinks();
- showView('viewHome');
- // attachAllEvents();
- }
- function showView(viewName) {
- $('main > section').hide();
- $('#' + viewName).show();
- }
- function showHideMenuLinks() {
- $('#linkHome').show();
- if (localStorage.getItem('authToken') === null){
- $('#linkLogin').show();
- $('#linkRegister').show();
- $('#linkHome').hide();
- $('#linkListAds').hide();
- $('#linkCreateAd').hide();
- $('#linkLogout').hide();
- $('#linkLogin').on('click', showLoginView);
- $('#linkRegister').on('click', showRegisterView);
- } else {
- $('#linkLogin').hide();
- $('#linkRegister').hide();
- $('#linkHome').show();
- $('#linkListAds').show();
- $('#linkCreateAd').show();
- $('#linkLogout').show();
- $('#loggedInUser').show().text("Welcome, " + localStorage.getItem('username') + "!");
- }
- }
- function showLoginView() {
- showView('viewLogin');
- $('#formLogin').trigger('reset');
- }
- function showRegisterView() {
- showView('viewRegister');
- $('#formRegister').trigger('reset')
- }
- function showHomeView() {
- console.log('showHomeView');
- showView('viewHome');
- }
- $('#linkHome').on('click', showHomeView);
- $('#buttonRegisterUser').on('click', registerUser);
- $('#buttonLoginUser').on('click', function () {
- let username = $('#formLogin input[name="username"]').val();
- let password = $('#formLogin input[name="passwd"]').val();
- $.ajax({
- method: 'POST',
- url: BASE_URL + 'user/' + APP_KEY + '/login',
- headers: AUTH_HEADERS,
- data: {username, password}
- }).then(function (res) {
- signInUser(res, 'Login successful.')
- }).catch(handleAjaxError)
- });
- function registerUser() {
- console.log('here');
- let username = $('#formRegister input[name="username"]').val()
- let password = $('#formRegister input[name="passwd"]').val()
- $.ajax({
- method: "POST",
- url: BASE_URL + 'user/' + APP_KEY + '/',
- headers: AUTH_HEADERS,
- data: {username, password}
- }).then(function (res) {
- signInUser(res, 'Registration successful.')
- }).catch(handleAjaxError)
- }
- function signInUser(res, message) {
- saveAuthInSession(res);
- showHideMenuLinks();
- showHomeView();
- $('#loggedInUser').text("Hello " + res.username + "!");
- showInfo(message)
- }
- function saveAuthInSession(userInfo) {
- localStorage.setItem('authToken', userInfo._kmd.authtoken);
- localStorage.setItem('username', userInfo.username);
- localStorage.setItem('userId', userInfo._id);
- }
- function handleAjaxError(response) {
- let errorMsg = JSON.stringify(response);
- if (response.readyState === 0)
- errorMsg = "Cannot connect due to network error.";
- if (response.responseJSON && response.responseJSON.description)
- errorMsg = response.responseJSON.description;
- showError(errorMsg)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement