如何在 Node.js 后端使用 Passport.js 实现刷新令牌流?
How to implement refresh token flow with Passport.js on Node.js backend?
正如标题所说;我想知道如何在 Passport.js.
中实现令牌的检索
目前我实施了以下策略:
passport.use(new GoogleStrategy({
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: '/api/auth/google/callback'
},
async (accessToken, refreshToken, profile, done) => {
const newUser = {
googleId: profile.id,
displayName: profile.displayName,
firstName: profile.name.givenName,
lastName: profile.name.familyName,
image: profile.photos[0].value,
email: profile.emails[0].value
};
try {
let user = await User.findOne({ googleId: profile.id });
if(user) {
done(null, user);
} else {
user = await User.create(newUser);
await Key.create({
googleId: user.googleId,
keyProvider: 'Google',
token: accessToken,
refreshToken: refreshToken
});
done(null, user);
}
} catch (err) {
console.error(err);
}
}));
现在,例如,我想调用 Google API,让我们说这条路线:
router.get('/list', async (req, res) => {
console.log('list called');
const result = await axios({
method: "GET",
headers: {
'Authorization': "Bearer [token which is stored in my db]",
},
"Content-Type": "application/json",
url: 'https://www.googleapis.com/youtube/v3/subscriptions?channelId=[someid]',
});
console.log(result);
});
我将在何处以及如何实现 function/middleware/module,每次我向 Google API 发出请求时都会调用它,并在出错然后重试调用?
因为我是 Node 的新手。js/Express 我不知道如何将这样的流程放入代码中。我知道要实现的唯一选择是使用 axios 捕获错误,然后在每个 Google API 端点中手动检索带有冗余代码的新 accessToken。
我想说这里有两个主要概念:
设计模式
用新的访问令牌重试 401 是标准的,正如您所说 - 这里有一些 axios code of mine。
AXIOS 规格
Axios 有一个概念interceptors and here is an example that does 401 retries。
个人喜好
两者都可以 - 我更喜欢我的方法,它曾经被称为 service agent
模式。重要的因素是:
- 使调用 API 的业务逻辑代码漂亮干净
- 请记住,还有其他可能需要处理的横切问题,因此也请为此腾出空间
正如标题所说;我想知道如何在 Passport.js.
中实现令牌的检索目前我实施了以下策略:
passport.use(new GoogleStrategy({
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: '/api/auth/google/callback'
},
async (accessToken, refreshToken, profile, done) => {
const newUser = {
googleId: profile.id,
displayName: profile.displayName,
firstName: profile.name.givenName,
lastName: profile.name.familyName,
image: profile.photos[0].value,
email: profile.emails[0].value
};
try {
let user = await User.findOne({ googleId: profile.id });
if(user) {
done(null, user);
} else {
user = await User.create(newUser);
await Key.create({
googleId: user.googleId,
keyProvider: 'Google',
token: accessToken,
refreshToken: refreshToken
});
done(null, user);
}
} catch (err) {
console.error(err);
}
}));
现在,例如,我想调用 Google API,让我们说这条路线:
router.get('/list', async (req, res) => {
console.log('list called');
const result = await axios({
method: "GET",
headers: {
'Authorization': "Bearer [token which is stored in my db]",
},
"Content-Type": "application/json",
url: 'https://www.googleapis.com/youtube/v3/subscriptions?channelId=[someid]',
});
console.log(result);
});
我将在何处以及如何实现 function/middleware/module,每次我向 Google API 发出请求时都会调用它,并在出错然后重试调用?
因为我是 Node 的新手。js/Express 我不知道如何将这样的流程放入代码中。我知道要实现的唯一选择是使用 axios 捕获错误,然后在每个 Google API 端点中手动检索带有冗余代码的新 accessToken。
我想说这里有两个主要概念:
设计模式
用新的访问令牌重试 401 是标准的,正如您所说 - 这里有一些 axios code of mine。
AXIOS 规格
Axios 有一个概念interceptors and here is an example that does 401 retries。
个人喜好
两者都可以 - 我更喜欢我的方法,它曾经被称为 service agent
模式。重要的因素是:
- 使调用 API 的业务逻辑代码漂亮干净
- 请记住,还有其他可能需要处理的横切问题,因此也请为此腾出空间