Add writeCookie signature

This commit is contained in:
Tobe O 2019-08-16 09:53:15 -04:00
parent c045da3e03
commit 304545f441
2 changed files with 10 additions and 4 deletions

View file

@ -26,9 +26,7 @@ main() async {
app.get('/getid', (req, res) { app.get('/getid', (req, res) {
// Write the uniqid cookie. // Write the uniqid cookie.
var uniqid = rnd.nextInt(65536); var uniqid = rnd.nextInt(65536);
signer.writeCookies(res, [ signer.writeCookie(res, Cookie('uniqid', uniqid.toString()));
Cookie('uniqid', uniqid.toString()),
]);
// Send a response. // Send a response.
res.write('uniqid=$uniqid'); res.write('uniqid=$uniqid');

View file

@ -79,12 +79,20 @@ class CookieSigner {
} }
} }
/// Signs a single [cookie], and adds it to an outgoing
/// [res]ponse. The input [cookie] is not modified.
///
/// See [createSignedCookie].
void writeCookie(ResponseContext res, Cookie cookie) {
res.cookies.add(createSignedCookie(cookie));
}
/// Signs a set of [cookies], and adds them to an outgoing /// Signs a set of [cookies], and adds them to an outgoing
/// [res]ponse. The input [cookies] are not modified. /// [res]ponse. The input [cookies] are not modified.
/// ///
/// See [createSignedCookie]. /// See [createSignedCookie].
void writeCookies(ResponseContext res, Iterable<Cookie> cookies) { void writeCookies(ResponseContext res, Iterable<Cookie> cookies) {
res.cookies.addAll(cookies.map(createSignedCookie)); cookies.forEach((c) => writeCookie(res, c));
} }
/// Returns a new cookie, replacing the value of an input /// Returns a new cookie, replacing the value of an input