forked from Kieran/void.cat
Load profile on page load
This commit is contained in:
parent
990d636fba
commit
11b94c3ff9
@ -19,24 +19,25 @@ public class UserController : Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
[Route("")]
|
|
||||||
public async Task<IActionResult> GetUser([FromRoute] string id)
|
public async Task<IActionResult> GetUser([FromRoute] string id)
|
||||||
{
|
{
|
||||||
var loggedUser = HttpContext.GetUserId();
|
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)
|
if (loggedUser == requestedId)
|
||||||
{
|
{
|
||||||
return Json(await _store.Get<PrivateVoidUser>(id.FromBase58Guid()));
|
return Json(await _store.Get<PrivateVoidUser>(requestedId));
|
||||||
}
|
}
|
||||||
|
|
||||||
var user = await _store.Get<PublicVoidUser>(id.FromBase58Guid());
|
var user = await _store.Get<PublicVoidUser>(requestedId);
|
||||||
if (!(user?.Flags.HasFlag(VoidUserFlags.PublicProfile) ?? false)) return NotFound();
|
if (!(user?.Flags.HasFlag(VoidUserFlags.PublicProfile) ?? false)) return NotFound();
|
||||||
|
|
||||||
return Json(user);
|
return Json(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
[Route("")]
|
|
||||||
public async Task<IActionResult> UpdateUser([FromRoute] string id, [FromBody] PublicVoidUser user)
|
public async Task<IActionResult> UpdateUser([FromRoute] string id, [FromBody] PublicVoidUser user)
|
||||||
{
|
{
|
||||||
var loggedUser = await GetAuthorizedUser(id);
|
var loggedUser = await GetAuthorizedUser(id);
|
||||||
|
@ -1,10 +1,31 @@
|
|||||||
import "./Header.css";
|
import "./Header.css";
|
||||||
import {Link} from "react-router-dom";
|
import {Link} from "react-router-dom";
|
||||||
import {useSelector} from "react-redux";
|
import {useDispatch, useSelector} from "react-redux";
|
||||||
import {InlineProfile} from "./InlineProfile";
|
import {InlineProfile} from "./InlineProfile";
|
||||||
|
import {useApi} from "./Api";
|
||||||
|
import {logout, setProfile} from "./LoginState";
|
||||||
|
import {useEffect} from "react";
|
||||||
|
|
||||||
export function Header() {
|
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 (
|
return (
|
||||||
<div className="header page">
|
<div className="header page">
|
||||||
|
@ -1,30 +1,25 @@
|
|||||||
import {createSlice} from "@reduxjs/toolkit";
|
import {createSlice} from "@reduxjs/toolkit";
|
||||||
|
|
||||||
const LocalStorageKey = "token";
|
const LocalStorageKey = "token";
|
||||||
const LocalStorageProfileKey = "profile";
|
|
||||||
|
|
||||||
export const LoginState = createSlice({
|
export const LoginState = createSlice({
|
||||||
name: "Login",
|
name: "Login",
|
||||||
initialState: {
|
initialState: {
|
||||||
jwt: window.localStorage.getItem(LocalStorageKey),
|
jwt: window.localStorage.getItem(LocalStorageKey),
|
||||||
profile: JSON.parse(window.localStorage.getItem(LocalStorageProfileKey))
|
profile: null
|
||||||
},
|
},
|
||||||
reducers: {
|
reducers: {
|
||||||
setAuth: (state, action) => {
|
setAuth: (state, action) => {
|
||||||
state.jwt = action.payload.jwt;
|
state.jwt = action.payload.jwt;
|
||||||
state.profile = action.payload.profile;
|
state.profile = action.payload.profile;
|
||||||
window.localStorage.setItem(LocalStorageKey, state.jwt);
|
window.localStorage.setItem(LocalStorageKey, state.jwt);
|
||||||
window.localStorage.setItem(LocalStorageProfileKey, JSON.stringify(state.profile));
|
|
||||||
},
|
},
|
||||||
setProfile: (state, action) => {
|
setProfile: (state, action) => {
|
||||||
state.profile = action.payload;
|
state.profile = action.payload;
|
||||||
window.localStorage.setItem(LocalStorageProfileKey, JSON.stringify(state.profile));
|
|
||||||
},
|
},
|
||||||
logout: (state) => {
|
logout: (state) => {
|
||||||
state.jwt = null;
|
state.jwt = null;
|
||||||
state.profile = null;
|
state.profile = null;
|
||||||
window.localStorage.removeItem(LocalStorageKey);
|
window.localStorage.removeItem(LocalStorageKey);
|
||||||
window.localStorage.removeItem(LocalStorageProfileKey);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user