Milk Packet
Milk Packet
JCHJava Community | Help. Code. Learn.
Created by Milk Packet on 7/22/2024 in #java-help
How do I use UserDetailsService properly? Where do I define it exactly?
that helped
9 replies
JCHJava Community | Help. Code. Learn.
Created by Milk Packet on 7/22/2024 in #java-help
How do I use UserDetailsService properly? Where do I define it exactly?
Thank you very much!
9 replies
JCHJava Community | Help. Code. Learn.
Created by Milk Packet on 6/15/2024 in #java-help
How to access authenticated user/authentication object on react frontend after logging in?
ill fetch other details like id, name, email, username from a /me endpoint on the backend and just modify the isLoggedIn parameter everytime i submit my form values (from my login page) to true
45 replies
JCHJava Community | Help. Code. Learn.
Created by Milk Packet on 6/15/2024 in #java-help
How to access authenticated user/authentication object on react frontend after logging in?
something like this? (i tried implementing it for the first time so not sure)
const { createSlice } = require("@reduxjs/toolkit");

const initialState = {
id: null,
name: "",
email: "",
username: "",
isLoggedIn: false,
};

const userSlice = createSlice({
name: "user",
initialState,
reducers: {
login: (state, action) => {
state.id = action.payload.id;
state.name = action.payload.name;
state.email = action.payload.email;
state.username = action.payload.username;
state.isLoggedIn = true;
},
logout: (state) => {
state.id = null;
state.name = "";
state.email = "";
state.username = "";
state.isLoggedIn = false;
},
},
});

export const { login, logout } = userSlice.actions;
export default userSlice.reducer;
const { createSlice } = require("@reduxjs/toolkit");

const initialState = {
id: null,
name: "",
email: "",
username: "",
isLoggedIn: false,
};

const userSlice = createSlice({
name: "user",
initialState,
reducers: {
login: (state, action) => {
state.id = action.payload.id;
state.name = action.payload.name;
state.email = action.payload.email;
state.username = action.payload.username;
state.isLoggedIn = true;
},
logout: (state) => {
state.id = null;
state.name = "";
state.email = "";
state.username = "";
state.isLoggedIn = false;
},
},
});

export const { login, logout } = userSlice.actions;
export default userSlice.reducer;
45 replies
JCHJava Community | Help. Code. Learn.
Created by Milk Packet on 6/15/2024 in #java-help
How to access authenticated user/authentication object on react frontend after logging in?
its only the frontend side of auth im worried about
45 replies
JCHJava Community | Help. Code. Learn.
Created by Milk Packet on 6/15/2024 in #java-help
How to access authenticated user/authentication object on react frontend after logging in?
my apis are already protected using spring security so im not worried about that.
45 replies
JCHJava Community | Help. Code. Learn.
Created by Milk Packet on 6/15/2024 in #java-help
How to access authenticated user/authentication object on react frontend after logging in?
my entire application (except the login, register and verification pages and backend api) is protected behind authentication. its a social networking application. so, if a user is logged in, they are redirected to the dashboard page which has multiple other sub pages they can visit like events, home feed, announcements, etc. I don't want the user to be able to access those pages if they're not logged in and redirect them to /login instead.
45 replies
JCHJava Community | Help. Code. Learn.
Created by Milk Packet on 6/15/2024 in #java-help
How to access authenticated user/authentication object on react frontend after logging in?
tanstack has a built in feature to check for protected routes. @Tomasm21
45 replies
JCHJava Community | Help. Code. Learn.
Created by Milk Packet on 6/15/2024 in #java-help
How to access authenticated user/authentication object on react frontend after logging in?
thats how its setup
45 replies
JCHJava Community | Help. Code. Learn.
Created by Milk Packet on 6/15/2024 in #java-help
How to access authenticated user/authentication object on react frontend after logging in?
every other route, login.jsx in this example
import { createFileRoute } from "@tanstack/react-router";
import LogIn from "src/pages/Login";

export const Route = createFileRoute("/login")({
component: LogIn,
});
import { createFileRoute } from "@tanstack/react-router";
import LogIn from "src/pages/Login";

