因为前几天我们新增了 verification_code
这张表的栏位,调整了发送认证码的流程,并且设定该认证码在发送後只有10分钟时效,这样一来在原先处理检验认证码的程序逻辑上也要作出修改。
因为我们在发送认证码时,已经将10分钟後的 timestamp 写入了 verification_code
的 expire_at
,所以我们只要检视这个栏位就能实现认证码时效检验的功能。
有两个修改的重点:
bind_at
是否为空expire_at
是否大於等於现在时间bind_at
押上现在时间,并且将 users sheet 对应的使用者栏位 is_verifed
设为 truefunction tagVerificationCode(target, userId) {
const sheet = ReadMailAndInsertToGoogleSheet.connectToSheet('verification_code');
const searchResult = ReadMailAndInsertToGoogleSheet.searchColumnValue(sheet, 'code', target);
if (searchResult !== -1) {
const targetRowIndex = searchResult+2;
const targetRowRange = sheet.getRange(targetRowIndex, 2, 1, 4);
var targetRowValue = targetRowRange.getValues()[0];
if ((targetRowValue[0] === userId) && (targetRowValue[1].length === 0) && checkExpireAtWithNow(targetRowValue[3])) {
// write back bind_at
targetRowValue[1] = new Date();
targetRowRange.setValues([targetRowValue]);
// create or update user
upsertUserInfo(userId, {'is_verifed': true});
return true;
}
}
return false;
}
function checkExpireAtWithNow(expireAt) {
const now = Math.floor(Date.now() / 1000);
return expireAt >= now;
}
因为使用者已经认证成功了,所以这时可以根据前一天的教学,将身分认证
的图文选单切换为获取验证码
的图文选单
新增 changeRichMenuAfterBindSuccess 负责切换 userId 的图文选单为获取验证码
function changeRichMenuAfterBindSuccess(userId) {
// RICH_MENU_ID_AFTER_BIND_SUCCESS 是获取验证码图文选单的 rich menu id
UrlFetchApp.fetch(`https://api.line.me/v2/bot/user/${userId}/richmenu/${RICH_MENU_ID_AFTER_BIND_SUCCESS}`, {
'headers': {
'Content-Type': 'application/json; charset=UTF-8',
'Authorization': 'Bearer ' + CHANNEL_ACCESS_TOKEN
},
'method': 'post'
});
}
可以新增一个测试函式,并且在 GAS 专案执行此函式,测试是否有正常切换图文选单
p.s. Line User Id 现在应该可以在 Line Bot - 验证码小帮手
按下 执行身份认证
後,从 Google Sheet 的 verification_code 找到
function testChangeRichMenuAfterBindSuccess(){
changeRichMenuAfterBindSuccess('YOUR_LINE_USER_ID');
}
这几天实作的功能都是在整个认证流程中不可或缺的功能模组,最後只要组装起来就可以完成整个身份认证流程罗~明天继续努力!
<<: Day11-旧网站重写成Vue_2_json抓取资料与渲染
>>: 【设计+切版30天实作】|Day12 - 怎麽让使用者选中您想要他选的Plans设计?
使用AutoLayout分为两种方式 writing Code Use Interface Buil...
API Application Programming Interface的缩写,主要在I,一个接口...
call, apply, bind 方法 当我们对函式使用 call, apply, bind 这三...
我有一群变态朋友,我们总称变态五怪人(?),现在扣掉我就变成变态四乾妈了~ 拖到现在乾妈们终於来了...
前言: 函式,可以让我们的code 看起来比较好懂,重复利用性也会提高。 大纲: 修饰符号 函式宣告...