Advertisement
Guest User

Untitled

a guest
Jan 12th, 2017
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. 'use strict';
  2.  
  3. angular.module('angularJwtauthenticationApp')
  4. .controller('RegisterCtrl',['authToken','$scope','$rootScope','$http','alert', function (authToken,$scope,$rootScope, $http, alert) {
  5.  
  6.  
  7.  
  8. $scope.submit = function(){
  9.  
  10. var url ='http://localhost:3000/register';
  11. var user = {
  12. email : $scope.email,
  13. password : $scope.password
  14. };
  15.  
  16. $http.post(url, user)
  17. .then(function(res){
  18. alert('success', 'Account Created !', 'Welcome, ' + res.user.email + '!');
  19. authToken.setToken(res.token);
  20. })
  21. .catch(function(err){
  22. alert('warning', 'oops', 'Couldn't register please Try again !')
  23. console.log(err);
  24. console.log(email);
  25. });
  26. }
  27. }]);
  28.  
  29. <input name="email" ng-model="email" type="email" class="form-control" placeholder="Email address" required>
  30.  
  31. var express = require('express');
  32. var bodyParser = require('body-parser');
  33. var mongoose = require('mongoose');
  34. var User = require('./models/User.js');
  35. var jwt = require('./services/jwt.js');
  36. var app = express();
  37.  
  38.  
  39. app.use(bodyParser.json());
  40. app.use(function (req, res, next) {
  41. res.header('Access-Control-Allow-Origin', '*');
  42. res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
  43. res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
  44.  
  45. next();
  46. });
  47.  
  48. app.post('/register', function (req, res){
  49. var user = req.body;
  50.  
  51. var newUser = new User.model({
  52. email : user.email,
  53. password : user.password
  54. })
  55.  
  56.  
  57. var payload = {
  58. iss:req.hostname,
  59. sub:user._id
  60. }
  61. var token = jwt.encode(payload, "shhh..")
  62.  
  63. newUser.save(function(err) {
  64. res.status(200).send({
  65. user : newUser.toJSON(),
  66. token : token
  67. });
  68. })
  69.  
  70. });
  71.  
  72. mongoose.Promise = global.Promise;
  73. mongoose.connect('mongodb://localhost/AngularJWTAuthentication');
  74. var server = app.listen(3000, function() {
  75. console.log('api listening on : ',server.address().port);
  76. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement