所以最近我遇到了一个问题,当我尝试从移动设备使用 google oauth2 进行身份验证时会发生这种情况,但是当我尝试通过计算机进行身份验证时,它工作正常。我在 node/express 项目中将它与passportjs一起使用。

项目链接:https://rhubarb-tart-18821.herokuapp.com/

你们可以在这里看到代码:
https://ide.c9.io/saijax/www

只是整个身份验证太大了,我不能把所有东西都放在这里......

编辑:

这是一些主要文件

Passport .js

const GoogleStrategy = require("passport-google-oauth2")
.Strategy;
const mongoose = require("mongoose");
const keys = require("./keys");
module.exports = (passport) => {
passport.use(
    new GoogleStrategy({
          clientID: keys.googleClientID,
          clientSecret: keys.googleClientSecret,
          callbackURL: "/auth/google/callback",
          proxy: true
    }, (accessToken, refreshToken, profile, done) => {
    const image = profile.photos[0].value.substring(0, profile.photos[0].value.indexOf("?"));

    const newUser = {
        googleID: profile.id,
        email: profile.emails[0].value,
        firstName: profile.name.givenName,
        lastName: profile.name.familyName,
        image: image
    }
    // CHECK FOR USER
    User.findOne({
        googleID: profile.id
    }).then(user => {
        if(user){
            done(null, user);
        } else {
        // CREATE USER
            new User(newUser)
                .save()
                .then(user => done(null,user));
        }
    })
    })
);

passport.serializeUser((user, done) => {
    done(null, user.id);
});
passport.deserializeUser((id, done) => {
    User.findById(id)
        .then(user => done(null, user));
});

}

认证.js
const express = require("express");
const passport = require("passport");
const router = express.Router();
router.get("/google", passport.authenticate("google", {
    scope: [
    "profile",
    "email"
    ]
}));
router.get("/google/callback", passport.authenticate("google", {
    failureRedirect: "/"
}), (req, res) => {
    req.flash("success_msg", "Successfully Logged In");
    res.redirect("/dashboard");
});
router.get("/verify", (req, res) => {
    if(req.user){
        console.log(req.user);
    } else {
        console.log("Not auth");
    }
});
router.get('/logout', (req, res) => {
    req.logout();
    req.flash("success_msg", "Successfully Logged Out");
    res.redirect('/');
});

module.exports = router;

应用程序.js
// SETUP
const express = require("express");
const mongoose = require("mongoose");
const passport = require("passport");
const cookieParser = require("cookie-parser");
const session = require("express-session");
const exphbs = require("express-handlebars");
const bodyParser = require("body-parser");
const methodOverride = require("method-override");
const flash = require("connect-flash");

// LOAD GOOGLE AND MONGO KEYS
const keys = require("./config/keys");

// LOAD MODELS
require("./models/story");
require("./models/user");

// PASSPORT CONFIG
require("./config/passport")(passport);

// LOAD ROUTES
const auth = require("./routes/auth");
const index = require("./routes/index");
const stories = require("./routes/stories");

// HANLEBARS HELPERS
const {
  truncate,
  stripTags,
  formatDate,
  select,
  editIcon
} = require("./helpers/hbs");

// MONGOOSE CONNECT
mongoose.connect(keys.mongoURI)
  .then(() => {
    console.log("MongoDB Connected...");
  }).catch(err => console.log(err));

// USE APP
const app = express();

// CSS CONFIG
app.use(express.static(__dirname + "/public"));

// VIEW ENGINE
app.engine("handlebars", exphbs({
  helpers: {
    truncate: truncate,
    stripTags: stripTags,
    formatDate: formatDate,
    select: select,
    editIcon: editIcon
  },
  defaultLayout: "main"
}));
app.set("view engine", "handlebars");

// BODY PARSER
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

// METHOD OVERRIDE
app.use(methodOverride("_method"));

// FLASH
app.use(flash());

// COOKIE PARSER
app.use(cookieParser());

// SESSION MIDDLEWARE
app.use(session({
  secret: "KAPPA",
  resave: false,
  saveUninitialized: false
}));

// PASSPORT MIDDLEWARE
app.use(passport.initialize());
app.use(passport.session());

// SET GLOBAL VARS
app.use((req, res, next) => {
  res.locals.user = req.user || null;
  res.locals.success_msg = req.flash("success_msg");
  res.locals.error_msg = req.flash("error_msg");
  next();
});

// ROUTES
app.use("/", index);
app.use("/auth", auth);
app.use("/stories", stories);

最佳答案

更新:好吧,我不知道这是不是愚蠢的错误,但似乎手机无法读取
callbackURL: "/auth/google/callback"
相反,我改变了以下行
callbackURL: keys.callback + "/auth/google/callback"
其中,keys.callback 是我的应用程序的 url(来自 c9/heroku)......现在它完美无缺!

关于javascript - Google OAuth2 不适用于移动设备,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49635518/

10-13 01:03