Преглед на файлове

add message verification for register

Starink преди 6 години
родител
ревизия
b921618186
променени са 7 файла, в които са добавени 136 реда и са изтрити 6 реда
  1. 1 0
      package.json
  2. 49 4
      server/controller/user.js
  3. 18 0
      server/index.js
  4. 1 0
      server/routes/index.js
  5. 2 1
      server/schema/user.js
  6. 10 0
      server/util/aliClient.js
  7. 55 1
      yarn.lock

+ 1 - 0
package.json

@@ -12,6 +12,7 @@
     "test": "jest"
   },
   "dependencies": {
+    "@alicloud/pop-core": "^1.7.9",
     "@nuxtjs/axios": "^5.9.5",
     "@nuxtjs/style-resources": "^1.0.0",
     "@vue-space/core": "^0.0.1-alpha.2",

+ 49 - 4
server/controller/user.js

@@ -3,6 +3,7 @@ const jwt = require('jsonwebtoken');
 const bcrypt = require('bcrypt');
 const secret = require('../config/secret');
 const Response = require('../util/response');
+const aliClient = require('../util/aliClient')
 
 
 class UserController {
@@ -50,14 +51,58 @@ class UserController {
      */
     static async register(ctx) {
         const data = ctx.request.body;
-        const hashPassword = bcrypt.hashSync(data.password, 10);
-        const user = await userModel.create(data.name, hashPassword, data.phone);
-        if (user) {
+        if(!ctx.session.verification_phone || !ctx.session.verification_code){
+          ctx.body = Response.failed('请重新获取验证码');
+        }else if(ctx.session.verification_code !== data.identifyCode || ctx.session.verification_phone !== data.phone){
+          ctx.body = Response.failed('验证码错误');
+        }else{
+          const hashPassword = bcrypt.hashSync(data.password, 10);
+          const user = await userModel.create(data.name, hashPassword, data.phone);
+          if (user) {
             ctx.body = Response.success({name: user.name});
-        } else {
+          } else {
             ctx.body = Response.failed('此手机号已注册');
+          }
         }
     }
+
+    static async sendMessage(ctx){
+      let {phone} = ctx.request.body;
+
+
+      let code = "";
+      for(let i = 0; i < 6; i++){
+        code+=Math.floor(Math.random()*10)
+      }
+      let params =  {
+        PhoneNumbers: phone,
+        SignName: 'seeccoder',
+        TemplateCode: 'SMS_181555598',
+        TemplateParam: '{"code":"' + code + '"}'
+      };
+      let options = {
+        timeout: 3000, // default 3000 ms
+        formatAction: true, // default true, format the action to Action
+        formatParams: true, // default true, format the parameter name to first letter upper case
+        method: 'GET', // set the http method, default is GET
+        headers: {}, // set the http request headers
+      };
+      try{
+        const result = await aliClient.request('SendSms', params);
+        console.log("result:", result);
+        if(result.Code === "OK"){
+          ctx.session.verification_code = code;
+          ctx.session.verification_phone = phone;
+          console.log('verification_code:', ctx.session.verification_code );
+        }else{
+          console.log("error:", message);
+        }
+
+      }catch (e) {
+        console.log(e);
+      }
+
+    }
 }
 
 module.exports = UserController;

+ 18 - 0
server/index.js

@@ -10,6 +10,7 @@ const jwt = require('koa-jwt');
 const helmet = require('koa-helmet');
 const mount = require('koa-mount');
 const set = require('lodash/set');
+const session = require('koa-session');
 const { Provider } = require('oidc-provider');
 const consola = require('consola')
 const { Nuxt, Builder } = require('nuxt')
@@ -20,6 +21,7 @@ const configuration = require('./support/configuration');
 
 const router = require('./routes/index');
 
+
 const { PORT = 3000, ISSUER = `http://localhost:${PORT}` } = process.env;
 
 // error handler
@@ -30,6 +32,22 @@ app.use(bodyparser({
     enableTypes:['json', 'form', 'text']
 }));
 
+app.keys = ['seec'];
+
+const CONFIG = {
+  key: 'SESSION', /** (string) cookie key (default is koa:sess) */
+  /** (number || 'session') maxAge in ms (default is 1 days) */
+  /** 'session' will result in a cookie that expires when session/browser is closed */
+  /** Warning: If a session cookie is stolen, this cookie will never expire */
+  maxAge: 5 * 60 * 1000,
+  overwrite: true, /** (boolean) can overwrite or not (default true) */
+  httpOnly: true, /** (boolean) httpOnly or not (default true) */
+  signed: true, /** (boolean) signed or not (default true) */
+  rolling: false /** (boolean) Force a session identifier cookie to be set on every response. The expiration is reset to the original maxAge, resetting the expiration countdown. default is false **/
+}
+app.use(session(CONFIG, app));
+
+
 app.use(cors({
   origin:  (ctx)=>{
     return "*";

+ 1 - 0
server/routes/index.js

@@ -8,6 +8,7 @@ const router = new Router({prefix: '/api'});
 module.exports = () => {
     router.post('/user/login', UserController.login); // 登录
     router.post('/user/register', UserController.register);  // 登录(回调)
+  router.post('/user/sendMessage', UserController.sendMessage);  // 登录(回调)
 
     router.get('/course/getMyCourses',  CourseController.getMyCourses);
     router.get('/course/getHotCourses',  CourseController.getHotCourses);

+ 2 - 1
server/schema/user.js

@@ -19,7 +19,8 @@ module.exports = (sequelize, DataTypes) =>
         },
         phone: {
             type: DataTypes.STRING(64),
-            allowNull: false
+            allowNull: false,
+            unique: true
         },
         signature: {
             type: DataTypes.STRING(512),

+ 10 - 0
server/util/aliClient.js

@@ -0,0 +1,10 @@
+const RPCClient = require('@alicloud/pop-core').RPCClient;
+
+const client = new RPCClient({
+  accessKeyId: 'LTAI4FiAXXyNfYJLweBXpKPx',
+  accessKeySecret: 'IBoC7KGkZHOvxZQIfQgp98ZY48AVjW',
+  endpoint: 'https://dysmsapi.aliyuncs.com',
+  apiVersion: '2017-05-25'
+});
+
+module.exports = client;

+ 55 - 1
yarn.lock

@@ -2,6 +2,17 @@
 # yarn lockfile v1
 
 
+"@alicloud/pop-core@^1.7.9":
+  version "1.7.9"
+  resolved "https://registry.yarnpkg.com/@alicloud/pop-core/-/pop-core-1.7.9.tgz#97c0c5bb591894feda87e8a82d45c117bebbde13"
+  integrity sha512-WKeil0O51ee1EbCcYt65vPYo6eWcDn7dfmXlCMb/GbNDq0MvVheQVS/uAeeUipRXt8jGMc58FXeQxXfsFqlRZg==
+  dependencies:
+    debug "^3.1.0"
+    httpx "^2.1.2"
+    json-bigint "^0.2.3"
+    kitx "^1.2.1"
+    xml2js "^0.4.17"
+
 "@babel/code-frame@^7.0.0", "@babel/code-frame@^7.8.3":
   version "7.8.3"
   resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.8.3.tgz#33e25903d7481181534e12ec0a25f16b6fcf419e"
@@ -1404,6 +1415,11 @@
   resolved "https://registry.yarnpkg.com/@types/node/-/node-12.12.31.tgz#d6b4f9645fee17f11319b508fb1001797425da51"
   integrity sha512-T+wnJno8uh27G9c+1T+a1/WYCHzLeDqtsGJkoEdSp2X8RTh3oOCZQcUnjAx90CS8cmmADX51O0FI/tu9s0yssg==
 
+"@types/node@^12.0.2":
+  version "12.12.32"
+  resolved "https://registry.yarnpkg.com/@types/node/-/node-12.12.32.tgz#0ccc836d273e8a3cddf568daf22729cfa57c1925"
+  integrity sha512-44/reuCrwiQEsXud3I5X3sqI5jIXAmHB5xoiyKUw965olNHF3IWKjBLKK3F9LOSUZmK+oDt8jmyO637iX+hMgA==
+
 "@types/q@^1.5.1":
   version "1.5.2"
   resolved "https://registry.yarnpkg.com/@types/q/-/q-1.5.2.tgz#690a1475b84f2a884fd07cd797c00f5f31356ea8"
@@ -2291,6 +2307,11 @@ bignumber.js@9.0.0:
   resolved "https://registry.yarnpkg.com/bignumber.js/-/bignumber.js-9.0.0.tgz#805880f84a329b5eac6e7cb6f8274b6d82bdf075"
   integrity sha512-t/OYhhJ2SD+YGBQcjY8GzzDHEk9f3nerxjtfa6tlMXfe7frs/WozhvCNoGvpM0P3bNf3Gq5ZRMlGr5f3r4/N8A==
 
+bignumber.js@^4.0.0:
+  version "4.1.0"
+  resolved "https://registry.yarnpkg.com/bignumber.js/-/bignumber.js-4.1.0.tgz#db6f14067c140bd46624815a7916c92d9b6c24b1"
+  integrity sha512-eJzYkFYy9L4JzXsbymsFn3p54D+llV27oTQ+ziJG7WFRheJcNZilgVXMG0LoZtlQSKBsJdWtLFqOD0u+U0jZKA==
+
 binary-extensions@^1.0.0:
   version "1.13.1"
   resolved "https://registry.yarnpkg.com/binary-extensions/-/binary-extensions-1.13.1.tgz#598afe54755b2868a5330d2aff9d4ebb53209b65"
@@ -5345,6 +5366,14 @@ https-browserify@^1.0.0:
   resolved "https://registry.yarnpkg.com/https-browserify/-/https-browserify-1.0.0.tgz#ec06c10e0a34c0f2faf199f7fd7fc78fffd03c73"
   integrity sha1-7AbBDgo0wPL68Zn3/X/Hj//QPHM=
 
+httpx@^2.1.2:
+  version "2.2.3"
+  resolved "https://registry.yarnpkg.com/httpx/-/httpx-2.2.3.tgz#b7ec4e54c1946a13f3ad6639483ce03ba280fb24"
+  integrity sha512-peGQDI+x5JwGvRJ+VUd2oqR4jn9G/40EsV+6hmVK7hDDvDIgshIIBEXjJJ1ZLfhxaTW8ZEKS/VMeLP3bdyhxvQ==
+  dependencies:
+    "@types/node" "^12.0.2"
+    debug "^4.1.1"
+
 human-signals@^1.1.1:
   version "1.1.1"
   resolved "https://registry.yarnpkg.com/human-signals/-/human-signals-1.1.1.tgz#c5b1cd14f50aeae09ab6c59fe63ba3395fe4dfa3"
@@ -6430,6 +6459,13 @@ jsesc@~0.5.0:
   resolved "https://registry.yarnpkg.com/jsesc/-/jsesc-0.5.0.tgz#e7dee66e35d6fc16f710fe91d5cf69f70f08911d"
   integrity sha1-597mbjXW/Bb3EP6R1c9p9w8IkR0=
 
+json-bigint@^0.2.3:
+  version "0.2.3"
+  resolved "https://registry.yarnpkg.com/json-bigint/-/json-bigint-0.2.3.tgz#118d7f6ff1d38659f19f94cf73e64a75a3f988a8"
+  integrity sha1-EY1/b/HThlnxn5TPc+ZKdaP5iKg=
+  dependencies:
+    bignumber.js "^4.0.0"
+
 json-buffer@3.0.0:
   version "3.0.0"
   resolved "https://registry.yarnpkg.com/json-buffer/-/json-buffer-3.0.0.tgz#5b1f397afc75d677bde8bcfc0e47e1f9a3d9a898"
@@ -6570,6 +6606,11 @@ kind-of@^6.0.0, kind-of@^6.0.2:
   resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-6.0.3.tgz#07c05034a6c349fa06e24fa35aa76db4580ce4dd"
   integrity sha512-dcS1ul+9tmeD95T+x28/ehLgd9mENa3LsvDTtzm3vyBEO7RPptvAD+t44WVXaUjTBRcrpFeFlC8WCruUR456hw==
 
+kitx@^1.2.1:
+  version "1.3.0"
+  resolved "https://registry.yarnpkg.com/kitx/-/kitx-1.3.0.tgz#ab3ee7c598d2b1d629fd55568f868c4440c200ea"
+  integrity sha512-fhBqFlXd0GkKTB+8ayLfpzPUw+LHxZlPAukPNBD1Om7JMeInT+/PxCAf1yLagvD+VKoyWhXtJR68xQkX/a0wOQ==
+
 kleur@^3.0.3:
   version "3.0.3"
   resolved "https://registry.yarnpkg.com/kleur/-/kleur-3.0.3.tgz#a79c9ecc86ee1ce3fa6206d1216c501f147fc07e"
@@ -9921,7 +9962,7 @@ sass-resources-loader@^2.0.0:
     glob "^7.1.1"
     loader-utils "^1.0.4"
 
-sax@^1.2.4, sax@~1.2.4:
+sax@>=0.6.0, sax@^1.2.4, sax@~1.2.4:
   version "1.2.4"
   resolved "https://registry.yarnpkg.com/sax/-/sax-1.2.4.tgz#2816234e2378bddc4e5354fab5caa895df7100d9"
   integrity sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw==
@@ -11809,6 +11850,19 @@ xml-name-validator@^3.0.0:
   resolved "https://registry.yarnpkg.com/xml-name-validator/-/xml-name-validator-3.0.0.tgz#6ae73e06de4d8c6e47f9fb181f78d648ad457c6a"
   integrity sha512-A5CUptxDsvxKJEU3yO6DuWBSJz/qizqzJKOMIfUJHETbBw/sFaDxgd6fxm1ewUaM0jZ444Fc5vC5ROYurg/4Pw==
 
+xml2js@^0.4.17:
+  version "0.4.23"
+  resolved "https://registry.yarnpkg.com/xml2js/-/xml2js-0.4.23.tgz#a0c69516752421eb2ac758ee4d4ccf58843eac66"
+  integrity sha512-ySPiMjM0+pLDftHgXY4By0uswI3SPKLDw/i3UXbnO8M/p28zqexCUoPmQFrYD+/1BzhGJSs2i1ERWKJAtiLrug==
+  dependencies:
+    sax ">=0.6.0"
+    xmlbuilder "~11.0.0"
+
+xmlbuilder@~11.0.0:
+  version "11.0.1"
+  resolved "https://registry.yarnpkg.com/xmlbuilder/-/xmlbuilder-11.0.1.tgz#be9bae1c8a046e76b31127726347d0ad7002beb3"
+  integrity sha512-fDlsI/kFEx7gLvbecc0/ohLG50fugQp8ryHzMTuW9vSa1GJ0XYWKnhsUx7oie3G98+r56aTQIUB4kht42R3JvA==
+
 xtend@^4.0.0, xtend@~4.0.1:
   version "4.0.2"
   resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.2.tgz#bb72779f5fa465186b1f438f674fa347fdb5db54"