2016-03-03 97 views
0

有沒有人有經驗通過客戶端在StrongLoop中擴展內置模型「用戶」?StrongLoop客戶端擴展用戶模型內置登錄錯誤

事情是,我不能跳過新創建的客戶端的驗證。我已經聲明瞭選項 「emailVerificationRequired」 客戶端:假的,但我仍然無法登錄

錯誤:

error: Object code: "LOGIN_FAILED_EMAIL_NOT_VERIFIED" message: "login failed as the email has not been verified" name: "Error" stack: "Error: login failed as the email has not been verified↵ at C:\xampp\htdocs\loopback-getting-started\node_modules\loopback\common\models\user.js:248:21↵ at C:\xampp\htdocs\loopback-getting-started\node_modules\loopback\common\models\user.js:312:9" status: 401 statusCode: 401

回答

0

If you don't want email verification PUT below code in model-config.json

"user": { 
"dataSource": "YOUR DATASOURCE", 
"public": true, 
"options": { 
    "emailVerificationRequired": false 
}, 
"$promise": {}, 
"$resolved": true} 

And other thing is, you manually stop verification doing below logic. For example if users created by Admin.

user.beforeRemote('create' ,function(ctx, modelInstance, next) 
    { 

     if(ctx.req.query.key == "admin") // if users created by Admin. 
     { 
     ctx.args.data.emailVerified = 1; 
     } 

     next(); 
    }); 

IN afterRemote

user.afterRemote('create', function(context, user, next) { 


       if(!user.emailVerified) 
       { 

        console.log('> user.afterRemote triggered'); 
        var options = { 
         type: 'email', 
         to: user.email, 
         from: 'youremail, 
         subject: 'Thanks for registering.', 
         template: path.resolve(__dirname, '../../server/views/verify.ejs'), 
         redirect: '/verified', 
         user: user 
        }; 

        user.verify(options, function(err, response) { 
         if (err) return next(err); 

         context.res.render('response', { 
         title: 'Signed up successfully', 
         content: 'Please check your email and click on the verification link ' + 
          'before logging in.', 
         redirectTo: '/', 
         redirectToLinkText: 'Log in' 
         }); 
        }); 

       } 
       else 
       { 

        next(); 
       } 

      }); 
+0

夥計,你是救命恩人!非常感謝!沒有意識到它在model-config.json文件中有我的模型的配置XD –

+0

@GaudencioRachoTevesV不要忘記投票。 –