vapor 根据邮箱和用户密码登陆
2018-10-08 本文已影响5人
搞好关系
接注册完成我们来完成用户登陆:邮箱作为账户
- 注册路由:
SKUserController中添加function:
public func login(req: Request)throws-> EventLoopFuture<String>{
...
}
router.get("login", use: SKUserController().login)
- 用户登陆很常规会遇到几个常规问题:
· 账户不存在
. 账户存在密码不正确
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
}
}
}