我是ReactNative移动应用程序开发的新手,在互联网上浪费了太多时间来解决问题。
基本上,我正在尝试在NodeJS API中实现Google和Facebook身份验证,其中ReactNative Mobile App是将使用服务的客户端应用程序。
如果我使用从Google Auth Playground生成的令牌,NodeJS API身份验证测试将很好地进行。 ReactNative移动应用程序进行身份验证并提供用户的信息和令牌。但是,如果我在NodeJS API中使用从ReactNative应用获取的令牌,那么我会遇到Legacy People API问题。
我实现API和移动APP的步骤如下:
A)API身份验证:用于API身份验证的软件包passport
,passport-google-plus-token
护照配置
// Google oAuth token strategy
passport.use('google-token',new GoogleTokenStrategy(
{ clientID: gci,clientSecret: gcs,},async (accessToken,refreshToken,profile,done) => {
User.upsertGoogleUser(accessToken,function(err,user) {
return done(err,user);
})
}
));
用户模型
userSchema.statics.upsertGoogleUser = function(
accessToken,cb
) {
var that = this;
return this.findOne(
{ "googleProvider.id": profile.id,user) {
// no user was found,let's create a new one
if (!user) {
var newUser = new that({
method: 'googleProvider',email: profile.emails[0].value,googleProvider: { id: profile.id }
});
newUser.save(function(error,savedUser) {
if (error) {
console.log(error);
}
return cb(error,savedUser);
});
} else {
return cb(err,user);
}
}
)
}
B)移动应用程序:用于APP身份验证的软件包expo-google-app-auth
const signIn = async () => {
try {
const { type,accessToken,user } = await Google.logInAsync({
androidClientId: <Google-API-Client-Id-for android app>,scopes: ["profile","email"]
});
if (type === "success") {
console.log(user,accessToken);
} else {
console.log("cancelled")
}
} catch (e) {
console.log("error",e)
}
};
我在Google开发人员控制台的同一项目下使用了单独的google客户端ID ,其中一个用于移动应用程序,另一个用于NodeJS API。
请帮助我犯错误的部分。
谢谢