auth init

This commit is contained in:
ntr committed 2022-08-08 19:51:17 +10:00
1 parent faa2e70e08
commit 413aeedadf
29 files changed
+2854 -649

No files matched your search

+1 -1
View File
@@ -5,7 +5,7 @@
"license": "MIT",
"scripts": {
"build": "preact build",
"start": "preact watch",
"start": "preact --no-clear watch",
"lint": "eslint src"
},
"eslintConfig": {
+16 -8
View File
@@ -1,17 +1,25 @@
import axios from 'axios';
const actions = store => ({
const API_URL = 'http://localhost:41337';
axios.defaults.withCredentials = true;
axios.defaults.crossDomain = true;
const actions = {
getBundles: async () => {
const { data: bundles } = await axios.get('http://localhost:41337/bundles');
return { bundles };
const { data: bundles } = await axios.get(`${API_URL}/bundles`);
return bundles;
},
getUserOffers: async (state, userId) => {
store.setState({ offers: [] });
const { data: offers } = await axios.get(`http://localhost:41337/user/${userId}/offers`);
console.log(userId, offers);
return { offers };
const { data: offers } = await axios.get(`${API_URL}/user/${userId}/offers`);
return offers;
},
});
getOffers: async (userId) => {
const { data } = await axios.get(`${API_URL}/user/${userId}/offers`);
return data;
},
};
export default actions;
+4
View File
@@ -8,6 +8,8 @@ import actions from './actions';
import Header from './components/header';
import Home from './routes/home';
import Login from './routes/login';
import Register from './routes/register';
import Search from './routes/search';
import User from './routes/user';
@@ -21,6 +23,8 @@ const App = () => (
<Header />
<Router>
<Route path="/" component={Home} />
<Route path="/login" component={Login} />
<Route path="/register" component={Register} />
<Route path="/search" component={Search} />
<Route path="/user/:userId" component={User} />
</Router>
+24 -23
View File
@@ -1,30 +1,31 @@
import { h } from 'preact';
import { connect } from 'unistore/preact'
import { useState, useEffect } from 'preact/hooks';
import actions from './../actions';
import Bundle from './../components/bundle';
interface Props {
bundles: any[];
getBundles: () => void,
function Home() {
const [bundles, setBundles] = useState([]);
useEffect(() => {
actions.getBundles().then(setBundles);
}, []);
if (!bundles.length) {
return (
<div class={'home'}>
loading...
</div>
);
}
return (
<div class={'home'}>
{bundles.map(Bundle)}
</div>
);
}
function Home({ bundles, getBundles }: Props) {
if (!bundles.length) {
getBundles();
return (
<div class={'home'}>
loading...
</div>
);
}
return (
<div class={'home'}>
{bundles.map(Bundle)}
</div>
);
}
export default connect('bundles', actions)(Home);
export default Home;
+35
View File
@@ -0,0 +1,35 @@
import { h } from 'preact';
import { useState, useEffect } from 'preact/hooks';
import axios from 'axios';
const Register = () => {
const [email, setEmail] = useState('')
const [password, setPassword] = useState('');
async function postLogin() {
const { data } = await axios.post(`http://localhost:41337/auth/login`, {
email,
password,
});
console.log(data);
}
function handleSubmit(e) {
e.preventDefault();
postLogin();
}
return (
<div class={'home'}>
<form action="" method="get" >
<label for="email">email: </label>
<input type="email" name="email" id="email" value={email} required onChange={(e) => setEmail(e.target.value)} />
<label for="password">password: </label>
<input type="password" name="password" id="password" value={password} required onChange={(e) => setPassword(e.target.value)} />
<input type="submit" value="Login" onClick={handleSubmit} />
</form>
</div>
);
};
export default Register;
+35
View File
@@ -0,0 +1,35 @@
import { h } from 'preact';
import { useState, useEffect } from 'preact/hooks';
import axios from 'axios';
const Register = () => {
const [email, setEmail] = useState('')
const [password, setPassword] = useState('');
async function postRegister() {
const { data } = await axios.post(`http://localhost:41337/auth/register`, {
email,
password,
});
console.log(data);
}
function handleSubmit(e) {
e.preventDefault();
postRegister();
}
return (
<div class={'home'}>
<form action="" method="get" >
<label for="email">email: </label>
<input type="email" name="email" id="email" value={email} required onChange={(e) => setEmail(e.target.value)} />
<label for="password">password: </label>
<input type="password" name="password" id="password" value={password} required onChange={(e) => setPassword(e.target.value)} />
<input type="submit" value="Register" onClick={handleSubmit} />
</form>
</div>
);
};
export default Register;
+16 -23
View File
@@ -1,9 +1,7 @@
import { h } from 'preact';
import { useState, useEffect } from 'preact/hooks';
import { connect } from 'unistore/preact';
import axios from 'axios';
import style from './style.less';
// import style from './style.less';
import actions from './../actions';
import Bundle from './../components/bundle';
@@ -14,31 +12,26 @@ interface Props {
}
const User = ({ userId }: Props) => {
const [offers, setOffers] = useState([]);
const [offers, setOffers] = useState([]);
useEffect(() => {
async function fetchOffers() {
const { data } = await axios.get(`http://localhost:41337/user/${userId}/offers`);
setOffers(data);
}
fetchOffers();
useEffect(() => {
actions.getOffers(userId).then(setOffers);
}, [userId]);
if (!offers.length) {
return (
<div class={'home'}>
loading...
</div>
);
}
if (!offers.length) {
return (
<div class={'home'}>
loading...
</div>
);
}
return (
<div class={'home'}>
{offers.map(Bundle)}
</div>
);
<div class={'home'}>
{offers.map(Bundle)}
</div>
);
};
export default connect(['offers'], actions)(User);
export default User;