用 Line LIFF APP 实现信箱验证绑定功能(3) - 修改流程实现认证时效检验

因为前几天我们新增了 verification_code 这张表的栏位,调整了发送认证码的流程,并且设定该认证码在发送後只有10分钟时效,这样一来在原先处理检验认证码的程序逻辑上也要作出修改。

修改收到认证码後检验的流程

因为我们在发送认证码时,已经将10分钟後的 timestamp 写入了 verification_codeexpire_at,所以我们只要检视这个栏位就能实现认证码时效检验的功能。

修改 tagVerificationCode

有两个修改的重点:

  • 在 verification_code 找到认证码後,检查:
    1. 该列的 userId 是否符合发送时纪录的值
    2. bind_at 是否为空
    3. expire_at 是否大於等於现在时间
  • 若检验都通过,则在 bind_at 押上现在时间,并且将 users sheet 对应的使用者栏位 is_verifed 设为 true
function 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;
}

使用 Line Messaging 切换图文选单

因为使用者已经认证成功了,所以这时可以根据前一天的教学,将身分认证的图文选单切换为获取验证码的图文选单

新增 changeRichMenuAfterBindSuccess

新增 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设计?

NSLayoutConstraint使用 Day21

使用AutoLayout分为两种方式 writing Code Use Interface Buil...

D-9. Rails API-Only 实作 && House Robber

API Application Programming Interface的缩写,主要在I,一个接口...

【Day28】this - call, apply, bind 与严谨模式

call, apply, bind 方法 当我们对函式使用 call, apply, bind 这三...

出生第35天 变态四乾妈

我有一群变态朋友,我们总称变态五怪人(?),现在扣掉我就变成变态四乾妈了~ 拖到现在乾妈们终於来了...

Kotlin Android 第5天,从 0 到 ML - 函式

前言: 函式,可以让我们的code 看起来比较好懂,重复利用性也会提高。 大纲: 修饰符号 函式宣告...