简体   繁体   English

在Node / Express中登录Passport JS后没有刷新页面并搜索吗?

[英]No refresh page and search after Passport JS login in Node/Express?

I am making a venues search app using the Yelp API and the MEAN stack. 我正在使用Yelp API和MEAN堆栈制作场所搜索应用程序。 You can add and remove yourself from going to a venue, after you login with Facebook using Passport JS. 使用Passport JS登录到Facebook后,您可以添加自己和将自己移出场所。

In the case a user entered a search and the results are shown on the page (as an unauthenticated user), you login and are redirected to the homepage, the search results are refreshed. 如果用户输入了搜索并且结果显示在页面上(以未经身份验证的用户身份),则您登录并重定向到主页后,将刷新搜索结果。

What I want is that the user does not have to search again. 我想要的是用户不必再次搜索。 So basically no refresh of the page OR no refresh of $scope.venues in Angular that is an object holding all the venues data. 因此,基本上没有刷新页面,也没有刷新Angular中的$ scope.venues,该对象是保存所有场所数据的对象。

For example: 例如:

  • I search for Amsterdam 我搜寻阿姆斯特丹
  • I get a list of all the venues in Amsterdam 我得到了阿姆斯特丹所有场馆的清单
  • I login 我登录
  • The search results of Amsterdam are still there 阿姆斯特丹的搜索结果仍然存在

How can I do that? 我怎样才能做到这一点?

These are the Passport JS routes in Node/Express: 这些是Node / Express中的Passport JS路由:

var passport = require('passport');

var FacebookStrategy = require('passport-facebook').Strategy;

var session = require('express-session');

passport.serializeUser(function(user, done) {

done(null, user);

});

passport.deserializeUser(function(id, done) {

done(null, id);

});

app.get('/auth/facebook', passport.authenticate('facebook'));
//Authenticate with Passport when hitting this route

app.get('/auth/facebook/callback', passport.authenticate('facebook', {
//Handle callback after successfully authenticated woth Facebook  

successRedirect: '#/reload/' + searchQuery,
failureRedirect: '/error'

}));

This is what I have in Angular: 这是我在Angular中拥有的:

angular.module("popperooApp", ['ngRoute'])
  .config(function($routeProvider) {
    $routeProvider
      .when("/", {
        templateUrl: "list.html",
        controller: "ListController"
      }).when("/reload/:query", {

        templateUrl: "list.html",
        controller: "ReloadController"

    })
      .otherwise({

        redirectTo: "/"

      });
  })
  .service("Venues", function($http) {

    this.getVenues = function(location) {

      var url = "search/" + location;

      return $http.get(url);

    };


  })
  .controller('ListController', function($scope, Venues) {

    $scope.searchLocation = function(location) {

      Venues.getVenues(location)
        .then(function(response) {

          $scope.venues = response.data;

        }, function(response) {

          alert("Error retrieving venues");

          console.log(response);

        });

    };

  })
  .controller('ReloadController', function($scope,Venues,$routeParams){

        Venues.getVenues($routeParams.query)
            .then(function(response){

                $scope.venues = response.data;

            }, function(response){

               alert("Error retrieving venues"); 

               console.log(response);

            });


})

Should I store sessions in the MongoDB database? 我应该将会话存储在MongoDB数据库中吗? Should I not redirect to the '/' route in Angular and make something custom? 我是否不应该重定向到Angular中的“ /”路由并进行自定义?

Thanks for the help! 谢谢您的帮助!

You should redirect to a search URL like you have already implemented indeed, no need to store the info on the session. 您应该重定向到搜索URL,就像您确实已经实现了一样,而无需在会话中存储信息。

I think the best way in your case is to redirect the user to the page that is currently loaded, so it does not apply only to searches. 我认为您的最佳方法是将用户重定向到当前加载的页面,因此它不仅适用于搜索。

It, of course, assumes that you can access your app via this URL and that the app will automatically reload the search results based on the URL. 当然,它假定您可以通过此URL访问应用程序,并且该应用程序将基于URL自动重新加载搜索结果。

Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM