logincontroller.js
angular .module('app.pages.auth.login') .controller('logincontroller', logincontroller); /** @nginject */ function logincontroller($http, $location) { var vm = this; vm.submitpost = function(userdata){ $http({ url: 'http://localhost:7200/api/pages/auth/login', method: 'post', data: userdata }).then(function(res) { if(res.data.success){ $location.path('/pages/profile'); //console.log(res.data.success); //vm.message=res.data.message; } else { //console.log(res.data.message); vm.message=res.data.message; $location.path('/pages/auth/login'); } }, function(error) { console.log(error); alert('here'); }); }; }
route.js
var user = require('../models/user'); var config = require('../config'); var secretkey = config.secretkey; var jsonwebtoken = require('jsonwebtoken'); var validator = require('validator'); var express = require('express'); //var router = express.router(); var passport = require('passport'); var localstrategy = require('passport-local').strategy; var flash = require('connect-flash'); module.exports = function(app, express, passport){ var router = express.router(); passport.use(new localstrategy({ usernamefield: 'username', passwordfield: 'password'}, function(username, password, done) { user.findone({ name : username}, function(err, user) { if (!user){ return done(null, false,{message: 'incorrect username' }); } if(user){ var validpassword = user.comparepassword(password); if(!validpassword){ return done(null, false,{message: 'incorrect password' }); } } return done(null, user); }); } )); router.post('/pages/auth/login', function(req, res, next) { passport.authenticate('local', function(err, user, info) { if (user === false) { console.log("login error "); return res.json({ success:false, message: info.message, }); } else { console.log("login success"); return res.json({ success:true, //message: 'login success', }); } })(req, res, next); }); router.post('/pages/auth/register',function(req, res){ var user = new user({ name: req.body.username, email: req.body.email, password: req.body.password }); user.save(function(err){ if(err){ res.send(err); return; } res.json({ success:true, message: 'user has been created!', }); }); }); return router; }
login.html
<div id="login" class="scrollable" ms-scroll> <div id="login-form-wrapper" layout="row" layout-align="center center"> <div id="login-form" class="md-whiteframe-8dp"> <div class="logo md-accent-bg"> <span>f</span> </div> <div class="title" translate="login.title">log in account</div> <form name="loginform"> <div class="alertmessage" >{{vm.message}}</div> <md-input-container flex md-no-float> <input ng-model="vm.form.username" placeholder="username" translate translate-attr-placeholder="login.username" name="username" required="true"> <div ng-messages="loginform.username.$error" ng-show="loginform.username.$touched"> <div ng-message="required">this field required</div> </div> </md-input-container> <md-input-container flex md-no-float> <input ng-model="vm.form.password" type="password" placeholder="password" translate translate-attr-placeholder="login.password" name="password" required="true"> <div ng-messages="loginform.password.$error" ng-show="loginform.password.$touched"> <div ng-message="required">this field required</div> </div> </md-input-container> <div class="remember-forgot-password" layout="row" layout-sm="column" layout-align="space-between center"> <md-checkbox class="remember-me" ng-model="data.cb1" aria-label="remember me"> <span translate="login.remember_me">remember me</span> </md-checkbox> <a ui-sref="app.pages_auth_forgot-password" class="forgot-password md-accent-color" translate="login.forgot_password">forgot password?</a> </div> <md-button class="md-raised md-accent" aria-label="log in" translate="login.log_in" translate-attr-aria-label="login.log_in" ng-click="vm.submitpost(vm.form);"> log in </md-button> </form> <div class="separator"> <span class="text" translate="login.or">or</span> </div> <md-button class="md-raised google"> <div layout="row" layout-align="center center"> <span> <md-icon md-font-icon="icon-google-plus" class="s16"></md-icon> <span translate="login.with_google">log in google</span> </span> </div> </md-button> <md-button class="md-raised facebook"> <div layout="row" layout-align="center center"> <span> <md-icon md-font-icon="icon-facebook" class="s16"></md-icon> <span translate="login.with_facebook">log in facebook</span> </span> </div> </md-button> <div class="register" layout="row" layout-sm="column" layout-align="center center"> <span class="text" translate="login.dont_have_account">don't have account?</span> <a class="link" ui-sref="app.pages_auth_register" translate="login.create_account">create account</a> </div> </div> </div>
when give http://localhost:7200/api/pages/profile in address bar directly goes page, doesn't asking login authentication. pleas me changes want angularjs authentication.
Comments
Post a Comment