update
This commit is contained in:
3
.env
3
.env
@@ -1,4 +1,5 @@
|
||||
VITE_API_URL=https://ssa.betadelivery.com/apia/v1
|
||||
VITE_API_URL='https://ssa.betadelivery.com/apia/v1'
|
||||
# VITE_API_URL='http://192.16.50.44/seo-backend/apia/v1'
|
||||
VITE_USER_NAME="Admin"
|
||||
VITE_PASSWORD="71%@L%es^bUX94`J9XT*%4&^%tUU^%Q^ffgt"
|
||||
VITE_APP_NAME=MyViteApp
|
||||
@@ -82,7 +82,7 @@ define(['./workbox-54d0af47'], (function (workbox) { 'use strict';
|
||||
"revision": "3ca0b8505b4bec776b69afdba2768812"
|
||||
}, {
|
||||
"url": "index.html",
|
||||
"revision": "0.tg13lftuk78"
|
||||
"revision": "0.i87c5gdh6n"
|
||||
}], {});
|
||||
workbox.cleanupOutdatedCaches();
|
||||
workbox.registerRoute(new workbox.NavigationRoute(workbox.createHandlerBoundToURL("index.html"), {
|
||||
|
||||
18
package-lock.json
generated
18
package-lock.json
generated
@@ -15,6 +15,7 @@
|
||||
"axios": "^1.7.9",
|
||||
"chart.js": "^4.4.7",
|
||||
"framer-motion": "^11.18.0",
|
||||
"js-cookie": "^3.0.5",
|
||||
"next-themes": "^0.4.4",
|
||||
"react": "^18.3.1",
|
||||
"react-chartjs-2": "^5.3.0",
|
||||
@@ -28,6 +29,7 @@
|
||||
"devDependencies": {
|
||||
"@chakra-ui/cli": "^3.2.3",
|
||||
"@eslint/js": "^9.17.0",
|
||||
"@types/js-cookie": "^3.0.6",
|
||||
"@types/react": "^18.3.18",
|
||||
"@types/react-dom": "^18.3.5",
|
||||
"@vitejs/plugin-react": "^4.3.4",
|
||||
@@ -3292,6 +3294,13 @@
|
||||
"integrity": "sha512-AYnb1nQyY49te+VRAVgmzfcgjYS91mY5P0TKUDCLEM+gNnA+3T6rWITXRLYCpahpqSQbN5cE+gHpnPyXjHWxcw==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/@types/js-cookie": {
|
||||
"version": "3.0.6",
|
||||
"resolved": "https://registry.npmjs.org/@types/js-cookie/-/js-cookie-3.0.6.tgz",
|
||||
"integrity": "sha512-wkw9yd1kEXOPnvEeEV1Go1MmxtBJL0RR79aOTAApecWFVu7w0NNXNqhcWgvw2YgZDYadliXkl14pa3WXw5jlCQ==",
|
||||
"dev": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/@types/json-schema": {
|
||||
"version": "7.0.15",
|
||||
"resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.15.tgz",
|
||||
@@ -7394,6 +7403,15 @@
|
||||
"node": ">=10"
|
||||
}
|
||||
},
|
||||
"node_modules/js-cookie": {
|
||||
"version": "3.0.5",
|
||||
"resolved": "https://registry.npmjs.org/js-cookie/-/js-cookie-3.0.5.tgz",
|
||||
"integrity": "sha512-cEiJEAEoIbWfCZYKWhVwFuvPX1gETRYPw6LlaTKoxD3s2AkXzkCjnp6h0V77ozyqj0jakteJ4YqDJT830+lVGw==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=14"
|
||||
}
|
||||
},
|
||||
"node_modules/js-tokens": {
|
||||
"version": "4.0.0",
|
||||
"resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz",
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
"axios": "^1.7.9",
|
||||
"chart.js": "^4.4.7",
|
||||
"framer-motion": "^11.18.0",
|
||||
"js-cookie": "^3.0.5",
|
||||
"next-themes": "^0.4.4",
|
||||
"react": "^18.3.1",
|
||||
"react-chartjs-2": "^5.3.0",
|
||||
@@ -30,6 +31,7 @@
|
||||
"devDependencies": {
|
||||
"@chakra-ui/cli": "^3.2.3",
|
||||
"@eslint/js": "^9.17.0",
|
||||
"@types/js-cookie": "^3.0.6",
|
||||
"@types/react": "^18.3.18",
|
||||
"@types/react-dom": "^18.3.5",
|
||||
"@vitejs/plugin-react": "^4.3.4",
|
||||
|
||||
12
src/App.tsx
12
src/App.tsx
@@ -1,20 +1,20 @@
|
||||
import { useContext } from 'react';
|
||||
import { Route, BrowserRouter as Router, Routes } from "react-router-dom";
|
||||
import GlobalStateContext from './Contexts/GlobalStateContext';
|
||||
import DefaultLayout from './Layouts/DefaultLayout';
|
||||
import Login from './Pages/Login';
|
||||
import { RouteLink } from './Routes/Routes';
|
||||
import Cookies from "js-cookie";
|
||||
|
||||
function App() {
|
||||
const context = useContext(GlobalStateContext);
|
||||
if (!context) throw new Error('App must be used within a GlobalStateProvider');
|
||||
const { isAuthenticate } = context;
|
||||
// const context = useContext(GlobalStateContext);
|
||||
// if (!context) throw new Error('App must be used within a GlobalStateProvider');
|
||||
// const { isAuthenticate } = context;
|
||||
const token = Cookies.get("token")
|
||||
|
||||
return (
|
||||
<Router>
|
||||
<Routes>
|
||||
<Route path="/login" element={<Login />} />
|
||||
<Route path="/*" element={isAuthenticate === true ? (<DefaultLayout><Routes>{RouteLink.map(({ path, Component }, index) => (<Route key={index} path={path} element={<Component />} />))}</Routes></DefaultLayout>) : (<Login />)} />
|
||||
<Route path="/*" element={token ? (<DefaultLayout><Routes>{RouteLink.map(({ path, Component }, index) => (<Route key={index} path={path} element={<Component />} />))}</Routes></DefaultLayout>) : (<Login />)} />
|
||||
<Route path="*" element={<Login />} />
|
||||
</Routes>
|
||||
</Router>
|
||||
|
||||
@@ -9,6 +9,8 @@ import logo from '../assets/logo.svg'
|
||||
import { Button } from "../components/ui/button"
|
||||
import { Field } from "../components/ui/field"
|
||||
import { Toaster } from "../components/ui/toaster"
|
||||
import { PasswordInput } from "../components/ui/password-input"
|
||||
import { useNavigate } from "react-router-dom"
|
||||
|
||||
interface FormValues {
|
||||
mobileNumber: number
|
||||
@@ -16,6 +18,7 @@ interface FormValues {
|
||||
}
|
||||
|
||||
const Login = () => {
|
||||
const navigate = useNavigate()
|
||||
const dispatch = useDispatch()
|
||||
const [isLoading, setIsLoading] = useState<boolean>(false)
|
||||
const context = useContext(GlobalStateContext);
|
||||
@@ -35,13 +38,13 @@ const Login = () => {
|
||||
// Encode Basic Auth Credentials
|
||||
const username = import.meta.env.VITE_USER_NAME||''; // Replace with actual username
|
||||
const password = import.meta.env.VITE_PASSWORD||''; // Replace with actual password
|
||||
const basicAuth = btoa(`${username}:${password}`); // Encode to Base64
|
||||
const basicAuth = `${username} : ${password}`; // Encode to Base64
|
||||
|
||||
try {
|
||||
const response = await axios.post(
|
||||
`${import.meta.env.VITE_API_URL}/v1/login`,
|
||||
const res = await axios.post(
|
||||
`${import.meta.env.VITE_API_URL}/login`,
|
||||
{
|
||||
mobile_number: data.mobileNumber,
|
||||
mobile_number: Number(data.mobileNumber),
|
||||
password: data.password,
|
||||
},
|
||||
{
|
||||
@@ -52,10 +55,21 @@ const Login = () => {
|
||||
}
|
||||
);
|
||||
|
||||
console.log("====================================");
|
||||
console.log(response);
|
||||
console.log("====================================");
|
||||
dispatch(setToken(String(response.data["access-token"])));
|
||||
if (res.data) {
|
||||
setIsAuthenticate(true)
|
||||
console.log('====================================');
|
||||
console.log(res.data?.data);
|
||||
console.log('====================================');
|
||||
navigate('/')
|
||||
dispatch(setToken(String(res.data?.data["access-token"])));
|
||||
} else {
|
||||
|
||||
console.log("====================================");
|
||||
console.log(res);
|
||||
console.log("====================================");
|
||||
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
if (error) {
|
||||
|
||||
@@ -101,11 +115,11 @@ const Login = () => {
|
||||
|
||||
<VStack mt={6} gap={4} w={'full'}>
|
||||
<Field color={'#313039'} label={'Enter Mobile Number'} w={'100%'} invalid={!!errors.mobileNumber} errorText={errors.mobileNumber?.message} >
|
||||
<Input ps={3} {...register("mobileNumber", { required: "Mobile Number address is required" })} placeholder="Mobile Number Address" />
|
||||
<Input type="number" ps={3} {...register("mobileNumber", { required: "Mobile Number address is required" })} placeholder="Mobile Number Address" />
|
||||
{/* <Text as={'span'} w={'100%'} fontSize={'xs'} fontWeight={'normal'} color={'#686677'}>Forget password</Text> */}
|
||||
</Field>
|
||||
<Field color={'#313039'} label={'Enter Mobile Number'} w={'100%'} invalid={!!errors.password} errorText={errors.password?.message} >
|
||||
<Input ps={3} {...register("password", { required: "Pasword is required" })} type="password" placeholder="Enter password" />
|
||||
<Field color={'#313039'} label={'Enter Mobile Number'} w={'100%'} invalid={!!errors.password} errorText={errors.password?.message} >
|
||||
<PasswordInput ps={3} {...register("password", { required: "Pasword is required" })} placeholder="Enter password" />
|
||||
{/* <Text as={'span'} w={'100%'} fontSize={'xs'} fontWeight={'normal'} color={'#686677'}>Forget password</Text> */}
|
||||
</Field>
|
||||
<Button loading={isLoading} mt={4} size={'sm'} bg={'#02A0A0'} rounded={'md'} w={'100%'} color={'#ffffff'} type="submit">Login</Button>
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
|
||||
import Cookies from "js-cookie";
|
||||
|
||||
type AuthState = {
|
||||
token: string | null;
|
||||
};
|
||||
|
||||
const initialState: AuthState = {
|
||||
token: localStorage.getItem("token"), // Load token from localStorage
|
||||
token: Cookies.get("token") || null, // Load token from cookies
|
||||
};
|
||||
|
||||
const authSlice = createSlice({
|
||||
@@ -14,11 +15,11 @@ const authSlice = createSlice({
|
||||
reducers: {
|
||||
setToken: (state, action: PayloadAction<string>) => {
|
||||
state.token = action.payload;
|
||||
localStorage.setItem("token", action.payload);
|
||||
Cookies.set("token", action.payload, { expires: 7, secure: true, sameSite: "Strict" }); // Store in cookies for 7 days
|
||||
},
|
||||
logout: (state) => {
|
||||
state.token = null;
|
||||
localStorage.removeItem("token");
|
||||
Cookies.remove("token");
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
@@ -6,7 +6,7 @@ import { VitePWA } from "vite-plugin-pwa";
|
||||
export default defineConfig({
|
||||
server: {
|
||||
host: "0.0.0.0",
|
||||
port: 3001, // You can use any port
|
||||
port: 3000, // You can use any port
|
||||
},
|
||||
plugins: [
|
||||
react(),
|
||||
|
||||
Reference in New Issue
Block a user