Skip to content

Commit

Permalink
Doc: Cookie (#974)
Browse files Browse the repository at this point in the history
* doc: cookie

* doc: cookie

* cookie: changes
  • Loading branch information
ShrutiVerma97 authored Feb 8, 2022
1 parent d3fa639 commit 5e4a0cf
Showing 1 changed file with 110 additions and 1 deletion.
111 changes: 110 additions & 1 deletion docs/website/docs/v1.x/dsl/cookies/index.md
Original file line number Diff line number Diff line change
@@ -1 +1,110 @@
# Work in progress
# Cookie

**ZIO HTTP** has special support for Cookie headers using the `Cookie` Domain to add and invalidate cookies. Adding a cookie will generate the correct [Set-Cookie](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie) headers

## Create a Cookie

`Cookie` can be created with params `name`, `content`, `expires`, `domain`, `path`, `isSecure`, `isHttpOnly`, `maxAge`, `sameSite` and `secret` according to HTTP [Set-Cookie](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie)

The below snippet creates a cookie `name` as `id` and `content` as `abc` with default params.
```scala
val cookie: Cookie = Cookie("id", "abc")
```
### Update a Cookie

- `withContent` updates the content of cookie
```scala
val newCookie = cookie.withContent("def")
```
- `withExpiry` updates the expiration date of cookie
```scala
val newCookie = cookie.withExpiry(Instant.MAX)
```
- `withMaxAge` updates the max-age of the cookie
```scala
val newCookie = cookie.withMaxAge(5 days)
```
- `withDomain` updates the host to which the cookie will be sent
```scala
val newCookie = cookie.withDomain("example.com")
```
- `withPath` updates the path of the cookie
```scala
val newCookie = cookie.withPath(!! / "cookie")
```
- `withSecure` enables cookie only on https server
```scala
val newCookie = cookie.withSecure
```
- `withHttpOnly` forbids JavaScript from accessing the cookie
```scala
val newCookie = cookie.withHttpOnly
```
- `withSameSite` updates whether or not a cookie is sent with cross-origin requests
```scala
val newCookie = cookie.withSameSite(Instant.MAX)
```

## Reset a Cookie

you can reset cookie params using:
- `withoutSecure` resets `isSecure` to `false` in cookie
- `withoutHttpOnly` resets `isHttpOnly` to `false` in cookie
- `withoutExpiry` resets `expires` to `None`
- `withoutDomain` resets `domain` to `None`
- `withoutPath` resets `path` to `None`
- `withoutMaxAge` resets `maxAge` to `None`
- `withoutSameSite` resets `sameSite` to `None`

## Sign a Cookie

The cookies can be signed with a signature:

- Using `sign`
To sign a cookie, you can use `sign`
```scala
val cookie = Cookie("key", "hello").withMaxAge(5 days)
val app = Http.collect[Request] { case Method.GET -> !! / "cookie" =>
Response.ok.addCookie(cookie.sign("secret"))
}
```
- Using `signCookies` middleware

To sign all the cookies in your `HttpApp`, you can use `signCookies` middleware:
```scala
private val cookie = Cookie("key", "hello").withMaxAge(5 days)
private val app = Http.collect[Request] {
case Method.GET -> !! / "cookie" => Response.ok.addCookie(cookie)
case Method.GET -> !! / "secure-cookie" => Response.ok.addCookie(cookie.withSecure)
}

// Run it like any simple app
override def run(args: List[String]): URIO[zio.ZEnv, ExitCode] =
Server.start(8090, app @@ signCookies("secret")).exitCode
```

## Adding Cookie in Response

The cookies can be added in `Response` headers:
```scala
val cookie1: Cookie = Cookie("id", "abc")
val res = Response.ok.addCookie(cookie1)
```
It updates the response header `Set-Cookie` as

```Set-Cookie: <cookie-name>=<cookie-value>```

## Getting Cookie from Request

In HTTP requests, cookies are stored in the `cookie` header.
`cookiesDecoded` can be used to get all the cookies in the request.

```scala
private val app = Http.collect[Request] {
case req @ Method.GET -> !! / "cookie" =>
Response.text(req.cookiesDecoded.mkString(""))
}
```



0 comments on commit 5e4a0cf

Please sign in to comment.