From 0475f9cdd18b814191dfae0abfb58e285805283a Mon Sep 17 00:00:00 2001 From: Project_IO Date: Thu, 20 Mar 2025 12:40:32 +0900 Subject: [PATCH] feat: get account read --- internal/middleware/auth.go | 4 +- internal/routes/auth.go | 43 ++++++-- src/components/settings/index.tsx | 143 +++++++++++++++++--------- src/components/settings/settings.scss | 58 +++++++++-- 4 files changed, 185 insertions(+), 63 deletions(-) diff --git a/internal/middleware/auth.go b/internal/middleware/auth.go index e9fcbbd..05b092c 100644 --- a/internal/middleware/auth.go +++ b/internal/middleware/auth.go @@ -29,7 +29,7 @@ func BasicAuth(ctx *gin.Context) { auth := service.NewAuthService() username, password, ok := ctx.Request.BasicAuth() if !ok { - ctx.Status(403) + ctx.Status(401) return } @@ -41,7 +41,7 @@ func BasicAuth(ctx *gin.Context) { } if !ok { - ctx.Status(403) + ctx.Status(401) return } diff --git a/internal/routes/auth.go b/internal/routes/auth.go index a0fe692..03f11c1 100644 --- a/internal/routes/auth.go +++ b/internal/routes/auth.go @@ -38,19 +38,49 @@ func authentication(group *gin.RouterGroup) { }) }) + group.GET("/read", func(ctx *gin.Context) { + auth := service.NewAuthService() + username, password, ok := ctx.Request.BasicAuth() + if !ok { + ctx.Status(401) + return + } + + ok, err := auth.VerifyToken(username, password) + if err != nil { + ctx.JSON(500, gin.H{ + "ok": 0, + "errno": "internal server error!", + }) + } + + if !ok { + ctx.JSON(401, gin.H{ + "ok": 0, + "errno": "unauthorized", + }) + return + } + + ctx.JSON(200, gin.H{ + "ok": 1, + "username": username, + }) + }) + group.PATCH("/update", func(ctx *gin.Context) { auth := service.NewAuthService() old := ctx.PostForm("password") new := ctx.PostForm("new_password") username, _, ok := ctx.Request.BasicAuth() if !ok { - ctx.Status(403) + ctx.Status(401) return } ok, err := auth.Verify(username, old) if err != nil || !ok { - ctx.Status(403) + ctx.Status(401) return } @@ -65,14 +95,13 @@ func authentication(group *gin.RouterGroup) { group.DELETE("/delete", func(ctx *gin.Context) { auth := service.NewAuthService() - pass := ctx.PostForm("password") - username, _, ok := ctx.Request.BasicAuth() + username, password, ok := ctx.Request.BasicAuth() if !ok { - ctx.Status(403) + ctx.Status(401) return } - ok, err := auth.Verify(username, pass) + ok, err := auth.VerifyToken(username, password) if err != nil { ctx.Status(500) _, _ = fmt.Fprintln(os.Stderr, err) @@ -80,7 +109,7 @@ func authentication(group *gin.RouterGroup) { } if !ok { - ctx.Status(403) + ctx.Status(401) return } diff --git a/src/components/settings/index.tsx b/src/components/settings/index.tsx index f2da56a..50173eb 100644 --- a/src/components/settings/index.tsx +++ b/src/components/settings/index.tsx @@ -2,6 +2,7 @@ import React, { useEffect, useRef, useState } from "react"; import { AuthState, useAuthStore } from "../../store/auth"; import "./settings.scss"; +import { DynamicIcon } from "lucide-react/dynamic"; function Settings() { const auth = useAuthStore(); @@ -31,9 +32,9 @@ function Settings() { return (
-

General

+

General

- +
); } @@ -46,64 +47,114 @@ function SettingBox({ children }: { children: React.ReactNode }) { ); } -function ChangePassword({ auth }: { auth: AuthState }) { +function AccountSetting({ auth }: { auth: AuthState }) { const orRef = useRef(null); const pwRef = useRef(null); const ckRef = useRef(null); return ( -

Change Password

- If you change your password, you will need to log in again. +

Account Setting

+ You can modify your account. This is a sensitive option. Please reconsider if you want to change your account information.
-
- - - +
+
+
Change Password
+ If you change your password, you will need to log in again. +
- - + + if (origin === "" || password === "" || check === "") { + alert("You must need to write all inputs"); + return; + } + + if (password !== check) { + alert("New password is not matches!"); + return; + } + + const form = new URLSearchParams(); + form.append("password", origin); + form.append("new_password", password); + + fetch("/api/auth/update", { + body: form, + method: "PATCH", + headers: { + "Authorization": `Basic ${auth.token}` + } + }).then((res) => { + if (res.status !== 200) { + alert(`${res.status} ${res.statusText}`); + return; + } + + alert("password changed!"); + document.location.href = "/logout"; + }); + }}>Change Password + +
+
+
+
Delete Account
+ You can delete account. This action is irreversible. Please proceed with caution. +
+ +
+ +
+
); } +function PasswordInput({ placeholder, ref }: { placeholder: string; ref: React.RefObject }) { + const [show, setShow] = useState(false); + + return ( + + ); +} + export default Settings; diff --git a/src/components/settings/settings.scss b/src/components/settings/settings.scss index 083548d..6c5411a 100644 --- a/src/components/settings/settings.scss +++ b/src/components/settings/settings.scss @@ -3,7 +3,13 @@ height: 100%; display: flex; margin: 1rem 0; + align-items: center; flex-direction: column; + justify-content: center; + + .ka-title { + width: 100%; + } .setting-box { width: 100%; @@ -18,24 +24,60 @@ .box-row { width: 100%; display: flex; - margin: 0 2rem; + margin: 1rem 0; flex-direction: row; + justify-content: space-between; + + @media (max-width: 640px) { + margin: 0; + flex-direction: column; + } } .box-col { + height: 100%; display: flex; - min-width: 300px; + margin: 1rem 0; + min-width: 350px; flex-direction: column; + + @media (max-width: 640px) { + min-width: 100%; + } } .line { margin-bottom: 15px; } - - #pw-change { - input { - margin-bottom: 10px; - } - } + } +} + +#pw-change { + .input-pass { + height: 40; + display: flex; + align-items: center; + margin-bottom: 10px; + flex-direction: row; + justify-content: center; + } + + input { + width: 100%; + border-radius: 25px 0 0 25px; + } + + .pw-btn { + width: 40px; + height: 40px; + display: flex; + align-items: center; + justify-content: center; + border-radius: 0 25px 25px 0; + background-color: var(--nav-color); + } + + button { + margin-top: 1rem; } }