我正在我的网站上的 Node JS 中集成电话电子邮件“使用电话登录”按钮。我创建了按钮并调用他们的 URL,它会打开一个弹出窗口以输入手机号码。提交后,显示 OTP 窗口。我成功收到 OTP 短信并输入 OTP,但我没有在我的网站上收到经过验证的号码。这是我网页的代码片段。
// Main endpoint
app.get('/', (req, res) => {
// Set query parameters for constructing the URL
const queryParams = {
countryCode: process.env.COUNTRY_CODE,
phoneNo: process.env.PHONE_NO,
redirectUrl: process.env.REDIRECT_URL,
};
// Construct URL for the phone sign-in page
const phoneSignInUrl = `https://www.phone.email/auth/sign-in?countrycode=${queryParams.countryCode}&phone_no=${queryParams.phoneNo}&redirect_url=${queryParams.redirectUrl}`;
// Render the main view with the constructed URL
return res.render('pages/index', { phoneSignInUrl });
});
单击上面代码片段上的按钮,它会在我的重定向 URL(http://localhost:3000/auth)中返回响应。下面给出了我的身份验证页面代码片段。
app.get('/auth', (req, res) => {
try {
// Extract token from query parameters
const token = req.query.token;
// Check if token exists
if (!token) {
throw new Error("Token is required for authentication.");
}
// Verify the JWT
const decoded = jwt.verify(token, process.env.API_KEY, { algorithm: 'HS256' });
// Render success view with decoded information
const message = `${decoded.country_code} ${decoded.phone_no}`;
const status = `success`;
return res.render('pages/auth-success', { message, status, token });
} catch (error) {
// Render error view with error message
const status = 'failure';
const message = error.message;
return res.render('pages/auth-error', { status, message });
}
});
调用成功,但我没有收到令牌。我已在 .env 文件中定义了所需的凭据,并且它们是正确的。
您没有从 get 参数接收到正确的令牌变量。将代码中的标记替换为
phtoken
。试试这个代码:
app.get('/auth', (req, res) => {
try {
// Extract token from query parameters
const token = req.query.phtoken;
// Check if token exists
if (!token) {
throw new Error("Token is required for authentication.");
}
// Verify the JWT
const decoded = jwt.verify(token, process.env.API_KEY, { algorithm: 'HS256' });
// Render success view with decoded information
const message = `${decoded.country_code} ${decoded.phone_no}`;
const status = `success`;
return res.render('pages/auth-success', { message, status, token });
} catch (error) {
// Render error view with error message
const status = 'failure';
const message = error.message;
return res.render('pages/auth-error', { status, message });
}
});