Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function startApp() {
- //clear user auth data
- sessionStorage.clear()
- showHideMenuLinks()
- showView('viewHome')
- //bind navigation menu links
- $('#linkHome').click(showHomeView)
- $('#linkLogin').click(showLoginView)
- $('#linkRegister').click(showRegisterView)
- $('#linkListAds').click(listAds)
- $('#linkCreateAd').click(showCreateAdView)
- $('#linkLogout').click(logoutUser)
- //bind the form submit buttons
- $('#formLogin').submit(loginUser)
- $('#formRegister').submit(registerUser)
- $('#buttonCreateAd').click(createAd)
- $('#buttonEditAd').click(editAd)
- //bind the info/error boxes: hide on click
- $('#infoBox, #errorBox').click(function () {
- $(this).fadeOut()
- })
- // Attach AJAX "loading" event listener
- $(document).on({
- ajaxStart: function () {
- $("#loadingBox").show()
- },
- ajaxStop: function () {
- $("#loadingBox").hide()
- }
- });
- const kinveyBaseUrl = "https://baas.kinvey.com/";
- const kinveyAppKey = "kid_HJbWZ-hMe";
- const kinveyAppSecret = "2230ec6d6ff848a3b9f0c65ae46f7b09";
- const kinveyAppAuthHeaders = {
- 'Authorization': "Basic " + btoa(kinveyAppKey + ":" + kinveyAppSecret),
- 'Content-type': "application/json"
- };
- function showHideMenuLinks() {
- $('#menu a').hide()
- if (sessionStorage.getItem('authToken')) {
- // setUsernameInLink()
- //logged in user
- $('#linkHome').show()
- $('#linkListAds').show()
- $('#linkCreateAd').show()
- $('#linkLogout').show()
- $('#loggedInUser').show()
- } else {
- //no user logged in
- $('#linkHome').show()
- $('#linkLogin').show()
- $('#linkRegister').show()
- }
- }
- function showView(viewName) {
- // Hide all views and show the selected view only
- $('main > section').hide();
- $('#' + viewName).show();
- }
- function showHomeView(){
- showView('viewHome')
- }
- function showLoginView(){
- showView('viewLogin')
- $('#formLogin').trigger('reset')
- }
- function showRegisterView(){
- showView('viewRegister')
- $('#formRegister').trigger('reset')
- }
- function registerUser(event){
- event.preventDefault()
- let data = {
- username: $('#formRegister input[name=username]').val(),
- password: $('#formRegister input[name=passwd]').val()
- }
- $.ajax({
- method: "POST",
- url: kinveyBaseUrl + 'user/' + kinveyAppKey + '/',
- headers: kinveyAppAuthHeaders,
- data: JSON.stringify(data),
- success: registerUserSuccess,
- error: handleAjaxError
- })
- function registerUserSuccess(userInfo){
- saveAuthInSession(userInfo)
- showHideMenuLinks()
- listAds()
- showInfo('User registration successful.')
- }
- }
- function saveAuthInSession(userInfo){
- sessionStorage.setItem('username', userInfo.username)
- sessionStorage.setItem('authToken', userInfo._kmd.authtoken)
- sessionStorage.setItem('userId', userInfo._id)
- $('#loggedInUSer').text('Welcome, ' + userInfo.username)
- }
- function showInfo(msg){
- $('#infoBox').text(msg)
- setTimeout(function () {
- $('#infoBox').fadeOut()
- }, 3000)
- }
- function handleAjaxError(error){
- $('#errorBox').text('Error: ' + error)
- }
- function listAds(){
- }
- function showCreateAdView(){
- }
- function logoutUser(){
- }
- function loginUser(){
- }
- function createAd(){
- }
- function editAd(){
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement