接注冊(cè)完成我們來(lái)完成用戶登陸:郵箱作為賬戶
- 注冊(cè)路由:
SKUserController中添加function:
public func login(req: Request)throws-> EventLoopFuture<String>{
...
}
router.get("login", use: SKUserController().login)
- 用戶登陸很常規(guī)會(huì)遇到幾個(gè)常規(guī)問(wèn)題:
· 賬戶不存在
. 賬戶存在密碼不正確
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: "密碼錯(cuò)誤")
}
return result.futureResult
}
}
}