diff --git a/build/2.0/404.html b/build/2.0/404.html index b0597f03..fd3e6f79 100644 --- a/build/2.0/404.html +++ b/build/2.0/404.html @@ -911,6 +911,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/auth/getting-started/index.html b/build/2.0/auth/getting-started/index.html index 6cb8a976..216bd0f2 100644 --- a/build/2.0/auth/getting-started/index.html +++ b/build/2.0/auth/getting-started/index.html @@ -1062,6 +1062,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/auth/helper/index.html b/build/2.0/auth/helper/index.html index f68630f3..f5b9380e 100644 --- a/build/2.0/auth/helper/index.html +++ b/build/2.0/auth/helper/index.html @@ -1000,6 +1000,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/auth/package/index.html b/build/2.0/auth/package/index.html index 522a7c79..2a8568a4 100644 --- a/build/2.0/auth/package/index.html +++ b/build/2.0/auth/package/index.html @@ -960,6 +960,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/auth/password/index.html b/build/2.0/auth/password/index.html index fea8f622..5e190e7e 100644 --- a/build/2.0/auth/password/index.html +++ b/build/2.0/auth/password/index.html @@ -993,6 +993,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/auth/persist/index.html b/build/2.0/auth/persist/index.html index c19ea04c..54490714 100644 --- a/build/2.0/auth/persist/index.html +++ b/build/2.0/auth/persist/index.html @@ -1021,6 +1021,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + @@ -1878,13 +1890,13 @@ the username and password.

    -
  • + Redirect Middleware + +
  • + + diff --git a/build/2.0/auth/redirect-middleware/index.html b/build/2.0/auth/redirect-middleware/index.html new file mode 100644 index 00000000..a1501ed3 --- /dev/null +++ b/build/2.0/auth/redirect-middleware/index.html @@ -0,0 +1,1921 @@ + + + + + + + + + + + + + + + + + + Redirect Middleware - Vapor Docs + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    + +
    + +
    + + +
    +
    + + +
    +
    +
    + +
    +
    +
    + + +
    +
    +
    + + +
    +
    +
    + + +
    +
    + + edit + + + +

    Redirect Middlewares

    +

    Included in the AuthProvider package are RedirectMiddleware and InverseRedirectMiddleware classes that will help you +redirect unauthenticated or authenticated requests to a given path. This is especially useful for redirecting users away from secure +pages to a login page and vice versa.

    +

    Redirect Middleware

    +

    Let's take a look at how to add a RedirectMiddleware to your application.

    +

    Existing Auth

    +

    Since we only want this middleware to apply to secure pages, we'll apply it using route groups.

    +

    You should already have a protected area in your application using one of the authentication middlewares.

    +
    import Vapor
    +import AuthProvider
    +
    +let drop = try Droplet()
    +
    +drop.get("login") { req in
    +    return // some login form
    +}
    +
    +let auth = TokenAuthenticationMiddleware(User.self)
    +let protected = drop.grouped([auth])
    +protected.get("secure") { req in
    +    let user = try req.auth.assertAuthenticated(User.self)
    +    return "Welcome to the secure page, \(user.name)"
    +}
    +
    + + +

    The above snippet protects access to the page at GET /secure using the TokenAuthenticationMiddleware.

    +

    Since we've applied TokenAuthenticationMiddleware, this page cannot be accessed by anyone not authenticated. +Although this is perfectly secure, we should provide a better experience for unauthenticated users. Instead of +just showing them an error message, we can redirect them to the login page.

    +

    Add Redirect

    +

    Creating a redirect middleware is very simple. We'll use one of the presets for redirecting a user to /login.

    +
    let redirect = RedirectMiddleware.login()
    +
    + + +

    Now we just need to add this redirect middleware to our protected route group mentioned previously.

    +
    let protected = drop.grouped([redirect, auth])
    +
    + + +
    +

    Warning

    +

    Make sure the redirect middleware comes before the auth middleware.

    +
    +

    Complete Example

    +

    Now whenever an unauthenticated user attemps to visit GET /secure, they will be redirected to GET /login.

    +
    import Vapor
    +import AuthProvider
    +
    +let drop = try Droplet()
    +
    +let redirect = RedirectMiddleware.login()
    +let auth = TokenAuthenticationMiddleware(TestUser.self)
    +
    +let protected = drop.grouped([redirect, auth])
    +protected.get { req in
    +    let user = try req.auth.assertAuthenticated(TestUser.self)
    +    return "Welcome to the dashboard, \(user.name)"
    +}
    +
    + + +

    Custom Route

    +

    If your login page is not /login or you'd like the redirect middleware to redirect to a different type of page, +simply use the full initializer.

    +
    let redirect = RedirectMiddleware(path: "/foo")
    +
    + + +

    Inverse Redirect Middleware

    +

    Complementary to the RedirectMiddleware is the InverseRedirectMiddleware. Just like you want to redirect unauthenticated +users away from secure pages, you also might want to redirect authenticated users away from certain pages.

    +

    For example, if a user is already authenticated and they visit the login page, they might be confused and attempt to login again.

    +

    Example

    +

    Here is an example of the InverseRedirectMiddleware being used to redirect authenticated Users away from the login page.

    +

    We are using the preset .home() convenience, which redirects the user to GET /.

    +
    import Vapor
    +import AuthProvider
    +
    +let drop = try Droplet()
    +
    +let redirect = InverseRedirectMiddleware.home(User.self)
    +let group = drop.grouped([redirect])
    +group.get("login") { req in
    +    return "Please login"
    +}
    +
    + + +

    Custom Route

    +

    If your desired page is not / or you'd like the inverse redirect middleware to redirect to a different type of page, +simply use the full initializer.

    +
    let redirect = InverseRedirectMiddleware(User.self, path: "/foo")
    +
    + + +
    +
    +
    +
    + + + + +
    + + + + + + + + + + + + \ No newline at end of file diff --git a/build/2.0/bits/overview/index.html b/build/2.0/bits/overview/index.html index e65c5eaa..89e59f62 100644 --- a/build/2.0/bits/overview/index.html +++ b/build/2.0/bits/overview/index.html @@ -917,6 +917,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/bits/package/index.html b/build/2.0/bits/package/index.html index 3888cbc9..2466ba14 100644 --- a/build/2.0/bits/package/index.html +++ b/build/2.0/bits/package/index.html @@ -917,6 +917,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/cache/overview/index.html b/build/2.0/cache/overview/index.html index ca06c2f4..35f23779 100644 --- a/build/2.0/cache/overview/index.html +++ b/build/2.0/cache/overview/index.html @@ -1000,6 +1000,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/cache/package/index.html b/build/2.0/cache/package/index.html index 3ba8fa88..da1703aa 100644 --- a/build/2.0/cache/package/index.html +++ b/build/2.0/cache/package/index.html @@ -928,6 +928,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/configs/config/index.html b/build/2.0/configs/config/index.html index c04ef22f..a5149ae1 100644 --- a/build/2.0/configs/config/index.html +++ b/build/2.0/configs/config/index.html @@ -1027,6 +1027,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/core/overview/index.html b/build/2.0/core/overview/index.html index 99e4aa5e..3fa95fb2 100644 --- a/build/2.0/core/overview/index.html +++ b/build/2.0/core/overview/index.html @@ -917,6 +917,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/core/package/index.html b/build/2.0/core/package/index.html index 0bb48f2b..7a078c2a 100644 --- a/build/2.0/core/package/index.html +++ b/build/2.0/core/package/index.html @@ -917,6 +917,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/debugging/overview/index.html b/build/2.0/debugging/overview/index.html index 89800944..6f8ca7b0 100644 --- a/build/2.0/debugging/overview/index.html +++ b/build/2.0/debugging/overview/index.html @@ -917,6 +917,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/debugging/package/index.html b/build/2.0/debugging/package/index.html index f5d24019..e715cccc 100644 --- a/build/2.0/debugging/package/index.html +++ b/build/2.0/debugging/package/index.html @@ -917,6 +917,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/deploy/nginx/index.html b/build/2.0/deploy/nginx/index.html index c7c82c4f..e97df52c 100644 --- a/build/2.0/deploy/nginx/index.html +++ b/build/2.0/deploy/nginx/index.html @@ -917,6 +917,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/deploy/supervisor/index.html b/build/2.0/deploy/supervisor/index.html index 20efec6d..b10aec0c 100644 --- a/build/2.0/deploy/supervisor/index.html +++ b/build/2.0/deploy/supervisor/index.html @@ -917,6 +917,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/fluent/database/index.html b/build/2.0/fluent/database/index.html index 3db84447..b393bef0 100644 --- a/build/2.0/fluent/database/index.html +++ b/build/2.0/fluent/database/index.html @@ -1062,6 +1062,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/fluent/getting-started/index.html b/build/2.0/fluent/getting-started/index.html index eb3978d8..256eca3a 100644 --- a/build/2.0/fluent/getting-started/index.html +++ b/build/2.0/fluent/getting-started/index.html @@ -1061,6 +1061,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/fluent/model/index.html b/build/2.0/fluent/model/index.html index c718fc55..8af120e7 100644 --- a/build/2.0/fluent/model/index.html +++ b/build/2.0/fluent/model/index.html @@ -1144,6 +1144,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/fluent/package/index.html b/build/2.0/fluent/package/index.html index fb9907ea..acac69d7 100644 --- a/build/2.0/fluent/package/index.html +++ b/build/2.0/fluent/package/index.html @@ -967,6 +967,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/fluent/query/index.html b/build/2.0/fluent/query/index.html index c5cf9959..2e0493b7 100644 --- a/build/2.0/fluent/query/index.html +++ b/build/2.0/fluent/query/index.html @@ -1076,6 +1076,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/fluent/relations/index.html b/build/2.0/fluent/relations/index.html index b21164c2..05fdd45b 100644 --- a/build/2.0/fluent/relations/index.html +++ b/build/2.0/fluent/relations/index.html @@ -1054,6 +1054,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/getting-started/hello-world/index.html b/build/2.0/getting-started/hello-world/index.html index b53c6723..d4aa5b44 100644 --- a/build/2.0/getting-started/hello-world/index.html +++ b/build/2.0/getting-started/hello-world/index.html @@ -1040,6 +1040,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/getting-started/install-on-macos/index.html b/build/2.0/getting-started/install-on-macos/index.html index ebc1dec3..50b13dc6 100644 --- a/build/2.0/getting-started/install-on-macos/index.html +++ b/build/2.0/getting-started/install-on-macos/index.html @@ -1021,6 +1021,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/getting-started/install-on-ubuntu/index.html b/build/2.0/getting-started/install-on-ubuntu/index.html index 47aec94a..fcd2a050 100644 --- a/build/2.0/getting-started/install-on-ubuntu/index.html +++ b/build/2.0/getting-started/install-on-ubuntu/index.html @@ -1008,6 +1008,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/getting-started/manual/index.html b/build/2.0/getting-started/manual/index.html index 19c39dbe..8a7892a1 100644 --- a/build/2.0/getting-started/manual/index.html +++ b/build/2.0/getting-started/manual/index.html @@ -994,6 +994,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/getting-started/toolbox/index.html b/build/2.0/getting-started/toolbox/index.html index f9af6694..c8bca585 100644 --- a/build/2.0/getting-started/toolbox/index.html +++ b/build/2.0/getting-started/toolbox/index.html @@ -1013,6 +1013,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/getting-started/xcode/index.html b/build/2.0/getting-started/xcode/index.html index b48b5091..846ec64f 100644 --- a/build/2.0/getting-started/xcode/index.html +++ b/build/2.0/getting-started/xcode/index.html @@ -980,6 +980,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/http/body/index.html b/build/2.0/http/body/index.html index 515a95f3..31d8ffa8 100644 --- a/build/2.0/http/body/index.html +++ b/build/2.0/http/body/index.html @@ -917,6 +917,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/http/client/index.html b/build/2.0/http/client/index.html index 4ad50322..b6c99386 100644 --- a/build/2.0/http/client/index.html +++ b/build/2.0/http/client/index.html @@ -917,6 +917,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/http/cors/index.html b/build/2.0/http/cors/index.html index 6ea65e41..e6562d74 100644 --- a/build/2.0/http/cors/index.html +++ b/build/2.0/http/cors/index.html @@ -917,6 +917,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/http/middleware/index.html b/build/2.0/http/middleware/index.html index d9774dd3..07e1cc24 100644 --- a/build/2.0/http/middleware/index.html +++ b/build/2.0/http/middleware/index.html @@ -917,6 +917,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/http/package/index.html b/build/2.0/http/package/index.html index d58c32f0..e9f236e2 100644 --- a/build/2.0/http/package/index.html +++ b/build/2.0/http/package/index.html @@ -917,6 +917,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/http/request/index.html b/build/2.0/http/request/index.html index e1c83463..8f871fb4 100644 --- a/build/2.0/http/request/index.html +++ b/build/2.0/http/request/index.html @@ -917,6 +917,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/http/responder/index.html b/build/2.0/http/responder/index.html index 53c50bf9..41edf31a 100644 --- a/build/2.0/http/responder/index.html +++ b/build/2.0/http/responder/index.html @@ -917,6 +917,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/http/response-representable/index.html b/build/2.0/http/response-representable/index.html index 60c266bf..1d72bbb4 100644 --- a/build/2.0/http/response-representable/index.html +++ b/build/2.0/http/response-representable/index.html @@ -917,6 +917,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/http/response/index.html b/build/2.0/http/response/index.html index 42066e4a..3084ecdc 100644 --- a/build/2.0/http/response/index.html +++ b/build/2.0/http/response/index.html @@ -917,6 +917,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/http/server/index.html b/build/2.0/http/server/index.html index 7a12f4c3..09d55f84 100644 --- a/build/2.0/http/server/index.html +++ b/build/2.0/http/server/index.html @@ -917,6 +917,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/index.html b/build/2.0/index.html index da079edd..809e4298 100644 --- a/build/2.0/index.html +++ b/build/2.0/index.html @@ -1069,6 +1069,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/json/overview/index.html b/build/2.0/json/overview/index.html index 3554b88a..4445d688 100644 --- a/build/2.0/json/overview/index.html +++ b/build/2.0/json/overview/index.html @@ -1000,6 +1000,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/json/package/index.html b/build/2.0/json/package/index.html index 25574621..34c7d448 100644 --- a/build/2.0/json/package/index.html +++ b/build/2.0/json/package/index.html @@ -960,6 +960,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/leaf/leaf/index.html b/build/2.0/leaf/leaf/index.html index c5e66b98..eed6a89e 100644 --- a/build/2.0/leaf/leaf/index.html +++ b/build/2.0/leaf/leaf/index.html @@ -917,6 +917,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/leaf/package/index.html b/build/2.0/leaf/package/index.html index c5064685..fd72b65d 100644 --- a/build/2.0/leaf/package/index.html +++ b/build/2.0/leaf/package/index.html @@ -917,6 +917,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/leaf/provider/index.html b/build/2.0/leaf/provider/index.html index eb4fba58..e62c1248 100644 --- a/build/2.0/leaf/provider/index.html +++ b/build/2.0/leaf/provider/index.html @@ -917,6 +917,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/mkdocs/search_index.json b/build/2.0/mkdocs/search_index.json index f2837ad8..13425165 100644 --- a/build/2.0/mkdocs/search_index.json +++ b/build/2.0/mkdocs/search_index.json @@ -1925,6 +1925,56 @@ "text": "Now we can make a request to our Vapor app. GET /me HTTP / 1.1 Authorization : Basic dmFwb3I6Zm9v Note dmFwb3I6Zm9v is \"vapor:foo\" base64 encoded where \"vapor\" is the username and \n\"foo\" is the password. This is the format of Basic authorization headers. And we should get a response like. HTTP / 1.1 200 OK Content-Type : text/plain Set-Cookie : vapor-session=... \n\nVapor Notice the vapor-session in the response. This can be used in subsequent requests instead of \nthe username and password.", "title": "Request" }, + { + "location": "/auth/redirect-middleware/", + "text": "Redirect Middlewares\n\n\nIncluded in the \nAuthProvider\n package are \nRedirectMiddleware\n and \nInverseRedirectMiddleware\n classes that will help you \nredirect unauthenticated or authenticated requests to a given path. This is especially useful for redirecting users away from secure\npages to a login page and vice versa.\n\n\nRedirect Middleware\n\n\nLet's take a look at how to add a \nRedirectMiddleware\n to your application.\n\n\nExisting Auth\n\n\nSince we only want this middleware to apply to secure pages, we'll apply it using route groups.\n\n\nYou should already have a protected area in your application using one of the authentication middlewares.\n\n\nimport\n \nVapor\n\n\nimport\n \nAuthProvider\n\n\n\nlet\n \ndrop\n \n=\n \ntry\n \nDroplet\n()\n\n\n\ndrop\n.\nget\n(\nlogin\n)\n \n{\n \nreq\n \nin\n\n \nreturn\n \n// some login form\n\n\n}\n\n\n\nlet\n \nauth\n \n=\n \nTokenAuthenticationMiddleware\n(\nUser\n.\nself\n)\n\n\nlet\n \nprotected\n \n=\n \ndrop\n.\ngrouped\n([\nauth\n])\n\n\nprotected\n.\nget\n(\nsecure\n)\n \n{\n \nreq\n \nin\n\n \nlet\n \nuser\n \n=\n \ntry\n \nreq\n.\nauth\n.\nassertAuthenticated\n(\nUser\n.\nself\n)\n\n \nreturn\n \nWelcome to the secure page, \n\\(\nuser\n.\nname\n)\n\n\n}\n\n\n\n\n\n\nThe above snippet protects access to the page at \nGET /secure\n using the \nTokenAuthenticationMiddleware\n. \n\n\nSince we've applied \nTokenAuthenticationMiddleware\n, this page cannot be accessed by anyone not authenticated.\nAlthough this is perfectly secure, we should provide a better experience for unauthenticated users. Instead of \njust showing them an error message, we can redirect them to the login page.\n\n\nAdd Redirect\n\n\nCreating a redirect middleware is very simple. We'll use one of the presets for redirecting a user to \n/login\n.\n\n\nlet\n \nredirect\n \n=\n \nRedirectMiddleware\n.\nlogin\n()\n\n\n\n\n\n\nNow we just need to add this redirect middleware to our \nprotected\n route group mentioned previously.\n\n\nlet\n \nprotected\n \n=\n \ndrop\n.\ngrouped\n([\nredirect\n,\n \nauth\n])\n\n\n\n\n\n\n\n\nWarning\n\n\nMake sure the redirect middleware comes \nbefore\n the auth middleware. \n\n\n\n\nComplete Example\n\n\nNow whenever an unauthenticated user attemps to visit \nGET /secure\n, they will be redirected to \nGET /login\n.\n\n\nimport\n \nVapor\n\n\nimport\n \nAuthProvider\n\n\n\nlet\n \ndrop\n \n=\n \ntry\n \nDroplet\n()\n\n\n\nlet\n \nredirect\n \n=\n \nRedirectMiddleware\n.\nlogin\n()\n\n\nlet\n \nauth\n \n=\n \nTokenAuthenticationMiddleware\n(\nTestUser\n.\nself\n)\n\n\n\nlet\n \nprotected\n \n=\n \ndrop\n.\ngrouped\n([\nredirect\n,\n \nauth\n])\n\n\nprotected\n.\nget\n \n{\n \nreq\n \nin\n\n \nlet\n \nuser\n \n=\n \ntry\n \nreq\n.\nauth\n.\nassertAuthenticated\n(\nTestUser\n.\nself\n)\n\n \nreturn\n \nWelcome to the dashboard, \n\\(\nuser\n.\nname\n)\n\n\n}\n\n\n\n\n\n\nCustom Route\n\n\nIf your login page is not \n/login\n or you'd like the redirect middleware to redirect to a different type of page, \nsimply use the full initializer.\n\n\nlet\n \nredirect\n \n=\n \nRedirectMiddleware\n(\npath\n:\n \n/foo\n)\n\n\n\n\n\n\nInverse Redirect Middleware\n\n\nComplementary to the \nRedirectMiddleware\n is the \nInverseRedirectMiddleware\n. Just like you want to redirect unauthenticated\nusers away from secure pages, you also might want to redirect \nauthenticated\n users away from certain pages.\n\n\nFor example, if a user is already authenticated and they visit the login page, they might be confused and attempt to login again.\n\n\nExample\n\n\nHere is an example of the \nInverseRedirectMiddleware\n being used to redirect authenticated \nUser\ns away from the login page.\n\n\nWe are using the preset \n.home()\n convenience, which redirects the user to \nGET /\n.\n\n\nimport\n \nVapor\n\n\nimport\n \nAuthProvider\n\n\n\nlet\n \ndrop\n \n=\n \ntry\n \nDroplet\n()\n\n\n\nlet\n \nredirect\n \n=\n \nInverseRedirectMiddleware\n.\nhome\n(\nUser\n.\nself\n)\n\n\nlet\n \ngroup\n \n=\n \ndrop\n.\ngrouped\n([\nredirect\n])\n\n\ngroup\n.\nget\n(\nlogin\n)\n \n{\n \nreq\n \nin\n\n \nreturn\n \nPlease login\n\n\n}\n\n\n\n\n\n\nCustom Route\n\n\nIf your desired page is not \n/\n or you'd like the inverse redirect middleware to redirect to a different type of page, \nsimply use the full initializer.\n\n\nlet\n \nredirect\n \n=\n \nInverseRedirectMiddleware\n(\nUser\n.\nself\n,\n \npath\n:\n \n/foo\n)", + "title": "Redirect Middleware" + }, + { + "location": "/auth/redirect-middleware/#redirect-middlewares", + "text": "Included in the AuthProvider package are RedirectMiddleware and InverseRedirectMiddleware classes that will help you \nredirect unauthenticated or authenticated requests to a given path. This is especially useful for redirecting users away from secure\npages to a login page and vice versa.", + "title": "Redirect Middlewares" + }, + { + "location": "/auth/redirect-middleware/#redirect-middleware", + "text": "Let's take a look at how to add a RedirectMiddleware to your application.", + "title": "Redirect Middleware" + }, + { + "location": "/auth/redirect-middleware/#existing-auth", + "text": "Since we only want this middleware to apply to secure pages, we'll apply it using route groups. You should already have a protected area in your application using one of the authentication middlewares. import Vapor import AuthProvider let drop = try Droplet () drop . get ( login ) { req in \n return // some login form } let auth = TokenAuthenticationMiddleware ( User . self ) let protected = drop . grouped ([ auth ]) protected . get ( secure ) { req in \n let user = try req . auth . assertAuthenticated ( User . self ) \n return Welcome to the secure page, \\( user . name ) } The above snippet protects access to the page at GET /secure using the TokenAuthenticationMiddleware . Since we've applied TokenAuthenticationMiddleware , this page cannot be accessed by anyone not authenticated.\nAlthough this is perfectly secure, we should provide a better experience for unauthenticated users. Instead of \njust showing them an error message, we can redirect them to the login page.", + "title": "Existing Auth" + }, + { + "location": "/auth/redirect-middleware/#add-redirect", + "text": "Creating a redirect middleware is very simple. We'll use one of the presets for redirecting a user to /login . let redirect = RedirectMiddleware . login () Now we just need to add this redirect middleware to our protected route group mentioned previously. let protected = drop . grouped ([ redirect , auth ]) Warning Make sure the redirect middleware comes before the auth middleware.", + "title": "Add Redirect" + }, + { + "location": "/auth/redirect-middleware/#complete-example", + "text": "Now whenever an unauthenticated user attemps to visit GET /secure , they will be redirected to GET /login . import Vapor import AuthProvider let drop = try Droplet () let redirect = RedirectMiddleware . login () let auth = TokenAuthenticationMiddleware ( TestUser . self ) let protected = drop . grouped ([ redirect , auth ]) protected . get { req in \n let user = try req . auth . assertAuthenticated ( TestUser . self ) \n return Welcome to the dashboard, \\( user . name ) }", + "title": "Complete Example" + }, + { + "location": "/auth/redirect-middleware/#custom-route", + "text": "If your login page is not /login or you'd like the redirect middleware to redirect to a different type of page, \nsimply use the full initializer. let redirect = RedirectMiddleware ( path : /foo )", + "title": "Custom Route" + }, + { + "location": "/auth/redirect-middleware/#inverse-redirect-middleware", + "text": "Complementary to the RedirectMiddleware is the InverseRedirectMiddleware . Just like you want to redirect unauthenticated\nusers away from secure pages, you also might want to redirect authenticated users away from certain pages. For example, if a user is already authenticated and they visit the login page, they might be confused and attempt to login again.", + "title": "Inverse Redirect Middleware" + }, + { + "location": "/auth/redirect-middleware/#example", + "text": "Here is an example of the InverseRedirectMiddleware being used to redirect authenticated User s away from the login page. We are using the preset .home() convenience, which redirects the user to GET / . import Vapor import AuthProvider let drop = try Droplet () let redirect = InverseRedirectMiddleware . home ( User . self ) let group = drop . grouped ([ redirect ]) group . get ( login ) { req in \n return Please login }", + "title": "Example" + }, + { + "location": "/auth/redirect-middleware/#custom-route_1", + "text": "If your desired page is not / or you'd like the inverse redirect middleware to redirect to a different type of page, \nsimply use the full initializer. let redirect = InverseRedirectMiddleware ( User . self , path : /foo )", + "title": "Custom Route" + }, { "location": "/sessions/package/", "text": "Using Sessions\n\n\nThis module is a part of Vapor, just add:\n\n\nimport\n \nSessions", diff --git a/build/2.0/mysql/driver/index.html b/build/2.0/mysql/driver/index.html index cc4d24a1..5a6da010 100644 --- a/build/2.0/mysql/driver/index.html +++ b/build/2.0/mysql/driver/index.html @@ -967,6 +967,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/mysql/package/index.html b/build/2.0/mysql/package/index.html index 35501372..c70dfbd2 100644 --- a/build/2.0/mysql/package/index.html +++ b/build/2.0/mysql/package/index.html @@ -974,6 +974,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/mysql/provider/index.html b/build/2.0/mysql/provider/index.html index c0a5430d..ead5764c 100644 --- a/build/2.0/mysql/provider/index.html +++ b/build/2.0/mysql/provider/index.html @@ -1015,6 +1015,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/node/getting-started/index.html b/build/2.0/node/getting-started/index.html index aeb27305..d02975cd 100644 --- a/build/2.0/node/getting-started/index.html +++ b/build/2.0/node/getting-started/index.html @@ -917,6 +917,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/node/package/index.html b/build/2.0/node/package/index.html index de9be0ef..43a00cb2 100644 --- a/build/2.0/node/package/index.html +++ b/build/2.0/node/package/index.html @@ -917,6 +917,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/redis/package/index.html b/build/2.0/redis/package/index.html index 914a1ab1..867c5b9b 100644 --- a/build/2.0/redis/package/index.html +++ b/build/2.0/redis/package/index.html @@ -960,6 +960,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/redis/provider/index.html b/build/2.0/redis/provider/index.html index c50cfb58..a4729cac 100644 --- a/build/2.0/redis/provider/index.html +++ b/build/2.0/redis/provider/index.html @@ -994,6 +994,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/routing/collection/index.html b/build/2.0/routing/collection/index.html index 900e943a..fd739a14 100644 --- a/build/2.0/routing/collection/index.html +++ b/build/2.0/routing/collection/index.html @@ -960,6 +960,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/routing/group/index.html b/build/2.0/routing/group/index.html index 5ee7c835..cf6e7996 100644 --- a/build/2.0/routing/group/index.html +++ b/build/2.0/routing/group/index.html @@ -981,6 +981,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/routing/overview/index.html b/build/2.0/routing/overview/index.html index 671d5a09..be2b9452 100644 --- a/build/2.0/routing/overview/index.html +++ b/build/2.0/routing/overview/index.html @@ -1015,6 +1015,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/routing/package/index.html b/build/2.0/routing/package/index.html index 9767094f..36089bfa 100644 --- a/build/2.0/routing/package/index.html +++ b/build/2.0/routing/package/index.html @@ -960,6 +960,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/routing/parameters/index.html b/build/2.0/routing/parameters/index.html index 87f2b383..d195cc11 100644 --- a/build/2.0/routing/parameters/index.html +++ b/build/2.0/routing/parameters/index.html @@ -980,6 +980,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/sessions/package/index.html b/build/2.0/sessions/package/index.html index f2aa27d7..dfab08e6 100644 --- a/build/2.0/sessions/package/index.html +++ b/build/2.0/sessions/package/index.html @@ -917,6 +917,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + @@ -1580,7 +1592,7 @@ diff --git a/build/2.0/sessions/sessions/index.html b/build/2.0/sessions/sessions/index.html index 5e0299e0..79bbb6a2 100644 --- a/build/2.0/sessions/sessions/index.html +++ b/build/2.0/sessions/sessions/index.html @@ -917,6 +917,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/sitemap.xml b/build/2.0/sitemap.xml index 56ee23fb..ce78b2af 100644 --- a/build/2.0/sitemap.xml +++ b/build/2.0/sitemap.xml @@ -4,7 +4,7 @@ / - 2017-07-20 + 2017-07-26 daily @@ -13,37 +13,37 @@ /getting-started/install-on-macos/ - 2017-07-20 + 2017-07-26 daily /getting-started/install-on-ubuntu/ - 2017-07-20 + 2017-07-26 daily /getting-started/toolbox/ - 2017-07-20 + 2017-07-26 daily /getting-started/hello-world/ - 2017-07-20 + 2017-07-26 daily /getting-started/manual/ - 2017-07-20 + 2017-07-26 daily /getting-started/xcode/ - 2017-07-20 + 2017-07-26 daily @@ -53,49 +53,49 @@ /vapor/folder-structure/ - 2017-07-20 + 2017-07-26 daily /vapor/droplet/ - 2017-07-20 + 2017-07-26 daily /vapor/views/ - 2017-07-20 + 2017-07-26 daily /vapor/controllers/ - 2017-07-20 + 2017-07-26 daily /vapor/provider/ - 2017-07-20 + 2017-07-26 daily /vapor/hash/ - 2017-07-20 + 2017-07-26 daily /vapor/log/ - 2017-07-20 + 2017-07-26 daily /vapor/commands/ - 2017-07-20 + 2017-07-26 daily @@ -105,7 +105,7 @@ /configs/config/ - 2017-07-20 + 2017-07-26 daily @@ -115,13 +115,13 @@ /json/package/ - 2017-07-20 + 2017-07-26 daily /json/overview/ - 2017-07-20 + 2017-07-26 daily @@ -131,31 +131,31 @@ /routing/package/ - 2017-07-20 + 2017-07-26 daily /routing/overview/ - 2017-07-20 + 2017-07-26 daily /routing/parameters/ - 2017-07-20 + 2017-07-26 daily /routing/group/ - 2017-07-20 + 2017-07-26 daily /routing/collection/ - 2017-07-20 + 2017-07-26 daily @@ -165,37 +165,37 @@ /fluent/package/ - 2017-07-20 + 2017-07-26 daily /fluent/getting-started/ - 2017-07-20 + 2017-07-26 daily /fluent/model/ - 2017-07-20 + 2017-07-26 daily /fluent/database/ - 2017-07-20 + 2017-07-26 daily /fluent/query/ - 2017-07-20 + 2017-07-26 daily /fluent/relations/ - 2017-07-20 + 2017-07-26 daily @@ -205,13 +205,13 @@ /cache/package/ - 2017-07-20 + 2017-07-26 daily /cache/overview/ - 2017-07-20 + 2017-07-26 daily @@ -221,19 +221,19 @@ /mysql/package/ - 2017-07-20 + 2017-07-26 daily /mysql/provider/ - 2017-07-20 + 2017-07-26 daily /mysql/driver/ - 2017-07-20 + 2017-07-26 daily @@ -243,13 +243,13 @@ /redis/package/ - 2017-07-20 + 2017-07-26 daily /redis/provider/ - 2017-07-20 + 2017-07-26 daily @@ -259,37 +259,43 @@ /auth/package/ - 2017-07-20 + 2017-07-26 daily /auth/provider/ - 2017-07-20 + 2017-07-26 daily /auth/getting-started/ - 2017-07-20 + 2017-07-26 daily /auth/helper/ - 2017-07-20 + 2017-07-26 daily /auth/password/ - 2017-07-20 + 2017-07-26 daily /auth/persist/ - 2017-07-20 + 2017-07-26 + daily + + + + /auth/redirect-middleware/ + 2017-07-26 daily @@ -299,13 +305,13 @@ /sessions/package/ - 2017-07-20 + 2017-07-26 daily /sessions/sessions/ - 2017-07-20 + 2017-07-26 daily @@ -315,61 +321,61 @@ /http/package/ - 2017-07-20 + 2017-07-26 daily /http/request/ - 2017-07-20 + 2017-07-26 daily /http/response/ - 2017-07-20 + 2017-07-26 daily /http/middleware/ - 2017-07-20 + 2017-07-26 daily /http/body/ - 2017-07-20 + 2017-07-26 daily /http/response-representable/ - 2017-07-20 + 2017-07-26 daily /http/responder/ - 2017-07-20 + 2017-07-26 daily /http/client/ - 2017-07-20 + 2017-07-26 daily /http/server/ - 2017-07-20 + 2017-07-26 daily /http/cors/ - 2017-07-20 + 2017-07-26 daily @@ -379,19 +385,19 @@ /leaf/package/ - 2017-07-20 + 2017-07-26 daily /leaf/provider/ - 2017-07-20 + 2017-07-26 daily /leaf/leaf/ - 2017-07-20 + 2017-07-26 daily @@ -401,13 +407,13 @@ /validation/package/ - 2017-07-20 + 2017-07-26 daily /validation/overview/ - 2017-07-20 + 2017-07-26 daily @@ -417,13 +423,13 @@ /node/package/ - 2017-07-20 + 2017-07-26 daily /node/getting-started/ - 2017-07-20 + 2017-07-26 daily @@ -433,13 +439,13 @@ /core/package/ - 2017-07-20 + 2017-07-26 daily /core/overview/ - 2017-07-20 + 2017-07-26 daily @@ -449,13 +455,13 @@ /bits/package/ - 2017-07-20 + 2017-07-26 daily /bits/overview/ - 2017-07-20 + 2017-07-26 daily @@ -465,13 +471,13 @@ /debugging/package/ - 2017-07-20 + 2017-07-26 daily /debugging/overview/ - 2017-07-20 + 2017-07-26 daily @@ -481,13 +487,13 @@ /deploy/nginx/ - 2017-07-20 + 2017-07-26 daily /deploy/supervisor/ - 2017-07-20 + 2017-07-26 daily @@ -497,19 +503,19 @@ /version/1_5/ - 2017-07-20 + 2017-07-26 daily /version/2_0/ - 2017-07-20 + 2017-07-26 daily /version/support/ - 2017-07-20 + 2017-07-26 daily diff --git a/build/2.0/validation/overview/index.html b/build/2.0/validation/overview/index.html index 1194b5eb..e4d235c5 100644 --- a/build/2.0/validation/overview/index.html +++ b/build/2.0/validation/overview/index.html @@ -917,6 +917,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/validation/package/index.html b/build/2.0/validation/package/index.html index 662e3c47..515768c0 100644 --- a/build/2.0/validation/package/index.html +++ b/build/2.0/validation/package/index.html @@ -917,6 +917,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/vapor/commands/index.html b/build/2.0/vapor/commands/index.html index fc4916e5..ea2debbd 100644 --- a/build/2.0/vapor/commands/index.html +++ b/build/2.0/vapor/commands/index.html @@ -928,6 +928,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/vapor/controllers/index.html b/build/2.0/vapor/controllers/index.html index 18668333..6005ed73 100644 --- a/build/2.0/vapor/controllers/index.html +++ b/build/2.0/vapor/controllers/index.html @@ -1000,6 +1000,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/vapor/droplet/index.html b/build/2.0/vapor/droplet/index.html index 9ae72973..95b2a9db 100644 --- a/build/2.0/vapor/droplet/index.html +++ b/build/2.0/vapor/droplet/index.html @@ -1021,6 +1021,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/vapor/folder-structure/index.html b/build/2.0/vapor/folder-structure/index.html index 69f1dde3..9c088f47 100644 --- a/build/2.0/vapor/folder-structure/index.html +++ b/build/2.0/vapor/folder-structure/index.html @@ -981,6 +981,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/vapor/hash/index.html b/build/2.0/vapor/hash/index.html index 63f2868e..05c9405a 100644 --- a/build/2.0/vapor/hash/index.html +++ b/build/2.0/vapor/hash/index.html @@ -1087,6 +1087,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/vapor/log/index.html b/build/2.0/vapor/log/index.html index e2039d98..7af3ac60 100644 --- a/build/2.0/vapor/log/index.html +++ b/build/2.0/vapor/log/index.html @@ -928,6 +928,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/vapor/provider/index.html b/build/2.0/vapor/provider/index.html index 59fd5786..14f81470 100644 --- a/build/2.0/vapor/provider/index.html +++ b/build/2.0/vapor/provider/index.html @@ -1014,6 +1014,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/vapor/views/index.html b/build/2.0/vapor/views/index.html index 34a0d2a7..6964547e 100644 --- a/build/2.0/vapor/views/index.html +++ b/build/2.0/vapor/views/index.html @@ -988,6 +988,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/version/1_5/index.html b/build/2.0/version/1_5/index.html index 3afedb66..4078ef72 100644 --- a/build/2.0/version/1_5/index.html +++ b/build/2.0/version/1_5/index.html @@ -917,6 +917,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/version/2_0/index.html b/build/2.0/version/2_0/index.html index a7b4a17f..fd2654cb 100644 --- a/build/2.0/version/2_0/index.html +++ b/build/2.0/version/2_0/index.html @@ -917,6 +917,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + + diff --git a/build/2.0/version/support/index.html b/build/2.0/version/support/index.html index 9652201d..adc0841d 100644 --- a/build/2.0/version/support/index.html +++ b/build/2.0/version/support/index.html @@ -917,6 +917,18 @@ + + + + + +
  • + + Redirect Middleware + +
  • + +