Last active
February 27, 2016 19:27
-
-
Save mekhami/88ca64516e1954abb3c3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var myApp = angular.module('myApp', ['ngRoute']); | |
| myApp.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) { | |
| $routeProvider. | |
| when('/', { | |
| templateUrl: '../views/codeform.html', | |
| controller: 'MainController', | |
| access: {restricted: false} | |
| }). | |
| when('/confirm/:code', { | |
| templateUrl: '../views/confirm.html', | |
| controller: 'ConfirmController', | |
| access: {restricted: false} | |
| }). | |
| when('/success', { | |
| templateUrl: '../views/success.html', | |
| access: {restricted: false} | |
| }). | |
| when('/error', { | |
| templateUrl: '../views/error.html', | |
| access: {restricted: false} | |
| }). | |
| when('/login', { | |
| templateUrl: '../views/login.html', | |
| controller: 'LoginController', | |
| access: {restricted: false} | |
| }). | |
| when('/logout', { | |
| controller: 'LogoutController', | |
| access: {restricted: false} | |
| }). | |
| when('/admin', { | |
| controller: 'AdminController', | |
| templateUrl: '../views/admin/index.html', | |
| access: {restricted: true} | |
| }). | |
| when('/admin/create', { | |
| controller: 'AdminController', | |
| templateUrl: '../views/admin/create.html', | |
| access: {restricted: true} | |
| }); | |
| $locationProvider.html5Mode(true); | |
| }]) | |
| myApp.run(function($rootScope, $location, $route, AuthService) { | |
| $rootScope.$on('$routeChangeStart', function(event, next, current) { | |
| if (next.access.restricted && AuthService.isLoggedIn()===false) { | |
| $location.path('/login'); | |
| $route.reload(); | |
| } | |
| }); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var express = require('express'); | |
| var router = express.Router(); | |
| var mongoose = require('mongoose'); | |
| var User = require('../models/user.js'); | |
| var passport = require('passport'); | |
| router.post('/register', function(req, res) { | |
| User.register(new User({ username: req.body.username}), req.body.password, function(err, account) { | |
| if (err) { | |
| return res.status(500).json({err:err}); | |
| } | |
| passport.authenticate('local')(req, res, function () { | |
| return res.status(200).json({status: 'Registration successful!'}); | |
| }); | |
| }); | |
| }); | |
| router.post('/login', function(req, res, next) { | |
| passport.authenticate('local', function(err, user, info) { | |
| if (err) { | |
| return res.status(500).json({err: err}) | |
| }; | |
| if (!user) { | |
| return res.status(401).json({err: info}) | |
| }; | |
| req.logIn(user, function(err) { | |
| if (err) { | |
| return res.status(500).json({err: 'Could not log in user'}); | |
| } | |
| res.status(200).json({status: 'Login successful!'}); | |
| }); | |
| })(req, res, next); | |
| }); | |
| router.get('/logout', function(req, res) { | |
| req.logout(); | |
| res.status(200).json({status: 'Bye!'}); | |
| }); | |
| module.exports = router; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| angular.module('myApp') | |
| .factory('rsvpDataService', ['$http', function($http) { | |
| var service = {}; | |
| service.getAllRsvp = function() { | |
| return $http.get('/api/rsvps/'); | |
| }; | |
| service.getRsvp = function(code) { | |
| return $http.get('/api/rsvps/'+code.toUpperCase()); | |
| }; | |
| service.updateRsvp = function(formData) { | |
| return $http.post('/api/rsvps/'+formData.code.toUpperCase(), formData); | |
| } | |
| return service; | |
| }]) | |
| .factory('AuthService', ['$q', '$timeout', '$http', function($q, $timeout, $http) { | |
| var user = null; | |
| function isLoggedIn() { | |
| if (user) { | |
| return true; | |
| } else { | |
| return false; | |
| } | |
| } | |
| function getUserStatus() { | |
| return user; | |
| } | |
| function login(username, password) { | |
| var deferred = $q.defer(); | |
| $http.post('/user/login', {username: username, password: password}) | |
| .success(function(data, status) { | |
| if(status===200 && data.status) { | |
| user = true; | |
| deferred.resolve(); | |
| } else { | |
| user = false; | |
| deferred.reject(); | |
| } | |
| }) | |
| .error(function(data) { | |
| user = false; | |
| deferred.reject(); | |
| }); | |
| return deferred.promise; | |
| } | |
| function logout() { | |
| var deferred = $q.defer(); | |
| $http.get('/user/logout') | |
| .success(function(data) { | |
| user = false; | |
| deferred.resolve(); | |
| }) | |
| .error(function(data) { | |
| user = false; | |
| deferred.reject(); | |
| }); | |
| return deferred.promise; | |
| } | |
| function register(username, password) { | |
| var deferred = $q.defer(); | |
| $http.post('/user/register', {username: username, password: password}) | |
| .success(function(data, status) { | |
| if(status===200 && data.status){ | |
| deferred.resolve(); | |
| } else { | |
| deferred.reject(); | |
| } | |
| }) | |
| .error(function(data) { | |
| deferred.reject(); | |
| }); | |
| return deferred.promimse; | |
| } | |
| return({ | |
| isLoggedIn: isLoggedIn, | |
| getUserStatus: getUserStatus, | |
| login: login, | |
| logout: logout, | |
| register: register | |
| }); | |
| }]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment