From 2b2b23c1e368fbe9fd2724eb2247884ef6e8353a Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sun, 28 Jan 2024 01:07:38 +0800 Subject: [PATCH] :sparkles: Logout --- view/package.json | 1 + view/src/index.tsx | 2 + view/src/layouts/RootLayout.tsx | 6 +- view/src/layouts/shared/Navbar.tsx | 24 ++- view/src/pages/auth/login.tsx | 4 +- view/src/pages/auth/register.tsx | 3 + view/src/pages/dashboard.tsx | 10 +- view/src/stores/{userinfo.ts => userinfo.tsx} | 42 ++++- view/vite.config.ts | 9 +- view/yarn.lock | 169 +++++++++++++++++- 10 files changed, 244 insertions(+), 26 deletions(-) create mode 100644 view/src/pages/auth/register.tsx rename view/src/stores/{userinfo.ts => userinfo.tsx} (64%) diff --git a/view/package.json b/view/package.json index 8838834..b97e52e 100644 --- a/view/package.json +++ b/view/package.json @@ -17,6 +17,7 @@ "autoprefixer": "^10.4.17", "daisyui": "^4.6.0", "postcss": "^8.4.33", + "solid-devtools": "^0.29.3", "tailwindcss": "^3.4.1", "typescript": "^5.2.2", "vite": "^5.0.8", diff --git a/view/src/index.tsx b/view/src/index.tsx index 3b87109..e816ce4 100644 --- a/view/src/index.tsx +++ b/view/src/index.tsx @@ -1,3 +1,5 @@ +import "solid-devtools"; + /* @refresh reload */ import { render } from "solid-js/web"; diff --git a/view/src/layouts/RootLayout.tsx b/view/src/layouts/RootLayout.tsx index d78be5c..dfe956c 100644 --- a/view/src/layouts/RootLayout.tsx +++ b/view/src/layouts/RootLayout.tsx @@ -1,5 +1,5 @@ import Navbar from "./shared/Navbar.tsx"; -import { readProfiles } from "../stores/userinfo.ts"; +import { readProfiles, UserinfoProvider } from "../stores/userinfo.tsx"; import { createSignal, Show } from "solid-js"; export default function RootLayout(props: any) { @@ -15,11 +15,11 @@ export default function RootLayout(props: any) { }> -
+
{props.children}
-
+ ); } \ No newline at end of file diff --git a/view/src/layouts/shared/Navbar.tsx b/view/src/layouts/shared/Navbar.tsx index 3edf5f9..36906e7 100644 --- a/view/src/layouts/shared/Navbar.tsx +++ b/view/src/layouts/shared/Navbar.tsx @@ -1,5 +1,6 @@ -import { For, Show } from "solid-js"; -import { userinfo } from "../../stores/userinfo.ts"; +import { For, Match, Switch } from "solid-js"; +import { clearUserinfo, useUserinfo } from "../../stores/userinfo.tsx"; +import { useNavigate } from "@solidjs/router"; interface MenuItem { label: string; @@ -9,6 +10,14 @@ interface MenuItem { export default function Navbar() { const nav: MenuItem[] = [{ label: "Dashboard", href: "/" }]; + const userinfo = useUserinfo(); + const navigate = useNavigate(); + + function logout() { + clearUserinfo(); + navigate("/auth/login"); + } + return ( ); diff --git a/view/src/pages/auth/login.tsx b/view/src/pages/auth/login.tsx index 8b4db99..f43c0c0 100644 --- a/view/src/pages/auth/login.tsx +++ b/view/src/pages/auth/login.tsx @@ -1,9 +1,9 @@ -import { readProfiles } from "../../stores/userinfo.ts"; +import { readProfiles } from "../../stores/userinfo.tsx"; import { useNavigate } from "@solidjs/router"; import { createSignal, For, Match, Show, Switch } from "solid-js"; import Cookie from "universal-cookie"; -export default function Login() { +export default function LoginPage() { const [title, setTitle] = createSignal("Sign in"); const [subtitle, setSubtitle] = createSignal("Via your Goatpass account"); diff --git a/view/src/pages/auth/register.tsx b/view/src/pages/auth/register.tsx new file mode 100644 index 0000000..5ccb62d --- /dev/null +++ b/view/src/pages/auth/register.tsx @@ -0,0 +1,3 @@ +export default function RegisterPage() { + +} \ No newline at end of file diff --git a/view/src/pages/dashboard.tsx b/view/src/pages/dashboard.tsx index 387284b..f33739a 100644 --- a/view/src/pages/dashboard.tsx +++ b/view/src/pages/dashboard.tsx @@ -1,10 +1,12 @@ -import { userinfo } from "../stores/userinfo.ts"; +import { useUserinfo } from "../stores/userinfo.tsx"; + +export default function DashboardPage() { + const userinfo = useUserinfo(); -export default function Dashboard() { return (
-

Welcome, {userinfo.displayName}

+

Welcome, {userinfo?.displayName}

What's a nice day!

- ) + ); } \ No newline at end of file diff --git a/view/src/stores/userinfo.ts b/view/src/stores/userinfo.tsx similarity index 64% rename from view/src/stores/userinfo.ts rename to view/src/stores/userinfo.tsx index f5ba90f..523e7d2 100644 --- a/view/src/stores/userinfo.ts +++ b/view/src/stores/userinfo.tsx @@ -1,16 +1,24 @@ -import { createStore } from "solid-js/store"; import Cookie from "universal-cookie"; +import { createContext, useContext } from "solid-js"; +import { createStore } from "solid-js/store"; -const [userinfo, setUserinfo] = createStore({ +export interface Userinfo { + isLoggedIn: boolean, + displayName: string, + profiles: any, + meta: any +} + +const UserinfoContext = createContext(); + +const defaultUserinfo: Userinfo = { isLoggedIn: false, displayName: "Citizen", profiles: null, meta: null -}); +}; -function checkLoggedIn(): boolean { - return new Cookie().get("access_token"); -} +const [userinfo, setUserinfo] = createStore(structuredClone(defaultUserinfo)); export function getAtk(): string { return new Cookie().get("access_token"); @@ -36,6 +44,10 @@ export async function refreshAtk() { } } +function checkLoggedIn(): boolean { + return new Cookie().get("access_token"); +} + export async function readProfiles() { if (!checkLoggedIn()) return; @@ -59,4 +71,20 @@ export async function readProfiles() { }); } -export { userinfo }; \ No newline at end of file +export function clearUserinfo() { + new Cookie().remove("access_token", { path: "/" }); + new Cookie().remove("refresh_token", { path: "/" }); + setUserinfo(defaultUserinfo); +} + +export function UserinfoProvider(props: any) { + return ( + + {props.children} + + ); +} + +export function useUserinfo() { + return useContext(UserinfoContext); +} \ No newline at end of file diff --git a/view/vite.config.ts b/view/vite.config.ts index 410d3f6..80db646 100644 --- a/view/vite.config.ts +++ b/view/vite.config.ts @@ -1,11 +1,12 @@ -import { defineConfig } from 'vite' -import solid from 'vite-plugin-solid' +import { defineConfig } from "vite"; +import solid from "vite-plugin-solid"; +import devtools from "solid-devtools/vite"; export default defineConfig({ - plugins: [solid()], + plugins: [devtools({ autoname: true }), solid()], server: { proxy: { "/api": "http://localhost:8444" } } -}) +}); diff --git a/view/yarn.lock b/view/yarn.lock index 08982d3..6e9fd7c 100644 --- a/view/yarn.lock +++ b/view/yarn.lock @@ -179,6 +179,13 @@ dependencies: "@babel/helper-plugin-utils" "^7.22.5" +"@babel/plugin-syntax-typescript@^7.23.3": + version "7.23.3" + resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-typescript/-/plugin-syntax-typescript-7.23.3.tgz#24f460c85dbbc983cd2b9c4994178bcc01df958f" + integrity sha512-9EiNjVJOMwCO+43TqoTrgQ8jMwcAd0sWyXi9RPfIsLTj4R2MADDDQXELhffaUx/uJv2AYcxBgPwH6j4TIA4ytQ== + dependencies: + "@babel/helper-plugin-utils" "^7.22.5" + "@babel/template@^7.22.15", "@babel/template@^7.23.9": version "7.23.9" resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.23.9.tgz#f881d0487cba2828d3259dcb9ef5005a9731011a" @@ -204,7 +211,7 @@ debug "^4.3.1" globals "^11.1.0" -"@babel/types@^7.0.0", "@babel/types@^7.18.6", "@babel/types@^7.20.7", "@babel/types@^7.22.15", "@babel/types@^7.22.5", "@babel/types@^7.23.0", "@babel/types@^7.23.6", "@babel/types@^7.23.9": +"@babel/types@^7.0.0", "@babel/types@^7.18.6", "@babel/types@^7.20.7", "@babel/types@^7.22.15", "@babel/types@^7.22.5", "@babel/types@^7.23.0", "@babel/types@^7.23.3", "@babel/types@^7.23.6", "@babel/types@^7.23.9": version "7.23.9" resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.23.9.tgz#1dd7b59a9a2b5c87f8b41e52770b5ecbf492e002" integrity sha512-dQjSq/7HaSjRM43FFGnv5keM2HsxpmyV1PfaSVm0nzzjwwTmjOe6J4bC8e3+pTEIgHaHj+1ZlLThRJ2auc/w1Q== @@ -393,6 +400,11 @@ "@nodelib/fs.scandir" "2.1.5" fastq "^1.6.0" +"@nothing-but/utils@~0.12.0": + version "0.12.1" + resolved "https://registry.yarnpkg.com/@nothing-but/utils/-/utils-0.12.1.tgz#e95549e78738d328fed740e1055e696bd5ce9126" + integrity sha512-1qZU1Q5El0IjE7JT/ucvJNzdr2hL3W8Rm27xNf1p6gb3Nw8pGnZmxp6/GEW9h+I1k1cICxXNq25hBwknTQ7yhg== + "@pkgjs/parseargs@^0.11.0": version "0.11.0" resolved "https://registry.yarnpkg.com/@pkgjs/parseargs/-/parseargs-0.11.0.tgz#a77ea742fab25775145434eb1d2328cf5013ac33" @@ -463,6 +475,150 @@ resolved "https://registry.yarnpkg.com/@rollup/rollup-win32-x64-msvc/-/rollup-win32-x64-msvc-4.9.6.tgz#2c1fb69e02a3f1506f52698cfdc3a8b6386df9a6" integrity sha512-jqzNLhNDvIZOrt69Ce4UjGRpXJBzhUBzawMwnaDAwyHriki3XollsewxWzOzz+4yOFDkuJHtTsZFwMxhYJWmLQ== +"@solid-devtools/debugger@^0.23.3": + version "0.23.3" + resolved "https://registry.yarnpkg.com/@solid-devtools/debugger/-/debugger-0.23.3.tgz#f56fa5952460db45a146673eda764b349474381d" + integrity sha512-VrgswTjb2FyHxQJp5y5u7OaJ2k1R14LYlAOX/1rDZrGHWKdGYCaWHGzxI7C8AExtMP+LS+WOxy0uXMPQpoAD2g== + dependencies: + "@nothing-but/utils" "~0.12.0" + "@solid-devtools/shared" "^0.13.1" + "@solid-primitives/bounds" "^0.0.118" + "@solid-primitives/cursor" "^0.0.112" + "@solid-primitives/event-bus" "^1.0.8" + "@solid-primitives/event-listener" "^2.3.0" + "@solid-primitives/keyboard" "^1.2.5" + "@solid-primitives/platform" "^0.1.0" + "@solid-primitives/rootless" "^1.4.2" + "@solid-primitives/scheduled" "^1.4.1" + "@solid-primitives/static-store" "^0.0.5" + "@solid-primitives/utils" "^6.2.1" + +"@solid-devtools/shared@^0.13.1": + version "0.13.1" + resolved "https://registry.yarnpkg.com/@solid-devtools/shared/-/shared-0.13.1.tgz#f672ec4c96d77b6f91e9b0e598dd3ae3abf4a6fb" + integrity sha512-qaAcZF47FFr4alVQSy5ooLy7mMt4MMDxSHw52heY1oCut8yfXDrnLcYDONabfoin2WYIwsQpjYhryHgjtB0uDg== + dependencies: + "@solid-primitives/event-bus" "^1.0.8" + "@solid-primitives/event-listener" "^2.3.0" + "@solid-primitives/media" "^2.2.5" + "@solid-primitives/refs" "^1.0.5" + "@solid-primitives/rootless" "^1.4.2" + "@solid-primitives/scheduled" "^1.4.1" + "@solid-primitives/static-store" "^0.0.5" + "@solid-primitives/styles" "^0.0.111" + "@solid-primitives/utils" "^6.2.1" + +"@solid-primitives/bounds@^0.0.118": + version "0.0.118" + resolved "https://registry.yarnpkg.com/@solid-primitives/bounds/-/bounds-0.0.118.tgz#098229e9996288e0220ec4639731f8977a90c706" + integrity sha512-Qj42w8LlnhJ3r/t+t0c0vrdwIvvQMPgjEFGmLiwREaA85ojLbgL9lSBq2tKvljeLCvRVkgj10KEUf+vc99VCIg== + dependencies: + "@solid-primitives/event-listener" "^2.3.0" + "@solid-primitives/resize-observer" "^2.0.22" + "@solid-primitives/static-store" "^0.0.5" + "@solid-primitives/utils" "^6.2.1" + +"@solid-primitives/cursor@^0.0.112": + version "0.0.112" + resolved "https://registry.yarnpkg.com/@solid-primitives/cursor/-/cursor-0.0.112.tgz#00f5bf5492bbfb0c927407f07bef38c5d53bc6fd" + integrity sha512-TAtU7qD7ipSLSXHnq8FhhosAPVX+dnOCb/ITcGcLlj8e/C9YKcxDhgBHJ3R/d1xDRb5/vO/szJtEz6fnQD311Q== + dependencies: + "@solid-primitives/utils" "^6.2.1" + +"@solid-primitives/event-bus@^1.0.8": + version "1.0.9" + resolved "https://registry.yarnpkg.com/@solid-primitives/event-bus/-/event-bus-1.0.9.tgz#8c618145dea3f7f187938c259c5221e4a2fd6fa3" + integrity sha512-BI9dla3GQzINsufEzr/CV3B/9e0D1pyk7Ig6kPI6s4geUkiVgFadY5V8R95PEeHH1NagOny7AHpw+RaRIuBpkA== + dependencies: + "@solid-primitives/utils" "^6.2.2" + +"@solid-primitives/event-listener@^2.3.0", "@solid-primitives/event-listener@^2.3.1": + version "2.3.1" + resolved "https://registry.yarnpkg.com/@solid-primitives/event-listener/-/event-listener-2.3.1.tgz#a303bc1f090a6c145cf8d71556ec2eac9d37cb7b" + integrity sha512-S1AfFYatOJ3g/ZUbGDoKplSGLTTfarQ3Mfd3F/fXb9SnzGtROtd+Y6yLkPVzK4AVw83r2wUSaS0GS6dg8izTEQ== + dependencies: + "@solid-primitives/utils" "^6.2.2" + +"@solid-primitives/keyboard@^1.2.5": + version "1.2.6" + resolved "https://registry.yarnpkg.com/@solid-primitives/keyboard/-/keyboard-1.2.6.tgz#b23692239253478c406d6a7c61d0ff84a52a73d7" + integrity sha512-ZbXkzAwFs+6hyaZ2hT9uQ38iPZzGTjV6kWvVvJ3BDfKASg0dDDuhEZRKmpkmpLfnqOabV58BUkASJWMAA4dZCg== + dependencies: + "@solid-primitives/event-listener" "^2.3.1" + "@solid-primitives/rootless" "^1.4.3" + "@solid-primitives/utils" "^6.2.2" + +"@solid-primitives/media@^2.2.5": + version "2.2.6" + resolved "https://registry.yarnpkg.com/@solid-primitives/media/-/media-2.2.6.tgz#a36b39242518a4bdfcc69f44aee10cd118cbbc0a" + integrity sha512-VopOSqoUZgmSFY4SNnwBzHYaoGG+7gQYcwX+RJ/qQtuZJgzOiC+PejZEwNJh+aBZ383HPwrypyd3zrYVm7EnpQ== + dependencies: + "@solid-primitives/event-listener" "^2.3.1" + "@solid-primitives/rootless" "^1.4.3" + "@solid-primitives/static-store" "^0.0.6" + "@solid-primitives/utils" "^6.2.2" + +"@solid-primitives/platform@^0.1.0": + version "0.1.1" + resolved "https://registry.yarnpkg.com/@solid-primitives/platform/-/platform-0.1.1.tgz#a90a84b2325d1b42b9d87b4ce5be60abbbfbd6c5" + integrity sha512-Ln7dzHFjNDpjmhnKiMAUWBjObRZ01FQtj4ABkSmE51VjLI0i9JW54lbZhJImYjpCyoBjtxBEGWsCgYi9JyRXrA== + +"@solid-primitives/refs@^1.0.5": + version "1.0.6" + resolved "https://registry.yarnpkg.com/@solid-primitives/refs/-/refs-1.0.6.tgz#636b4a792243e00e3899ed948be00857d1b16666" + integrity sha512-ruh4YdVMxThEVnvqbpeLXKojW442vpFU8q7dSKtElGOTa31aKOAkRb9BTbdaTwVjN4BEq79fiiYIXozJNl4dSw== + dependencies: + "@solid-primitives/utils" "^6.2.2" + +"@solid-primitives/resize-observer@^2.0.22": + version "2.0.23" + resolved "https://registry.yarnpkg.com/@solid-primitives/resize-observer/-/resize-observer-2.0.23.tgz#5ba0cdc415ea03e86fb31ffafade68d33a0536c3" + integrity sha512-SgKRzRfy1oFbPSapmtF5H9VQcN66foQaQK3QTyzh4cihIG2k/UiVNjbC+el1nEDnSeAP7zTO4Xsf2UW8dc+aBw== + dependencies: + "@solid-primitives/event-listener" "^2.3.1" + "@solid-primitives/rootless" "^1.4.3" + "@solid-primitives/static-store" "^0.0.6" + "@solid-primitives/utils" "^6.2.2" + +"@solid-primitives/rootless@^1.4.2", "@solid-primitives/rootless@^1.4.3": + version "1.4.3" + resolved "https://registry.yarnpkg.com/@solid-primitives/rootless/-/rootless-1.4.3.tgz#9ac2fb5a96135a760031376428a0d195a1f97919" + integrity sha512-IPsfUhKsqQOxLtRMQWK2EZAYbL9RKJMLBelLwpaXl9+oa1tl5aNvA6GHgrNrK+85oUhiYh7/OuogO18AuHepqQ== + dependencies: + "@solid-primitives/utils" "^6.2.2" + +"@solid-primitives/scheduled@^1.4.1": + version "1.4.2" + resolved "https://registry.yarnpkg.com/@solid-primitives/scheduled/-/scheduled-1.4.2.tgz#9ec2bec6d0961297a22da0b317e8623520ece6f9" + integrity sha512-duKaugDQtPk0v6MnkBuEalWk66/vA2G7zzoimQEvmUdh2+K2o8t908HIfI2NdBfwakQMQBV4epE3TFeN2Vsveg== + +"@solid-primitives/static-store@^0.0.5": + version "0.0.5" + resolved "https://registry.yarnpkg.com/@solid-primitives/static-store/-/static-store-0.0.5.tgz#ed3e5be8e72121f92fac54e7cfd4ed81aa2e29cd" + integrity sha512-ssQ+s/wrlFAEE4Zw8GV499yBfvWx7SMm+ZVc11wvao4T5xg9VfXCL9Oa+x4h+vPMvSV/Knv5LrsLiUa+wlJUXQ== + dependencies: + "@solid-primitives/utils" "^6.2.1" + +"@solid-primitives/static-store@^0.0.6": + version "0.0.6" + resolved "https://registry.yarnpkg.com/@solid-primitives/static-store/-/static-store-0.0.6.tgz#54249f5db9befef4818b22f8782cfcd8bb95088f" + integrity sha512-PtvkbbucbjT+9p95pksOciG9gOnCtJz4IUyAKX1Ld7YwI+QgtPTo0Wuxs8gNbNtLtoDv5PNv5t4YRzUyl0fwdg== + dependencies: + "@solid-primitives/utils" "^6.2.2" + +"@solid-primitives/styles@^0.0.111": + version "0.0.111" + resolved "https://registry.yarnpkg.com/@solid-primitives/styles/-/styles-0.0.111.tgz#b11372dc07f64b87042c20c0be742e2c02ca2869" + integrity sha512-1mBxOGAPXmfD5oYCvqjKBDN7SuNjz2qz7RdH7KtsuNLQh6lpuSKadtHnLvru0Y8Vz1InqTJisBIy/6P5kyDmPw== + dependencies: + "@solid-primitives/rootless" "^1.4.2" + "@solid-primitives/utils" "^6.2.1" + +"@solid-primitives/utils@^6.2.1", "@solid-primitives/utils@^6.2.2": + version "6.2.2" + resolved "https://registry.yarnpkg.com/@solid-primitives/utils/-/utils-6.2.2.tgz#01c28bab7b2842b46bd5559716497decc51ad3cd" + integrity sha512-11ypVbp987XxETeRqY5Y3OmmTpm8/jZqJXRvo6AyqBthzkvvjEdReuUMU2yVb+pwWGxfZpWHZ6EUCcGXUMhfwg== + "@solidjs/router@^0.10.10": version "0.10.10" resolved "https://registry.yarnpkg.com/@solidjs/router/-/router-0.10.10.tgz#cd236ef438e9aa50864e7361db710af26b152ad5" @@ -1281,6 +1437,17 @@ signal-exit@^4.0.1: resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-4.1.0.tgz#952188c1cbd546070e2dd20d0f41c0ae0530cb04" integrity sha512-bzyZ1e88w9O1iNJbKnOlvYTrWPDl46O1bG0D3XInv+9tkPrxrN8jUUTiFlDkkmKWgn1M6CfIA13SuGqOa9Korw== +solid-devtools@^0.29.3: + version "0.29.3" + resolved "https://registry.yarnpkg.com/solid-devtools/-/solid-devtools-0.29.3.tgz#62c563ab5c340d02168b63017c204f5632aad537" + integrity sha512-9j3VxVbEoC54ML22gAMytR8ZS1nk9xKatsWziKSkI4c/Bcyh4sxQBGESHuXSLs9xaxpyGVTmFl3hknoxEpKzlA== + dependencies: + "@babel/core" "^7.23.3" + "@babel/plugin-syntax-typescript" "^7.23.3" + "@babel/types" "^7.23.3" + "@solid-devtools/debugger" "^0.23.3" + "@solid-devtools/shared" "^0.13.1" + solid-js@^1.8.7: version "1.8.12" resolved "https://registry.yarnpkg.com/solid-js/-/solid-js-1.8.12.tgz#12903f076cdbd74e13dd6ab47b1006c3a07f5591"