From 7d346ba2cef80c80705300d838f9a773dcc62b6a Mon Sep 17 00:00:00 2001 From: Andre Henriques Date: Thu, 2 May 2024 16:58:01 +0100 Subject: [PATCH] closes #101 --- logic/users/users.go | 12 ++++++++++++ logic/utils/handler.go | 2 +- webpage/src/routes/UserStore.svelte.ts | 5 +++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/logic/users/users.go b/logic/users/users.go index 5c2c9d3..9b542a6 100644 --- a/logic/users/users.go +++ b/logic/users/users.go @@ -412,6 +412,18 @@ func UsersEndpints(db db.Db, handle *Handle) { return c.SendJSON("Ok") }) + handle.DeleteAuth("/user/token/logoff", User_Normal, func(c *Context) *Error { + if c.Token == nil { + return c.JsonBadRequest("Failed to get token") + } + _, err := c.Db.Exec("delete from tokens where token=$1;", c.Token) + if err != nil { + return c.E500M("Failed to delete token", err) + } + + return c.SendJSON("OK") + }) + type DeleteUser struct { Id string `json:"id" validate:"required"` Password string `json:"password" validate:"required"` diff --git a/logic/utils/handler.go b/logic/utils/handler.go index ebf79c9..4b35f81 100644 --- a/logic/utils/handler.go +++ b/logic/utils/handler.go @@ -175,7 +175,7 @@ func (x *Handle) DeleteAuth(path string, authLevel dbtypes.UserType, fn func(c * } return fn(c) } - x.posts = append(x.posts, HandleFunc{path, inner_fn}) + x.deletes = append(x.deletes, HandleFunc{path, inner_fn}) } func DeleteAuthJson[T interface{}](x *Handle, path string, authLevel dbtypes.UserType, fn func(c *Context, obj *T) *Error) { diff --git a/webpage/src/routes/UserStore.svelte.ts b/webpage/src/routes/UserStore.svelte.ts index 5f082f2..50e55b5 100644 --- a/webpage/src/routes/UserStore.svelte.ts +++ b/webpage/src/routes/UserStore.svelte.ts @@ -1,4 +1,5 @@ import { goto } from '$app/navigation'; +import { rdelete } from '$lib/requests.svelte'; type User = { token: string; @@ -33,6 +34,10 @@ export function createUserStore() { if (value) { localStorage.setItem('user', JSON.stringify(value)); } else { + if (user) { + // Request the deletion of the token + rdelete('/user/token/logoff', {}); + } localStorage.removeItem('user'); } user = value;