export const Route = createFileRoute("/login")({
component: LogIn,
});
45 replies
JCHJava Community | Help. Code. Learn.
Created by Milk Packet on 6/15/2024 in #java-help
How to access authenticated user/authentication object on react frontend after logging in?
__root.jsx
import { createRootRoute, Outlet } from "@tanstack/react-router";

export const Route = createRootRoute({
component: () => (
<>
<Outlet />
</>
),
});
import { createRootRoute, Outlet } from "@tanstack/react-router";

export const Route = createRootRoute({
component: () => (
<>
<Outlet />
</>
),
});
45 replies
JCHJava Community | Help. Code. Learn.
Created by Milk Packet on 6/15/2024 in #java-help
How to access authenticated user/authentication object on react frontend after logging in?
No description
45 replies
JCHJava Community | Help. Code. Learn.
Created by Milk Packet on 6/15/2024 in #java-help
How to access authenticated user/authentication object on react frontend after logging in?
Here's how I setup my react vite frontend main.jsx
import React from "react";
import ReactDOM from "react-dom/client";
import { NextUIProvider } from "@nextui-org/react";
import { RouterProvider, createRouter } from "@tanstack/react-router";
import { routeTree } from "./routeTree.gen";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { Provider } from "react-redux";
import { store } from "./redux/store";

import "./index.css";

const queryClient = new QueryClient({});

const router = createRouter({
routeTree,
defaultPreload: "intent",
});

ReactDOM.createRoot(document.getElementById("root")).render(
<React.StrictMode>
<Provider store={store}>
<QueryClientProvider client={queryClient}>
<NextUIProvider>
<RouterProvider router={router} />
</NextUIProvider>
</QueryClientProvider>
</Provider>
</React.StrictMode>
);
import React from "react";
import ReactDOM from "react-dom/client";
import { NextUIProvider } from "@nextui-org/react";
import { RouterProvider, createRouter } from "@tanstack/react-router";
import { routeTree } from "./routeTree.gen";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { Provider } from "react-redux";
import { store } from "./redux/store";

import "./index.css";

const queryClient = new QueryClient({});

const router = createRouter({
routeTree,
defaultPreload: "intent",
});

ReactDOM.createRoot(document.getElementById("root")).render(
<React.StrictMode>
<Provider store={store}>
<QueryClientProvider client={queryClient}>
<NextUIProvider>
<RouterProvider router={router} />
</NextUIProvider>
</QueryClientProvider>
</Provider>
</React.StrictMode>
);
45 replies
JCHJava Community | Help. Code. Learn.
Created by Milk Packet on 6/15/2024 in #java-help
How to access authenticated user/authentication object on react frontend after logging in?
im using tanstack's file based routing btw
45 replies
JCHJava Community | Help. Code. Learn.
Created by Milk Packet on 6/15/2024 in #java-help
How to access authenticated user/authentication object on react frontend after logging in?
ill be back with any doubts xD hope you dont mind
45 replies
JCHJava Community | Help. Code. Learn.
Created by Milk Packet on 6/15/2024 in #java-help
How to access authenticated user/authentication object on react frontend after logging in?
this helps. thank you very much!
45 replies
JCHJava Community | Help. Code. Learn.
Created by Milk Packet on 6/15/2024 in #java-help
How to access authenticated user/authentication object on react frontend after logging in?
Ill try using redux global state method for now and see how it goes. thank you!
45 replies
JCHJava Community | Help. Code. Learn.
Created by Milk Packet on 6/15/2024 in #java-help
How to access authenticated user/authentication object on react frontend after logging in?
all I have is a csrf protection implemented and nothing for XSS 😦
45 replies
JCHJava Community | Help. Code. Learn.
Created by Milk Packet on 6/15/2024 in #java-help
How to access authenticated user/authentication object on react frontend after logging in?
which one would you suggest? state (with redux tlk cuz thats what i plan to use) or localstorage? not to forget about XSS attacks on localstorage
45 replies
JCHJava Community | Help. Code. Learn.
Created by Milk Packet on 6/15/2024 in #java-help
How to access authenticated user/authentication object on react frontend after logging in?
oh so everytime I login, I can update the state (say, isAuthenticated) to true and when I logout, i can update it to false. i can then use this state to decide whether or not to show the page or redirect to /login, right?
45 replies