【发布时间】:2021-03-05 18:18:02
【问题描述】:
这个问题有很多问题,我都阅读了,但没有看到任何类似的问题。在我的情况下,“auth”是前置的。这是我在应用设置中注册为回调 url
http://localhost:4500/auth/google/callback
这是 passport.js 配置:
passport.use(
new GoogleStrategy.Strategy(
{
clientID: process.env.GOOGLE_CLIENT_ID!,// "!" is typescript
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
callbackURL: "auth/google/callback",
},
async (accessToken, refreshToken, profile, done) => {
const existingUser = await User.findOne({ googleId: profile.id });
if (existingUser) {
done(undefined, existingUser);
}
const user = await new User({ googleId: profile.id }).save();
done(undefined, user);
}
)
);
路线如下:
export const authRoutes = (app: Application) => {
//with passing "google" passport knows that it will use GoogleStrategy
app.get(
"/auth/google",
passport.authenticate("google", { scope: ["profile", "email"] })
);
app.get("/auth/google/callback", passport.authenticate("google"));
app.get("/auth/current_user", (req: Request, res: Response) => {
res.send(req.user);
});
app.get("/auth/logout", (req: Request, res: Response) => {
req.logout();
res.json({ user: req.user });
});
};
这是错误信息:
错误 400:redirect_uri_mismatch 请求中的重定向 URI http://localhost:4500/auth/auth/google/callback 与授权给 OAuth 客户端的不匹配。要更新授权的重定向 URI,请访问:https://console.developers.google.com/apis/credentials/oauthclient/${your_client_id}?project=${your_project_number}
【问题讨论】:
-
你能添加整个错误信息吗? (标题只有一半的错误信息,没用)
-
@LawrenceCherone 没有感叹号。我没有将回调 url 设置为 env。只有数据库、客户端 ID 和客户端密码
-
"localhost:4500/auth/auth/google/callback" 为什么这个有 2 个
auth部分? -
@LawrenceCherone 这是打字稿。抱歉,我应该提到这一点。我会更新问题
-
@Dilshan 这就是问题所在。我不明白谷歌会返回“/auth/auth/google/callback”
标签: node.js google-api google-oauth passport.js google-api-nodejs-client