Google OAuth2不支持移动设备

时间:2018-04-03 17:01:44

标签: javascript node.js authentication passport.js passport-google-oauth

所以最近我遇到了一个问题,当我尝试使用google oauth2从移动设备进行身份验证时,会发生这种问题,但是当我尝试通过计算机进行身份验证时,它运行正常。我在节点/快递项目中使用它与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));
});

}

auth.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;

app.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);

1 个答案:

答案 0 :(得分:1)

更新:好吧,我不知道这是不是傻了,但似乎移动无法阅读

callbackURL: "/auth/google/callback"

而是用

更改了以下行

callbackURL: keys.callback + "/auth/google/callback"

其中keys.callback是我的应用程序的网址(来自c9 / heroku)...现在它完美无缺!