我正在嘗試創建一個產品驗證系統,并且登錄部分已關閉。我的問題是,是否有任何 API 可以驗證激活碼之類的內容并在成功與否時回傳。順便說一句,您可能需要水平滾動才能查看所有代碼
//How would I add a verification system
document.getElementById('redeemButton').addEventListener('click', () => {
var code = document.getElementById('redeemCodeBox').value;
var product = document.getElementById('productCode').value;
const fetchPromise = fetch(`https://www.mywebsite.com/api/redeem?product=${product}&code=${code}`);
fetchPromise.then( response => {
if (!response.ok) {
throw new Error(`HTTP error: ${response.status}`);
}
return response.json();
})
.then( json => {
console.log(json[0].name);
})
.catch( error => {
console.error(`Could not get products: ${error}`);
throw `Please Enter a Valid Activation Code`;
});
});
uj5u.com熱心網友回復:
我認為您不應該依賴第三方來驗證激活碼。
您應該使用存盤在資料庫中的 JWT 令牌 激活碼的組合。
生成激活碼。
生成 JWT 令牌。
const token = await this.jwtService.sign( { usetId: userId }, { expiresIn: "1d" } );
將激活碼和 JWT 令牌保存到資料庫中。
向用戶發送帶有激活碼的電子郵件或短信。 包括用戶可以插入激活碼的 URL。
URL 可能如下所示:
https://www.mywebsite.com/api/activation?token=eyJhbGciOiJIfasdfas
(使用JWT 令牌的查詢引數=>> ?token=JWT_token
驗證來自 URL 的 JWT 令牌(來自查詢引數“token”的值)并驗證 Token 是否未過期。
控制用戶輸入是否與存盤在資料庫中的激活碼匹配。
活性
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/485181.html
標籤:javascript api 确认
上一篇:試圖改變不使用null的條件
