2016-08-03 9 views
0

私は現在、ユーザー認証を必要とするアプリケーションをプログラミングしています。アプリケーションにログインすると、ノードサーバーはユーザーをメインページにリダイレクトする必要があります。残念ながら、私はそれをしようとするときに問題に直面しています。ここでNode.js res.redirectは機能しません

は、Node.jsの中に私のコードです:

//import necessary modules 
 
var path = require("path"); 
 
var http = require("http"); 
 
var express = require("express"); 
 
var session = require("express-session"); 
 

 
var port = 8080; 
 
var app = express(); 
 
var conString = "postgres://postgres:[email protected]:5432/db_invoice_kuga"; 
 

 
/** 
 
* Middleware components. 
 
* The session middleware is used to create sessions for 
 
* user authentication. 
 
* The static middleware is used to serve 
 
* the static file in the frontend folder. 
 
*/ 
 

 
//use sessions for authentication 
 
app.use(session({ 
 
    secret: "2C44-4D44-WppQ38S", 
 
    resave: true, 
 
    saveUninitialized: true 
 
})); 
 

 
//check authentication 
 
app.use(function(req, res, next) { 
 
    if(req.session && req.session.user) { 
 
     next(); 
 
    } else { 
 
     if(req.url.indexOf("/login/") === -1 && req.url.indexOf("/loginPage") === -1) { 
 
      res.redirect("/loginPage"); 
 
     } else { 
 
      next(); 
 
     } 
 
    } 
 
}); 
 

 
//static middleware 
 
app.use(express.static(__dirname + "/frontend")); 
 

 
/** 
 
* Routes define the endpoints for the application. 
 
* There is a specific file for customer routes, invoice routes, ... 
 
*/ 
 

 
//include routes 
 
require("./routes/routesCustomer.js")(app, conString); 
 

 
//login and logout routes 
 
app.get("/loginPage", function(req, res) { 
 
    res.sendFile(path.join(__dirname, "frontend", "login.html")); 
 
}); 
 

 
app.get("/", function(req, res) { 
 
    res.sendFile(path.join(__dirname, "frontend", "index.html")); 
 
}); 
 

 
/** 
 
* Logs the user in. 
 
* @name /login/:username/:password 
 
* @param username (obligatory) 
 
* @param password (obligatory) 
 
*/ 
 
app.get("/login/:username/:password", function(req, res) { 
 
    var username = req.params.username; 
 
    var password = req.params.password; 
 

 
    if(username === "Daniel" && password === "test") { 
 
     req.session.user = "Daniel"; 
 
     res.redirect("/"); 
 
    } else { 
 
     res.status(400).json({ 
 
      "loggedin": false 
 
     }); 
 
    } 
 
}); 
 

 
/** 
 
* Logs the user out. 
 
* @name /logout 
 
*/ 
 
app.get("/logout", function(req, res) { 
 
    req.session.destroy(); 
 
    res.redirect("/loginPage"); 
 
}); 
 

 
/** 
 
* Bind the server to the port and 
 
* start the application. 
 
*/ 
 

 
//create the server and bind it to the port 
 
http.createServer(app).listen(port, function() { 
 
    console.log("Server listening on port " + port); 
 
});

私の問題は、 "/ /ログイン:ユーザー名/:パスワード" ルートです。そこには "res.redirect( '/')"が機能しません。 redirect()は他のルートでうまく動作しますが...

何が間違っている可能性がありますか?おそらく、ユーザー認証を行うためのよりよい方法もあります。私は提案をありがとう:)

ありがとうございます。 ダニエル

+0

「機能しない」とはどういう意味ですか?何が起こるのですか?エラー?ユーザーは '/ loginPage'を終了します。クライアントはサーバーから応答を受け取りますか?いくつかの簡単なデバッグ手順を最初に実行する必要があります。 – jfriend00

+1

偶然、AJAXコール経由で 'login'ルートを利用していますか? - うーん、前に進み、それを仮定してください。あなたはAJAXコールからサーバリダイレクトを行うことはできません - クライアント上で行わなければなりません。 – tymeJV

+0

ありがとうございました。 –

答えて

0

これは私のコードは、今どのように見えるかです:急行-stormpathを使用した場合

/** 
 
* Created by Daniel on 27.07.2016. 
 
* This file contains the setup of the server. 
 
*/ 
 

 
"use strict"; 
 

 
//import necessary modules 
 
var path = require("path"); 
 
var http = require("http"); 
 
var colors = require("colors"); 
 
var express = require("express"); 
 
var stormpath = require("express-stormpath"); 
 

 
var app = express(); 
 
var port = 8080; 
 
var conString = "postgres://postgres:[email protected]:5432/db_invoice_kuga"; 
 

 
console.log("\nSETTING UP THE SERVER...\n" + 
 
"========================\n"); 
 

 
//MIDDLEWARE 
 
//log routes 
 
app.use(function(req, res, next) { 
 
    console.log(colors.yellow(req.method + "\t" + req.url)); 
 
    next(); 
 
}); 
 

 
//configure stormpath 
 
app.use(stormpath.init(app, { 
 
    client: { 
 
     apiKey: { 
 
      file: "./apiKey.properties" 
 
     } 
 
    }, 
 
    web: { 
 
     login: { 
 
      enabled: true 
 
     }, 
 
     logout: { 
 
      enabled: true 
 
     }, 
 
     me: { 
 
      enabled: false 
 
     }, 
 
     oauth2: { 
 
      enabled: false 
 
     }, 
 
     register: { 
 
      enabled: false 
 
     } 
 
    }, 
 
    application: { 
 
     href: "https://api.stormpath.com/v1/applications/onkfEfUjnUWO6qMg4y5J7", 
 
    } 
 
})); 
 

 
//serve static files in frontend folder 
 
app.use(stormpath.loginRequired, express.static(__dirname + "/frontend")); 
 

 
//ROUTES 
 
//include routes 
 
console.log("Including routes..."); 
 
require("./routes/routesCustomer.js")(app, conString); 
 

 
console.log(colors.green("Success.\n")); 
 

 
//BIND APP TO PORT 
 
//create the server and bind app to the port 
 
console.log("Binding application to port..."); 
 
http.createServer(app).listen(port, function() { 
 
    console.log(colors.green("Success. Magic happens on port " + port + "!")); 
 
}); 
 

 
app.on("stormpath.ready", function() { 
 
    console.log(colors.green("Stormpath ready!\n")); 
 
    console.log(colors.cyan(">> Application ready <<\n")); 
 
    console.log(colors.magenta("Requests:\n=========\n")); 
 
});

ログイン/ログアウトルートは、自動的にアプリケーションに追加されます。

関連する問題