Advertisement
Guest User

app.js

a guest
Apr 21st, 2018
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. $(() => {
  2. const app = Sammy('#container', function () {
  3. this.use('Handlebars', 'hbs');
  4.  
  5. this.get('#/home', getWelcomePage);
  6. this.get('index.html', getWelcomePage);
  7. this.post('#/register', (ctx) => {
  8. //console.log('lol');
  9. let username = ctx.params.username;
  10. let password = ctx.params.password;
  11. let repeatPass = ctx.params.passwordRepeat;
  12. if (repeatPass !== password) {
  13. notify.showError('Passwords should match!')
  14. }
  15. else {
  16. auth.register(username, password).then((userData) => {
  17. auth.saveSession(userData);
  18. notify.showInfo("user registration successful");
  19. ctx.redirect('#/home');
  20. })
  21. .catch(notify.handle);
  22.  
  23. }
  24. });
  25.  
  26.  
  27. this.post('#/login', (ctx) => {
  28.  
  29. let username = ctx.params['username-login'];
  30. let password = ctx.params['password-login'];
  31.  
  32. if (password === "") {
  33. notify.showError('Password field should not be empty!')
  34. }
  35. else if (username ===""){
  36. notify.showError('Username field should not be empty!')
  37.  
  38. }
  39. else {
  40. auth.login(username, password)
  41. .then((userData) => {
  42. auth.saveSession(userData);
  43. notify.showInfo('Login successful.');
  44. console.log("lol");
  45. ctx.loadPartials({
  46. header:"./templates/common/header.hbs",
  47. tableFootForm:"./templates/forms/tableFootForm.hbs",
  48. createEntyForm:"./templates/forms/createEntryForm.hbs",
  49.  
  50.  
  51. }).then(
  52. function(){
  53. this.partial("./templates/pages/nonAnnonymousPage.hbs");
  54.  
  55.  
  56. }
  57. )
  58. })
  59.  
  60. .catch(notify.handleError);
  61.  
  62.  
  63.  
  64.  
  65.  
  66. }
  67.  
  68. });
  69.  
  70. function getWelcomePage(ctx){
  71. if(!auth.isAuth()){
  72. ctx.loadPartials({
  73. header:"./templates/common/header.hbs",
  74. footer:"./templates/common/footer.hbs",
  75. loginForm:"./templates/forms/loginForm.hbs",
  76. registerForm:"./templates/forms/registerForm.hbs",
  77.  
  78. }).then(function(){
  79. this.partial('./templates/welcome-anonymous.hbs');
  80. })
  81.  
  82. }
  83. else{
  84. ctx.redirect('#/home');
  85. }
  86.  
  87. }
  88.  
  89. this.get()
  90. function getHome(ctx){
  91.  
  92. }
  93.  
  94.  
  95.  
  96.  
  97. });
  98.  
  99.  
  100. app.run();
  101. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement