Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const kinveyAppID = 'kid_BkSPwnsH';
- const kinveyAppSecret = '1292427a4d324036a96b3c93256da065';
- const kinveyServiceUrl = 'https://baas.kinvey.com/';
- function showView(viewId) {
- $("main > section").hide();
- $("#" + viewId).show();
- }
- function showHideNavigationLinks() {
- let loggedIn = sessionStorage.authToken != null;
- if (loggedIn) {
- $("#linkLogin").hide();
- $("#linkRegister").hide();
- $("#linkListBooks").show();
- $("#linkCreateBook").show();
- $("#linkLogout").show();
- } else {
- $("#linkLogin").show();
- $("#linkRegister").show();
- $("#linkListBooks").hide();
- $("#linkCreateBook").hide();
- $("#linkLogout").hide();
- }
- }
- function showHomeView() {
- showView('viewHome');
- }
- function showLoginView() {
- showView('viewLogin');
- }
- function login() {
- let authBase64 = btoa(kinveyAppID + ":" + kinveyAppSecret);
- let loginUrl = kinveyServiceUrl + "user/" + kinveyAppID + "/login";
- let loginData = {
- username: $("#loginUser").val(),
- password: $("#loginPass").val()
- };
- $.ajax({
- method: "POST",
- url: loginUrl,
- data: loginData,
- headers: { "Authorization": "Basic " + authBase64 },
- success: loginSuccess,
- error: showAjaxError
- });
- function loginSuccess(data, status) {
- sessionStorage.authToken = data._kmd.authtoken;
- showListBooksView();
- showHideNavigationLinks();
- showInfo("Login successful");
- }
- }
- function showInfo(messageText) {
- $("#infoBox").text(messageText).show().delay(3000).fadeOut() ;
- }
- function showAjaxError(data, status) {
- let errorMsg = "Error:" + JSON.stringify(data);
- $('#errorBox').text(errorMsg).show();
- }
- function showRegisterView() {
- showView('viewRegister');
- }
- function register() {
- let authBase64 = btoa(kinveyAppID + ":" + kinveyAppSecret);
- let loginUrl = kinveyServiceUrl + "user/" + kinveyAppID + "/";
- let loginData = {
- username: $("#registerUser").val(),
- password: $("#registerPass").val(),
- };
- $.ajax ({
- method:"POST",
- url: loginUrl,
- data: loginData,
- headers: { "Authorization": "Basic " + authBase64 },
- success: registerSuccess,
- error: showAjaxError
- });
- function registerSuccess(data, status) {
- sessionStorage.authToken = data._kmd.authtoken;
- showListBooksView();
- showHideNavigationLinks();
- showInfo("User registered successfullly");
- }
- }
- function showListBooksView() {
- showView('viewListBooks');
- $("#books").text('');
- let booksUrl = kinveyServiceUrl + "appdata/" + kinveyAppID + "/books-library";
- let authHeaders = { "Authorization": "Kinvey " + sessionStorage.authToken
- };
- $.ajax({
- method: "GET",
- url: booksUrl,
- headers: authHeaders,
- success: booksLoaded,
- error: showAjaxError,
- });
- function booksLoaded(books, status) {
- showInfo("Books loaded.");
- let booksTable = $("<table>")
- .append($("<tr>")
- .append($('<th>Title</th>'))
- .append($('<th>Author</th>'))
- .append($('<th>Description</th>'))
- );
- for (let book of books){
- booksTable.append($("<tr>")
- .append($('<td></td>').text(book.title))
- .append($('<td></td>').text(book.author))
- .append($('<td></td>').text(book.description))
- );
- }
- $("#books").append(booksTable);//video time point: 2:12:22
- }
- }
- function showCreateBookView() {
- showView('viewCreateBook');
- }
- function createBook() {
- $("#createBook").text('');
- let booksUrl = kinveyServiceUrl + "appdata/" + kinveyAppID + "/books-library";
- let authHeaders = {
- "Authorization": "Kinvey " + sessionStorage.authToken
- };
- let newBookData = {
- title: $("#bookTitle").val(),
- author: $("#bookAuthor").val(),
- description: $("#bookDescription").val(),
- };
- $.ajax({
- method: "POST",
- url: booksUrl,
- data: newBookData,
- headers: authHeaders,
- success: bookCreated,
- error: showAjaxError
- });
- function bookCreated(data) {
- showListBooksView();
- showInfo("Books created.");
- }
- }
- function logout() {
- sessionStorage.clear();
- showHideNavigationLinks();
- }
- function showInfo(messageText) {
- $("#infoBox").text(messageText).show().delay(3000).fadeOut() ;
- }
- $(function () {
- $("#linkHome").click(showHomeView);
- $("#linkLogin").click(showLoginView);
- $("#linkRegister").click(showRegisterView);
- $("#linkListBooks").click(showListBooksView);
- $("#linkCreateBook").click(showCreateBookView);
- $("#linkLogout").click(logout);
- $("#formLogin").submit(function(e) {e.preventDefault(); login()});
- $("#formRegister").submit(function(e) {e.preventDefault(); register()});
- $("#formCreateBook").submit(function(e) {e.preventDefault(); createBook()});
- showHomeView();
- showHideNavigationLinks();
- $(document)
- .ajaxStart(function() {
- $("#loadingBox").show();
- })
- .ajaxStop(function() {
- $("#loadingBox").hide();
- })
- });
Add Comment
Please, Sign In to add comment