美文网首页vapor 实践
vapor 根据邮箱和用户密码登陆

vapor 根据邮箱和用户密码登陆

作者: 搞好关系 | 来源:发表于2018-10-08 00:10 被阅读5次

    接注册完成我们来完成用户登陆:邮箱作为账户

    1. 注册路由:
    SKUserController中添加function:   
     public func login(req: Request)throws-> EventLoopFuture<String>{
    ...
    }
    
     router.get("login", use: SKUserController().login)
    
    1. 用户登陆很常规会遇到几个常规问题:
      · 账户不存在
      . 账户存在密码不正确
    public func login(req: Request)throws-> EventLoopFuture<String>{
            struct InnerUser: Content{
                var email: String
                var password: String
            }
            
            let user = try! req.query.decode(InnerUser.self)
         return   SKUser.query(on: req).group(SQLiteBinaryOperator.or) { (or) in
                or.filter(\.email, SQLiteBinaryOperator.equal, user.email)
                }.all().flatMap { (us) -> EventLoopFuture<String> in
                    let result = req.eventLoop.newPromise(String.self)
    
                    if us.isEmpty {
                        result.succeed(result: "用户不存在")
                        return result.futureResult
                    }else{
                        
                        if us.first!.password.elementsEqual(user.password.md5Base64) {
                            
                            result.succeed(result: "登陆成功:\(us.first!)")
                        }else{
                            result.succeed(result: "密码错误")
                        }
                        return result.futureResult
                    }
            }
            
        }
    
    

    相关文章

      网友评论

        本文标题:vapor 根据邮箱和用户密码登陆

        本文链接:https://www.haomeiwen.com/subject/aydraftx.html