使用resolve和middleware进行角度ui.router使用(用户身份验证问题)

时间:2017-07-06 12:53:22

标签: angularjs angular-ui-router

我在将用户登录时遇到问题。我想我缺少基本的知识(我是棱角分明的新手),非常感谢你的深刻解释。

我想检查用户是否已连接,如果是,则将其作为变量返回。 我无法想象我错过了什么。有人可以提供帮助吗?

也 当我想注销用户时 - 我可以向服务器发送获取请求吗?或者这样做的最佳做法是什么?

app.js(客户端):

var app = angular.module('DIYApp', ['ui.router']);

app.config(($stateProvider, $urlRouterProvider, $locationProvider) => {

   function CheckForAuthenticatedUser(ParseService, $state) {
       return ParseService.getCurrentUser().then(function (user) {
           // if resolved successfully return a user object that will set
           // the variable `resolvedUser`
           return user;
       }, function (err) {
           $state.go('login');
       })
   }

$locationProvider.html5Mode(true);
$urlRouterProvider.otherwise('/home');
$stateProvider
  .state('home', {
    url: '/home',
    templateUrl: 'views/partial-homepage.html',
    controller:  'homepageController'
  })
  .state('about', {
    url:'/about',
    templateUrl: 'views/partial-about.html'
  })
  .state('show', {
    url: '/show',
    templateUrl: 'views/partial-show.html',
    controller:  'showController'
  })
  .state('logout', {
    url: '/logout',
    templateUrl:'',
    controller: function($state, req) {
      req.signOut();
    },
    resolve : {
      resolvedUser : CheckForAuthenticatedUser
      }
    });

});

server.js(服务器端):

//package and module requirements, configuarion and server starting
const request               = require('request'),
      cheerio               = require('cheerio'),
      express               = require('express'),
      expressSession        = require('express-session'),
      passport              = require('passport'),
      passportLocal         = require('passport-local'),
      passportLocalMongoose = require('passport-local-mongoose'),
      bodyParser           = require('body-parser'),
      mongoose              = require('mongoose'),
      User                  = require("./models/user.js");
      app                   = express(),

app.use(express.static('public'));
app.use(express.static('node_modules'));
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
app.use(expressSession({
  secret:"secretString",
  resave:false,
  saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
passport.use(new passportLocal(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost/myData');

// --->routes
app.post('/login', passport.authenticate("local",{
  successRedirect: "show",
  failureRedirect: "home"
}), function(req, res){
});

app.post('/register',function(req, res){
  User.register(new User({username: req.body.username}), req.body.password, function(err, user){
    if (err){
      console.log(err);
       return res.send('oppppps-reg');
    }
    passport.authenticate("local")(req,res,function(){
            res.redirect("homeIprovmentDIY");
    });
  });
});


app.listen(8000, function() {
  console.log("Starting server - port 8000")
});

app.all('*', function(req, res) {
  res.sendFile(__dirname + "/public/index.html");
});

感谢!!!!!

0 个答案:

没有答案