Last active
September 9, 2017 10:04
-
-
Save bennibau/59e4a75c37770ca3a31a4c88ac32b732 to your computer and use it in GitHub Desktop.
Revisions
-
bennibau revised this gist
Sep 9, 2017 . 1 changed file with 10 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,13 @@ import Vapor import AuthProvider import Sessions import HTTP final class Routes: RouteCollection { let view: ViewRenderer //route builder for protected routes let authRoutes : RouteBuilder //route builder for login routes let loginRouteBuilder : RouteBuilder -
bennibau created this gist
Sep 9, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,25 @@ //route builder for protected routes let authRoutes : RouteBuilder //route builder for login routes let loginRouteBuilder : RouteBuilder // 1. we need a reference to our droplet to create the routes init(_ view: ViewRenderer,_ drop: Droplet) { //this stays the same self.view = view //create a password middleware with our user let passwordMiddleware = PasswordAuthenticationMiddleware(User.self) //create a memory storage for our sessions let memory = MemorySessions() //create the persis middleware with our User let persistMiddleware = PersistMiddleware(User.self) //create the sessions middleware with our memory let sessionsMiddleware = SessionsMiddleware(memory) //2. now that we have instantiated everthing, all we need to do is create two routes. // the first route is for password protected routes self.authRoutes = drop.grouped([sessionsMiddleware, persistMiddleware, passwordMiddleware]) // the second one is to login, We need this route to have the sessions and persist middleware in place to store // that a user has logged in and give him a vapor access token which he can use in the upcoming requests self.loginRouteBuilder = drop.grouped([sessionsMiddleware, persistMiddleware]) }