Skip to content

Commit

Permalink
Merge pull request #37 from Code-the-Dream-School/TokenUserInfo
Browse files Browse the repository at this point in the history
Token user info
  • Loading branch information
devandres-tech authored Dec 13, 2024
2 parents 3c635f5 + 1e8d1a9 commit 6f1e5c6
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 37 deletions.
24 changes: 15 additions & 9 deletions data.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,21 @@ const drugFormData = [
name: '',
generic: '',
class: [
'Analgesics',
'Antibiotics',
'Antidepressants',
'Antipsychotics',
'Antihypertensives',
'Anticoagulants',
'Bronchodilators',
'Diruretics',
'Antivirals',
'ACEI',
'Alpha Blockers',
'Analgesic',
'Angiotensin II',
'Antipyretic',
'CCBs',
'COX-2 Inhibitors',
'Histamine-2 Receptor Blockers',
'HMG CoA RIs',
'LHRH',
'NSAID',
'Prokinetic Agents',
'PPIs',
'SSRIs',
'Other',
],
quantity: '',
expiration: '',
Expand Down
34 changes: 34 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"react-dom": "^18.2.0",
"react-icons": "^5.4.0",
"react-router-dom": "^6.28.0",
"react-toastify": "^9.1.2",
"styled-components": "^6.1.13"
},
"devDependencies": {
Expand Down
5 changes: 4 additions & 1 deletion src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import {
} from './pages';

import { action as registerAction } from './pages/Register';

import { action as loginAction } from './pages/Login';
import { loader as dashboardLoader } from './pages/Dashboard';
const router = createBrowserRouter([
{
path: '/',
Expand All @@ -41,10 +42,12 @@ const router = createBrowserRouter([
{
path: 'login',
element: <Login />,
action: loginAction,
},
{
path: 'dashboard',
element: <Dashboard />,
loader: dashboardLoader,
children: [
{
index: 'true',
Expand Down
3 changes: 3 additions & 0 deletions src/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ import React from 'react';
import { ChakraProvider, defaultSystem } from '@chakra-ui/react';
import ReactDOM from 'react-dom/client';
import App from './App.jsx';
import 'react-toastify/dist/ReactToastify.css';
import './index.css';
import { ToastContainer } from 'react-toastify';

ReactDOM.createRoot(document.getElementById('root')).render(
<React.StrictMode>
<ChakraProvider value={defaultSystem}>
<App />
<ToastContainer position="top-center" />
</ChakraProvider>
</React.StrictMode>
);
29 changes: 28 additions & 1 deletion src/pages/Dashboard.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,38 @@
import { useState, createContext, useContext } from 'react';
import { Outlet } from 'react-router-dom';
import { Outlet, useLoaderData } from 'react-router-dom';
import styled from 'styled-components';
import { BigSidebar, Navbar, SmallSidebar } from '../components';
import customFetch from '../util/customFetch';

export const loader = async () => {
try {
// Retrieve the token from localStorage
const token = localStorage.getItem('token');
const userId = localStorage.getItem('userId');

// Make a GET request to the endpoint
const response = await customFetch.get(`/users/${userId}`, {
headers: {
Authorization: `Bearer ${token}`, // Add the token to the Authorization header
},
});

// Handle the response
console.log('User Details:', response.data);
return response.data; // Return the user details
} catch (error) {
// Handle errors
console.error('Error fetching user details:', error?.response?.data || error.message);
throw error; // Re-throw the error for further handling if necessary
}
};

const DashboardContext = createContext();

const Dashboard = () => {
const data = useLoaderData();
console.log(data);

const user = { name: 'john' };
const [showSidebar, setShowSidebar] = useState(false);

Expand Down
3 changes: 2 additions & 1 deletion src/pages/Landing.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const Landing = () => {
<Link to="/register" className="btn register-link">
Register
</Link>
<Link to="/register" className="btn register-link">
<Link to="/login" className="btn register-link">
Login
</Link>
</div>
Expand All @@ -47,6 +47,7 @@ const Wrapper = styled.section`
height: 8rem;
display: flex;
align-items: center;
margin-bottom: 3rem;
}
.mylogo {
object-fit: cover;
Expand Down
75 changes: 60 additions & 15 deletions src/pages/Login.jsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,83 @@
import { Link } from 'react-router-dom';
import { Link, Form, redirect, useNavigation, useActionData } from 'react-router-dom';
import styled from 'styled-components';
import { FormRow, Logo } from '../components';
import { toast } from 'react-toastify';
import customFetch from '../util/customFetch';

// export const action = async ({ request }) => {
// const formData = await request.formData();
// const data = Object.fromEntries(formData);
// const errors = { msg: '' };
// if (data.password.length < 3) {
// errors.msg = 'Password too short';
// return errors;
// }
// try {
// await customFetch.post('/auth/login', data);
// toast.success('Login Successful');
// return redirect('/dashboard');
// } catch (error) {
// toast.error(error?.response?.data?.msg);
// }
// return null;
// };

export const action = async ({ request }) => {
const formData = await request.formData();
const data = Object.fromEntries(formData);

const errors = { msg: '' };

if (data.password.length < 3) {
errors.msg = 'Password too short';
return errors;
}

try {
const response = await customFetch.post('/auth/login', data);
const { token, user } = response.data;
//SET THEM IN LOCAL STORAGE
localStorage.setItem('token', token);
localStorage.setItem('userId', user.id);
toast.success('Login Successful');
return redirect('/dashboard');
} catch (error) {
// Handle errors
toast.error(error?.response?.data?.msg || 'Something went wrong');
}

return null;
};

const Login = () => {
const errors = useActionData();
const navigation = useNavigation();
const isSubmitting = navigation.state === 'submitting';

return (
<Wrapper>
<form className="form">
<Form method="post" className="form">
<Logo />
<h4>Login</h4>
<FormRow
type="email"
name="email"
labelText="email"
defaultValue="[email protected]"
placeholder="EMAIL"
/>
{errors?.msg && <p style={{ color: 'red' }}>{errors.msg}</p>}

<FormRow type="email" name="email" labelText="email" placeholder="EMAIL" />
<FormRow
type="password"
name="password"
labelText="password"
defaultValue="CodeTheDreamPassword"
placeholder="PASSWORD"
placeholder="password"
/>
<button type="submit" className="btn btn-block">
Submit
<button type="submit" className="btn btn-block" disabled={isSubmitting}>
{isSubmitting ? 'submitting...' : 'submit'}
</button>
<p>
Not Registered Yet?
<Link to="/register" className="member-btn">
Register!
</Link>
</p>
</form>
</Form>
</Wrapper>
);
};
Expand All @@ -56,7 +102,6 @@ const Wrapper = styled.section`
}
.form-input {
background-color: var(--grey-50);
text-transform: uppercase;
}
h4 {
text-align: center;
Expand Down
23 changes: 13 additions & 10 deletions src/pages/Register.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Form, redirect, Link } from 'react-router-dom';
import { Form, redirect, useNavigation, Link } from 'react-router-dom';
import styled from 'styled-components';
import { FormRow, Logo } from '../components';
import customFetch from '../util/customFetch';
import { toast } from 'react-toastify';

//formData - is an api, gives back an array of arrays, must have name same as database
export const action = async ({ request }) => {
Expand All @@ -10,14 +11,18 @@ export const action = async ({ request }) => {
const data = Object.fromEntries(formData);
try {
await customFetch.post('/auth/signup', data);
toast.success('Registration Successful');
return redirect('/login');
} catch (error) {
console.log(error);
toast.error(error?.response?.data?.msg);
return error;
}
};

const Register = () => {
const navigation = useNavigation();
console.log(navigation);
const isSubmitting = navigation.state === 'submitting';
return (
<Wrapper>
<Form method="post" className="form">
Expand All @@ -31,18 +36,17 @@ const Register = () => {
defaultValue="DefaultLastName"
placeholder="LAST NAME"
/> */}
<FormRow type="text" name="store" labelText="store" placeholder="STORE" />
<FormRow type="text" name="role" labelText="role" placeholder="ROLE NAME" />
<FormRow type="email" name="email" labelText="email" placeholder="EMAIL" />
<FormRow type="text" name="store" labelText="store" placeholder="store name" />
<FormRow type="text" name="role" labelText="role" placeholder="name of role" />
<FormRow type="email" name="email" labelText="email" placeholder="email" />
<FormRow
type="password"
name="password"
labelText="password"
defaultValue="CodeTheDreamPassword"
placeholder="PASSWORD"
placeholder="password"
/>
<button type="submit" className="btn btn-block">
Submit
<button type="submit" className="btn btn-block" disabled={isSubmitting}>
{isSubmitting ? 'submitting...' : 'submit'}
</button>
<p>
Already Registered?
Expand Down Expand Up @@ -75,7 +79,6 @@ const Wrapper = styled.section`
}
.form-input {
background-color: var(--grey-50);
text-transform: uppercase;
}
h4 {
text-align: center;
Expand Down

0 comments on commit 6f1e5c6

Please sign in to comment.