Created
March 10, 2019 00:48
-
-
Save masatomix/44b875b514e5340c84e84451a9e0ad6f to your computer and use it in GitHub Desktop.
Hosting,Functions,Cookie
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 characters
import * as functions from 'firebase-functions' | |
import * as cookie from 'cookie' | |
// // Start writing Firebase Functions | |
// // https://firebase.google.com/docs/functions/typescript | |
// | |
export const addCookie = functions.https.onRequest((request, response) => { | |
response.setHeader('Cache-Control', 'private') // Hosting経由だと、これがないとset cookieが削除される | |
_addCookie(response, 'key', 'value') | |
response.send('Hello from Firebase!') | |
}) | |
export const getCookie = functions.https.onRequest((request, response) => { | |
// response.setHeader('Cache-Control', 'private') | |
const cookies = cookie.parse(request.headers.cookie || '') | |
const sessionState = cookies.state | |
response.send(sessionState) | |
}) | |
function _addCookie (res, key, value) { | |
const expiresIn = 60 * 60 * 24 | |
const options = { maxAge: expiresIn, httpOnly: true } | |
// const options = { maxAge: expiresIn, httpOnly: true, secure: true } | |
res.setHeader('Set-Cookie', cookie.serialize(key, value, options)) | |
} | |
// $ curl http://localhost:5000/addCookie -i | |
// HTTP/1.1 200 OK | |
// x-powered-by: Express | |
// cache-control: private | |
// pragma: no-cache | |
// expires: 0 | |
// set-cookie: a=b; Max-Age=86400; HttpOnly | |
// content-type: text/html; charset=utf-8 | |
// content-length: 20 | |
// etag: W/"14-z3iZXchEt5DVWZKsMncy8Wl4KSQ" | |
// date: Sun, 10 Mar 2019 00:46:45 GMT | |
// connection: close | |
// vary: Accept-Encoding, Authorization, Cookie | |
// Hello from Firebase! | |
// $ curl http://localhost:5000/getCookie -H 'Cookie: state=6; bar=28; baz=496' | |
// 6 | |
// $ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment