I've setup a signup and login system using passport.js. And Im using Angular.js in the front-end. But when I use angular it does not signup my users. Without angular it works well. See code below:
Without Angular Code (It works):
<form action="/signup" method="post">
<div class="form-group">
<label>Email</label>
<input type="text" class="form-control" name="email">
</div>
<div class="form-group">
<label>Password</label>
<input type="password" class="form-control" name="password">
</div>
<button type="submit" class="btn btn-warning btn-lg">Signup</button>
</form>
With Angular Code (Does Not Work):
<!doctype html>
<html ng-app="login">
<head>
<title>Node Authentication</title>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular-animate.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular-aria.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-material/1.0.4/angular-material.js"></script>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css">
<!-- load bootstrap css -->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css">
<!-- load fontawesome -->
<script src="./javascripts/controllers/signup.js"></script>
<style>
body {
padding-top: 80px;
}
</style>
</head>
<body ng-controller="loginCtrl">
<div class="container">
<div class="col-sm-6 col-sm-offset-3">
<h1><span class="fa fa-sign-in"></span> Signup</h1>
<!-- show any messages that come back with authentication -->
<% if (message.length> 0) { %>
<div class="alert alert-danger">
<%=m essage %>
</div>
<% } %>
<!-- LOGIN FORM -->
<form>
<div class="form-group">
<label>Email</label>
<input type="text" class="form-control" ng-model="user.email">
</div>
<div class="form-group">
<label>Password</label>
<input type="password" class="form-control" ng-model="user.password">
</div>
<button ng-click="signup()" class="btn btn-warning btn-lg">Signup</button>
</form>
<hr>
<p>Already have an account? <a href="/login">Login</a>
</p>
<p>Or go <a href="/">home</a>.</p>
</div>
</div>
Angular Controller Code:
var login = angular.module('login', [])
login.controller('loginCtrl', function($scope,$http){
$scope.user = {};
$scope.signup = function(){
return $http.post('/signup',$scope.user)
.then(function(data) {
console.log("da" + data);
});
};
})
The Signup Route:
// process the signup form
app.post('/signup', passport.authenticate('local-signup', {
successRedirect : '/profile', // redirect to the secure profile section
failureRedirect : '/signup', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}));
Note:
In the backend I've setup a Node/Express.js API. On successful signup it uses passport.js to add the user into MongoDB and also redirects user to the profile page.
BUT currently the data is going into the database (I can see the new user in the db) its not redirecting the user to the profile page nor is it showing any error messages that I send back (i.e if a user already exists)