Advertisement
Guest User

Untitled

a guest
Sep 9th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. //1. Modify this controller to be built by "authRoutes" to protect all it's routes
  2. /// GET /hello/...
  3. authRoutes.resource("hello", HelloController(view))
  4.  
  5. //2. create the login route
  6. builder.get("login") { req in
  7. return try self.view.make("login")
  8. }
  9.  
  10. //3. implement the login logic, built by the "loginRouteBuilder" so our session is persisted
  11. loginRouteBuilder.post("login") { req in
  12. guard let email = req.formURLEncoded?["email"]?.string,
  13. let password = req.formURLEncoded?["password"]?.string else {
  14. return "Bad credentials"
  15. }
  16. let credentials = Password(username: email, password: password)
  17. let user = try User.authenticate(credentials)
  18.  
  19. req.auth.authenticate(user)
  20.  
  21. return Response(redirect: "hello")
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement