diff --git a/VoidCat/Controllers/UserController.cs b/VoidCat/Controllers/UserController.cs index 1153e61..fe03a6a 100644 --- a/VoidCat/Controllers/UserController.cs +++ b/VoidCat/Controllers/UserController.cs @@ -19,31 +19,32 @@ public class UserController : Controller } [HttpGet] - [Route("")] public async Task GetUser([FromRoute] string id) { var loggedUser = HttpContext.GetUserId(); - var requestedId = id.FromBase58Guid(); + var isMe = id.Equals("me", StringComparison.InvariantCultureIgnoreCase); + if (isMe && !loggedUser.HasValue) return Unauthorized(); + + var requestedId = isMe ? loggedUser!.Value : id.FromBase58Guid(); if (loggedUser == requestedId) { - return Json(await _store.Get(id.FromBase58Guid())); + return Json(await _store.Get(requestedId)); } - var user = await _store.Get(id.FromBase58Guid()); + var user = await _store.Get(requestedId); if (!(user?.Flags.HasFlag(VoidUserFlags.PublicProfile) ?? false)) return NotFound(); return Json(user); } [HttpPost] - [Route("")] public async Task UpdateUser([FromRoute] string id, [FromBody] PublicVoidUser user) { var loggedUser = await GetAuthorizedUser(id); if (loggedUser == default) return Unauthorized(); if (!loggedUser.Flags.HasFlag(VoidUserFlags.EmailVerified)) return Forbid(); - + await _store.UpdateProfile(user); return Ok(); } diff --git a/VoidCat/spa/src/Header.js b/VoidCat/spa/src/Header.js index fdbfe53..43d0e04 100644 --- a/VoidCat/spa/src/Header.js +++ b/VoidCat/spa/src/Header.js @@ -1,10 +1,31 @@ import "./Header.css"; import {Link} from "react-router-dom"; -import {useSelector} from "react-redux"; +import {useDispatch, useSelector} from "react-redux"; import {InlineProfile} from "./InlineProfile"; +import {useApi} from "./Api"; +import {logout, setProfile} from "./LoginState"; +import {useEffect} from "react"; export function Header() { - const profile = useSelector(state => state.login.profile); + const dispatch = useDispatch(); + const jwt = useSelector(state => state.login.jwt); + const profile = useSelector(state => state.login.profile) + const {Api} = useApi(); + + async function initProfile() { + if (jwt && !profile) { + let rsp = await Api.getUser("me"); + if (rsp.ok) { + dispatch(setProfile(await rsp.json())); + } else { + dispatch(logout()); + } + } + } + + useEffect(() => { + initProfile(); + }, []); return (
diff --git a/VoidCat/spa/src/LoginState.js b/VoidCat/spa/src/LoginState.js index 94a08f7..c87118b 100644 --- a/VoidCat/spa/src/LoginState.js +++ b/VoidCat/spa/src/LoginState.js @@ -1,30 +1,25 @@ import {createSlice} from "@reduxjs/toolkit"; const LocalStorageKey = "token"; -const LocalStorageProfileKey = "profile"; - export const LoginState = createSlice({ name: "Login", initialState: { jwt: window.localStorage.getItem(LocalStorageKey), - profile: JSON.parse(window.localStorage.getItem(LocalStorageProfileKey)) + profile: null }, reducers: { setAuth: (state, action) => { state.jwt = action.payload.jwt; state.profile = action.payload.profile; window.localStorage.setItem(LocalStorageKey, state.jwt); - window.localStorage.setItem(LocalStorageProfileKey, JSON.stringify(state.profile)); }, setProfile: (state, action) => { state.profile = action.payload; - window.localStorage.setItem(LocalStorageProfileKey, JSON.stringify(state.profile)); }, logout: (state) => { state.jwt = null; state.profile = null; window.localStorage.removeItem(LocalStorageKey); - window.localStorage.removeItem(LocalStorageProfileKey); } } });