diff --git a/sessions/app.yaml b/sessions/app.yaml new file mode 100644 index 000000000..6e433043f --- /dev/null +++ b/sessions/app.yaml @@ -0,0 +1,17 @@ +# Copyright 2019, Google LLC. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +[START getting_started_sessions_runtime] +runtime: nodejs10 +[END getting_started_sessions_runtime] + diff --git a/sessions/index.js b/sessions/index.js new file mode 100644 index 000000000..ed9a6691e --- /dev/null +++ b/sessions/index.js @@ -0,0 +1,49 @@ +// Copyright 2019, Google LLC. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +const {Firestore} = require('@google-cloud/firestore'); +const express = require('express'); +const session = require('express-session'); + +const app = express(); +const {FirestoreStore} = require('@google-cloud/connect-firestore'); + +app.use( + session({ + store: new FirestoreStore({ + dataset: new Firestore({ + kind: 'express-sessions', + }), + }), + secret: 'my-secret', + resave: false, + saveUninitialized: true, + }) +); + +const colors = ['red', 'blue', 'green', 'yellow', 'pink']; + +app.get('/', (req, res) => { + console.log(req.session.id); + if (!req.session.views) { + req.session.views = 0; + req.session.color = colors[Math.floor(Math.random() * 5)]; + } + const views = req.session.views++; + res.send(`Views ${views}`); +}); + +const port = process.env.PORT || 8080; +app.listen(port, () => { + console.log(`Example app listening on port ${port}!`); +}); diff --git a/sessions/package.json b/sessions/package.json new file mode 100644 index 000000000..05b0b5896 --- /dev/null +++ b/sessions/package.json @@ -0,0 +1,17 @@ +{ + "name": "session-handling", + "private": true, + "description": "Session Handling via Firestore Tutorial", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "Apache-2.0", + "dependencies": { + "@google-cloud/connect-firestore": "^1.0.3", + "@google-cloud/firestore": "^1.3.0", + "express": "^4.17.1", + "express-session": "^1.16.2" + } +} \ No newline at end of file