質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Node.js

Node.jsとはGoogleのV8 JavaScriptエンジンを使用しているサーバーサイドのイベント駆動型プログラムです。

Express

ExpressはNode.jsのWebアプリケーションフレームワークです。 マルチページを構築するための機能セットおよびハイブリッドのWebアプリケーションを提供します。

Q&A

1回答

2021閲覧

express + passport-facebookで、facebook認証を実装

fukurou

総合スコア14

Node.js

Node.jsとはGoogleのV8 JavaScriptエンジンを使用しているサーバーサイドのイベント駆動型プログラムです。

Express

ExpressはNode.jsのWebアプリケーションフレームワークです。 マルチページを構築するための機能セットおよびハイブリッドのWebアプリケーションを提供します。

1グッド

1クリップ

投稿2016/05/28 02:13

node(express利用)のwebサーバを作り、passport-facebookのモジュールを利用して、facebook認証を実装したいです。
passport及びpassport-facebookをインストールした後、
passportの公式ホームページが推奨しているgithubページ(https://github.com/passport/express-4.x-facebook-example)に記載しているserver.jsファイルを作成し、node server.jsで動かしてみましたが、なぜか動かない状態です。

私はプログラミング初心者で、gitHubの使い方も慣れていないため、何がいけないのか困っております。お知恵を御借りできれば幸いです。

このようなエラーがコンソール上で表示されました。。

SyntaxError: Unexpected identifier at exports.runInThisContext (vm.js:53:16) at Module._compile (module.js:404:25) at Object.Module._extensions..js (module.js:432:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:311:12) at Function.Module.runMain (module.js:457:10) at startup (node.js:136:18) at node.js:972:3

以下がnode server.js動かしたサーバ側のコードです。

【server.js】 var express = require('express'); var passport = require('passport'); var Strategy = require('passport-facebook').Strategy; // Configure the Facebook strategy for use by Passport. // // OAuth 2.0-based strategies require a `verify` function which receives the // credential (`accessToken`) for accessing the Facebook API on the user's // behalf, along with the user's profile. The function must invoke `cb` // with a user object, which will be set at `req.user` in route handlers after // authentication. passport.use(new Strategy({ clientID: facebookより取得したものを記載, clientSecret: facebookより取得したものを記載, callbackURL: 'http://【使用しているドメイン記載】:3000/login/facebook/return' }, function(accessToken, refreshToken, profile, cb) { // In this example, the user's Facebook profile is supplied as the user // record. In a production-quality application, the Facebook profile should // be associated with a user record in the application's database, which // allows for account linking and authentication with other identity // providers. return cb(null, profile); })); // Configure Passport authenticated session persistence. // // In order to restore authentication state across HTTP requests, Passport needs // to serialize users into and deserialize users out of the session. In a // production-quality application, this would typically be as simple as // supplying the user ID when serializing, and querying the user record by ID // from the database when deserializing. However, due to the fact that this // example does not have a database, the complete Twitter profile is serialized // and deserialized. passport.serializeUser(function(user, cb) { cb(null, user); }); passport.deserializeUser(function(obj, cb) { cb(null, obj); }); // Create a new Express application. var app = express(); // Configure view engine to render EJS templates. app.set('views', __dirname + '/views'); app.set('view engine', 'ejs'); // Use application-level middleware for common functionality, including // logging, parsing, and session handling. app.use(require('morgan')('combined')); app.use(require('cookie-parser')()); app.use(require('body-parser').urlencoded({ extended: true })); app.use(require('express-session')({ secret: 'keyboard cat', resave: true, saveUninitialized: true })); // Initialize Passport and restore authentication state, if any, from the // session. app.use(passport.initialize()); app.use(passport.session()); // Define routes. app.get('/', function(req, res) { res.render('home', { user: req.user }); }); app.get('/login', function(req, res){ res.render('login'); }); app.get('/login/facebook', passport.authenticate('facebook')); app.get('/login/facebook/return', passport.authenticate('facebook', { failureRedirect: '/login' }), function(req, res) { res.redirect('/'); }); app.get('/profile', require('connect-ensure-login').ensureLoggedIn(), function(req, res){ res.render('profile', { user: req.user }); }); app.listen(3000);

他にもgitHubコードのコピペをするのでなく、Pssport公式ページに書いているコードをもとに、サーバ側(app.js)及びroutes側をいじったりしてみました。それで動かないため、サンプルとしてできがっているコードである上記server.jsを動かそうとしたという背景がございます。

ikuwow👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

github(https://github.com/passport/express-4.x-facebook-example)にあるソースコードはドキュメントどおりの手順で正しく動作します。
シンタックスエラーですので設定ミスなどではなく単純な文法の誤りであると推測されます。

SyntaxError: Unexpected identifier

というエラーは ,(カンマ) をつけ忘れたときによく出るエラーですが、どこかにカンマのつけ忘れがあったり位置がおかしかったりしませんか?

記載のserver.jsを見たところ個人的にはStrategyのコンストラクタ引数部分を書き換えられているようなのでそこが怪しいと思います。

投稿2016/05/30 22:30

nukosuke

総合スコア145

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問