Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Vapor
- import Leaf
- import Fluent
- import Authentication
- class WebsiteController: RouteCollection {
- func boot(router: Router) throws {
- let usersRoute = router.grouped("api", "users")
- usersRoute.post(LoginPostData.self, at: "login", use: loginPostHandler)
- }
- }
- extension WebsiteController {
- func loginPostHandler(_ req: Request, userData: LoginPostData) throws -> Future<Response> {
- return User.authenticate(email: userData.email, password: userData.password, using: BCryptDigest(), on: req).map(to: Response.self) { user in
- guard let user = user else {
- return req.redirect(to: "/login")
- }
- try req.authenticateSession(user)
- return req.redirect(to: "/")
- }
- }
- }
- struct LoginPostData: Content {
- let email: String
- let password: String
- }
Add Comment
Please, Sign In to add comment