mirror of https://github.com/vapor/docs.git
syntax + css updates
This commit is contained in:
parent
0834936a36
commit
767dceb3d9
|
|
@ -38,7 +38,7 @@ let auth = AuthMiddleware(user: User.self) { value in
|
|||
|
||||
### Cache
|
||||
|
||||
A custom `CacheProtocol` can be passed as well. The `MemoryCache()` used by default is not persisted between server restarts and does not allow for sharing between multiple running instances.
|
||||
A custom `CacheProtocol` can be passed as well. The `MemoryCache` used by default is not persisted between server restarts and does not allow for sharing between multiple running instances.
|
||||
|
||||
```swift
|
||||
import VaporRedis
|
||||
|
|
|
|||
|
|
@ -30,4 +30,4 @@ drop.grouped(protect).group("secure") { secure in
|
|||
}
|
||||
```
|
||||
|
||||
Visiting `GET secure/about` will return the authorized user, or an error if no user is authorized.
|
||||
Visiting `GET /secure/about` will return the authorized user, or an error if no user is authorized.
|
||||
|
|
|
|||
|
|
@ -24,7 +24,9 @@ Basic authorization consists of a username and password concatenated into a stri
|
|||
Authorization: Basic QWxhZGRpbjpPcGVuU2VzYW1l
|
||||
```
|
||||
|
||||
Here is what an example header looks like. You can read more about basic auth on [wikipedia](https://en.wikipedia.org/wiki/Basic_access_authentication).
|
||||
This is what an example header looks like. You can read more about basic auth on [wikipedia](https://en.wikipedia.org/wiki/Basic_access_authentication).
|
||||
|
||||
Below is how you access this header using `req.auth`.
|
||||
|
||||
```swift
|
||||
guard let credentials = req.auth.header?.basic else {
|
||||
|
|
@ -63,13 +65,13 @@ To access the raw authorization header, use `req.auth.header?.header`.
|
|||
|
||||
## Credentials
|
||||
|
||||
Both Basic and Bearer return something that conforms to `Credentials`. You can always create a custom `Credentials` object for authorization by conforming your own class to `Credentials` or by manually creating an `APIKey` or `AccessToken`.
|
||||
Both Basic and Bearer return something that conforms to `Credentials`. You can always create a custom `Credentials` object for authentication by conforming your own class to `Credentials` or by manually creating an `APIKey`, `AccessToken`, or `Identifier`.
|
||||
|
||||
```swift
|
||||
let key = AccessToken(string: "apikey123")
|
||||
```
|
||||
|
||||
### Form
|
||||
### Input
|
||||
|
||||
You can also create credentials from form or JSON data.
|
||||
|
||||
|
|
@ -96,26 +98,25 @@ If this call succeeds, the user is logged in and a session has been started. The
|
|||
|
||||
### Authenticate
|
||||
|
||||
Logging in calls the `authenticate` method on the `Realm`. If you have simply passed an `Auth.User` conformer to the `AuthMiddleware`, then this will call the `authenticate` method on that type.
|
||||
Logging in calls the `authenticate` method on `Auth.User` model you supplied to the `AuthMiddleware`. Make sure you add support for all the credential types you may want to use.
|
||||
|
||||
The method will be passed whichever credentials you are trying to login with, so make sure you add support for all the credential types you may want to use on your `Auth.User`.
|
||||
> Note: If you used a custom Realm, it will be called instead.
|
||||
|
||||
### Identifier
|
||||
|
||||
One important credential type is the `Identifier` type. This is used by Vapor when fetching the `User` object from the authorization cookie. It is also a convenient way to log a user in manually.
|
||||
Another important credential type is the `Identifier` type. This is used by Vapor when fetching the `User` object from the `vapor-auth` cookie. It is also a convenient way to log a user in manually.
|
||||
|
||||
```swift
|
||||
static func authenticate(credentials: Credentials) throws -> Auth.User {
|
||||
if ... {
|
||||
...
|
||||
} else if let id = credentials as? Identifier {
|
||||
switch credentials {
|
||||
...
|
||||
case id as Identifier:
|
||||
guard let user = try User.find(id.id) else {
|
||||
throw Abort.custom(status: .badRequest, message: "Invalid identifier.")
|
||||
}
|
||||
|
||||
return user
|
||||
} else {
|
||||
...
|
||||
...
|
||||
}
|
||||
}
|
||||
```
|
||||
|
|
@ -137,9 +138,11 @@ If you just want to log the user in for a single request, disable persistance.
|
|||
req.auth.login(credentials, persist: false)
|
||||
```
|
||||
|
||||
> Note: Supporting `Identifier` credentials is required for persisted authentication to work properly.
|
||||
|
||||
## User
|
||||
|
||||
By default, `request.auth.user()` returns the authorized `Auth.User`. This will need to be casted to your internal user type if you want to access its values.
|
||||
By default, `request.auth.user()` returns the authorized `Auth.User`. This will need to be casted to your internal `User` type for use.
|
||||
|
||||
Adding a convenience method on `Request` is a great way to simplify this.
|
||||
|
||||
|
|
@ -155,4 +158,4 @@ extension Request {
|
|||
}
|
||||
```
|
||||
|
||||
Now you can get access to your `User` type with `try req.user()`.
|
||||
Now you can access your `User` type with `try req.user()`.
|
||||
|
|
|
|||
14
auth/user.md
14
auth/user.md
|
|
@ -46,9 +46,9 @@ A user is authenticated when a set of credentials is passed to the static `authe
|
|||
protocol Credentials { }
|
||||
```
|
||||
|
||||
The credentials protocol is an empty protocol that any type can conform to. This gives great flexibility to your authorization model, but also requires that you properly handle the case of unsupported credential types.
|
||||
The credentials protocol is an empty protocol that any type can conform to. This gives great flexibility to your authentication model, but also requires that you properly handle the case of unsupported credential types.
|
||||
|
||||
#### AccessToken
|
||||
#### Access Token
|
||||
|
||||
One of the simplest credential types included is `AccessToken`. It carries a `String` based token that will be used to authenticate the user.
|
||||
|
||||
|
|
@ -80,7 +80,11 @@ Once we have found the user associated with the supplied access token, we simply
|
|||
|
||||
Vapor uses the `Identifier` credential type internally to lookup users from sessions. You can read more in the [Request](request.md) section.
|
||||
|
||||
### Many Credentials
|
||||
### Register
|
||||
|
||||
Similar to the authenticate method, the register method takes credentials. But instead of fetching the user from the data store, it provides a convenient way to create the user. You are not required to register your users through this method.
|
||||
|
||||
## Example
|
||||
|
||||
Here is an example of a User that supports multiple credentials.
|
||||
|
||||
|
|
@ -114,7 +118,3 @@ extension User: Auth.User {
|
|||
```
|
||||
|
||||
> Note: Try not to store passwords. If you must, hash and salt them.
|
||||
|
||||
### Register
|
||||
|
||||
Similar to the authenticate method, the register method takes credentials. But instead of fetching the user from the data store, it provides a convenient way to create the user. You are not required to register your users through this method.
|
||||
|
|
|
|||
|
|
@ -17,6 +17,12 @@ a {
|
|||
a:hover {
|
||||
color: #f6cfcf; }
|
||||
|
||||
h1, h2, h3, h4, h5, h6 {
|
||||
color: #333;
|
||||
margin: 0;
|
||||
font-family: "Quicksand";
|
||||
letter-spacing: -1px; }
|
||||
|
||||
h1 {
|
||||
font-size: 38px;
|
||||
font-weight: 200; }
|
||||
|
|
@ -30,13 +36,13 @@ h2 {
|
|||
|
||||
h3 {
|
||||
font-size: 24px;
|
||||
font-weight: 400; }
|
||||
font-weight: 400;
|
||||
margin-bottom: 6px; }
|
||||
|
||||
h1, h2, h3, h4, h5, h6 {
|
||||
color: #333;
|
||||
margin: 0;
|
||||
font-family: "Quicksand";
|
||||
letter-spacing: -1px; }
|
||||
h4 {
|
||||
font-size: 18px;
|
||||
font-weight: 400;
|
||||
margin-bottom: 6px; }
|
||||
|
||||
:target {
|
||||
background: #ffff99; }
|
||||
|
|
@ -293,7 +299,7 @@ p code, li code {
|
|||
border-radius: 5px;
|
||||
padding: 3px 5px;
|
||||
display: inline-block;
|
||||
color: #92a0b9;
|
||||
color: #6b7891;
|
||||
box-shadow: 0 1px 0px 0px rgba(0, 0, 0, 0.1); }
|
||||
|
||||
pre {
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
|
|
@ -20,6 +20,13 @@ a
|
|||
a:hover
|
||||
color: #f6cfcf
|
||||
|
||||
|
||||
h1, h2, h3, h4, h5, h6
|
||||
color: #333
|
||||
margin: 0
|
||||
font-family: 'Quicksand'
|
||||
letter-spacing: -1px
|
||||
|
||||
h1
|
||||
font-size: 38px
|
||||
@media screen and (min-width: $tablet)
|
||||
|
|
@ -33,12 +40,12 @@ h2
|
|||
h3
|
||||
font-size: 24px
|
||||
font-weight: 400
|
||||
margin-bottom: 6px
|
||||
|
||||
h1, h2, h3, h4, h5, h6
|
||||
color: #333
|
||||
margin: 0
|
||||
font-family: 'Quicksand'
|
||||
letter-spacing: -1px
|
||||
h4
|
||||
font-size: 18px
|
||||
font-weight: 400
|
||||
margin-bottom: 6px
|
||||
|
||||
\:target
|
||||
background: #ffff99
|
||||
|
|
@ -329,7 +336,7 @@ p code, li code
|
|||
border-radius: 5px
|
||||
padding: 3px 5px
|
||||
display: inline-block
|
||||
color: #92a0b9
|
||||
color: #6b7891
|
||||
box-shadow: 0 1px 0px 0px rgba(0, 0, 0, 0.1)
|
||||
|
||||
pre
|
||||
|
|
|
|||
Loading…
Reference in New Issue