From 84ec601e2a1d4f3eef561d7b7cd8b0a4994922dd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?=
Date: Tue, 7 May 2019 22:15:18 +0200
Subject: [PATCH] logout
---
backend/routes/user/logout.coffee | 25 +++++++++++++++++++++++++
backend/server.coffee | 2 ++
backend/session.coffee | 10 ++++++++++
frontend/src/views/About.vue | 16 ++++++++++++++++
4 files changed, 53 insertions(+)
create mode 100644 backend/routes/user/logout.coffee
diff --git a/backend/routes/user/logout.coffee b/backend/routes/user/logout.coffee
new file mode 100644
index 0000000..a8e6a07
--- /dev/null
+++ b/backend/routes/user/logout.coffee
@@ -0,0 +1,25 @@
+logger = (require 'logging').default 'logout'
+
+# session storage (volatile data)
+session = (require '../../session')
+# fftcg.db (persistent data)
+fftcgdb = (require '../../db')
+
+module.exports =
+ url: '/user/logout'
+ method: 'POST'
+
+ handler: (request, reply) ->
+ new Promise (resolve) ->
+ session.destroy request.body.session ? ""
+ .then ->
+ # active session found
+ logger.debug "OK removed session '#{request.body.session}'"
+ resolve null
+
+ .catch ->
+ resolve null
+
+ .then ->
+ reply.send
+ success: true
diff --git a/backend/server.coffee b/backend/server.coffee
index 709616a..eef08d7 100644
--- a/backend/server.coffee
+++ b/backend/server.coffee
@@ -14,6 +14,8 @@ fastify.route (require "./routes/#{route}") for route in [
'test'
# log in user
'user/login'
+ # log out user
+ 'user/logout'
# register user
'user/register'
]
diff --git a/backend/session.coffee b/backend/session.coffee
index 6861435..56999cf 100644
--- a/backend/session.coffee
+++ b/backend/session.coffee
@@ -33,6 +33,16 @@ class FFTCGSESSION
logger.info "OK '#{digest}' created"
resolve digest
+ destroy: (digest) ->
+ new Promise (resolve, reject) =>
+ # delete hash immediately
+ @db.del digest, (err, res) ->
+ if res == 0
+ reject null
+ else
+ logger.info "OK '#{digest}' deleted"
+ resolve null
+
check: (digest) ->
new Promise (resolve, reject) =>
# refresh expiry timer on digest
diff --git a/frontend/src/views/About.vue b/frontend/src/views/About.vue
index 3b3c1b9..9b52346 100644
--- a/frontend/src/views/About.vue
+++ b/frontend/src/views/About.vue
@@ -7,6 +7,7 @@
user session: {{ sessionID }}
+ Logout
@@ -21,6 +22,21 @@ export default {
sessionID: ''
}),
+ methods: {
+ logout() {
+ axios
+ .post('/user/logout', {
+ session: Cookies.get('session')
+ })
+ .then(response => {
+ if (response.data.success) {
+ Cookies.remove('session')
+ this.$router.push({name: 'home'})
+ }
+ })
+ }
+ },
+
mounted() {
axios
.post('/user/login', {