1

PassportJSを使用している認証部分のために、nodeJSでAPIを開発しています。私はこの良いチュートリアルを使用しています: http://scotch.io/tutorials/javascript/easy-node-authentication-setup-and-local

ブラウザでは、ユーザーを取得しても問題ありませんが、Android アプリで試してみると、req.user が定義されていません。

app.post('/login', passport.authenticate('local-login', {
    successRedirect: '/loginSuccess',
    failureRedirect: '/loginFailure',
    failureFlash : true // allow flash messages
}));

app.get('/loginFailure', function(req, res, next) {
    res.setHeader('Content-Type', 'application/json');
    res.json({user: req.user, message: req.flash('loginMessage')[0]});
});

app.get('/loginSuccess', function(req, res, next) {
    console.log(req.user);
    res.setHeader('Content-Type', 'application/json');
    res.json({user: req.user, message: "test"});
});

///

passport.use('local-login', new LocalStrategy({
    usernameField : 'login',
    passwordField : 'password',
    passReqToCallback : true // allows us to pass in the req from our route (lets us check if a user is logged in or not)
},
function(req, login, password, done) {
    // asynchronous
    process.nextTick(function() {
        User.findOne({$or:[{'local.email': login.toLowerCase()}, {'local.login': login}]}, function(err, user) {
            // if there are any errors, return the error
            if (err)
                return done(err);

            // if no user is found, return the message
            if (!user)
                return done(null, false, req.flash('loginMessage', 'No user found.'));

            if (!user.validPassword(password))
                return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.'));

            // all is well, return user
            else
                return done(null, user);
        });
    });

}));

そして私のAndroidコード:

public class LoginActivity extends Activity {

      private Button loginButton = null;
      private Button cancelButton = null;


      private boolean isOnline() {
            ConnectivityManager cm =
                (ConnectivityManager) getSystemService(AppInfo.getAppContext().CONNECTIVITY_SERVICE);
            NetworkInfo netInfo = cm.getActiveNetworkInfo();
            if (netInfo != null && netInfo.isConnectedOrConnecting()) {
                return true;
            }
            return false;
        }

      private OnClickListener clickListenerLoginButton = new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isOnline()) {
                    Toast.makeText(LoginActivity.this, getResources().getString(R.string.ERROR_NO_NETWORK), Toast.LENGTH_LONG).show();
                    return ;
                }
                new LoginOperation().execute(getResources().getString(R.string.urlLogin));
            }
        };

        private OnClickListener clickListenerCancelButton = new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent result = new Intent();
                setResult(RESULT_CANCELED, result);
                finish();
            }
        };

      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        loginButton = (Button)findViewById(R.id.loginButton);
        cancelButton = (Button)findViewById(R.id.cancelButton);

        loginButton.setOnClickListener(clickListenerLoginButton);
        cancelButton.setOnClickListener(clickListenerCancelButton);
      }

      private Object user;

      private class LoginOperation  extends AsyncTask<String, Void, Void> {

        String Response = "";
        String Error = null;
        String data ="";
        private ProgressDialog Dialog = new ProgressDialog(LoginActivity.this);
        EditText login_emailEditText = (EditText) findViewById(R.id.login_emailEditText);
        EditText passwordEditText = (EditText) findViewById(R.id.passwordEditText);

        protected void onPreExecute() {

            Dialog.setMessage("Please wait..");
            Dialog.show();

            try{
                data += "&"+URLEncoder.encode("login", "UTF-8")+ "=" +URLEncoder.encode(login_emailEditText.getText().toString(), "UTF-8");
                data += "&"+URLEncoder.encode("password", "UTF-8")+ "=" +URLEncoder.encode(passwordEditText.getText().toString(), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

        }

        @Override
        protected Void doInBackground(String... urls) {
            BufferedReader reader=null;
                try
                {
                   URL url = new URL(urls[0]);
                  // Send POST data request
                  URLConnection conn = url.openConnection();
                  conn.setDoOutput(true);
                  OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
                  wr.write( data );
                  wr.flush();

                  // Get the server response
                  reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                  StringBuilder sb = new StringBuilder();
                  String line = null;

                    // Read Server Response
                    while((line = reader.readLine()) != null) {
                        sb.append(line + " ");
                    }

                    // Append Server Response To Content String
                   Response = sb.toString();
                }
                catch(Exception ex) {
                    Error = ex.getMessage();
                }
                finally {
                    try {
                        reader.close();
                    }
                    catch(Exception ex) {}
                }
            return null;
        }

        protected void onPostExecute(Void unused) {
            Dialog.dismiss();

            if (Error != null) {
                Toast.makeText(LoginActivity.this, "ErrorAndroid: " + Error, Toast.LENGTH_LONG).show();
            } else {
                try {
                    JSONObject jsonObject = new JSONObject(Response);
                    Toast.makeText(LoginActivity.this, jsonObject.toString(), Toast.LENGTH_LONG).show();
                    user = jsonObject.get("user");
                } catch (JSONException e) {
                    Toast.makeText(LoginActivity.this, "ErrorAndroidJSON: " + e.getMessage(), Toast.LENGTH_LONG).show();
                }

             }
        }

      }
}

そして、ブラウザで試してみる: user1/password1

http://example.com:4040/login

私はexpress4.0を使用していますが、express3.8でも試しました。ブラウザでは機能するのにアプリでは機能しない理由がまったくわかりません。

編集: Android で API を呼び出す方法に問題があると思います。http ヘッダー要求はブラウザーによって異なる必要があるため、うまく機能していません。

ご協力いただきありがとうございます。

4

1 に答える 1