more auth stuff
This commit is contained in:
1 parent
65e603f222
commit
a8e5e5d7cd
7 files changed
+98
-53
No files matched your search
+22
-5
@@ -7,9 +7,7 @@ const api = axios.create({
|
||||
|
||||
const actions = {
|
||||
getBundles: async () => {
|
||||
const res = await api.get('/bundles');
|
||||
const { data: bundles } = res;
|
||||
console.log(res);
|
||||
const { data: bundles } = await api.get('/bundles');
|
||||
return bundles;
|
||||
},
|
||||
|
||||
@@ -23,13 +21,32 @@ const actions = {
|
||||
return data;
|
||||
},
|
||||
|
||||
getLogin: async () => {
|
||||
try {
|
||||
const { data: user } = await api.get('/auth/login')
|
||||
console.log(user);
|
||||
return user;
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
},
|
||||
|
||||
postLogin: async (email, password) => {
|
||||
const res = await api.post('/auth/login', {
|
||||
const { data } = await api.post('/auth/login', {
|
||||
email,
|
||||
password,
|
||||
});
|
||||
|
||||
return res.data;
|
||||
return data;
|
||||
},
|
||||
|
||||
postRegister: async (email, password) => {
|
||||
const { data } = await api.post('/auth/register', {
|
||||
email,
|
||||
password,
|
||||
});
|
||||
|
||||
return data;
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
+23
-11
@@ -1,8 +1,8 @@
|
||||
import { h } from 'preact';
|
||||
import { Route, Router } from 'preact-router';
|
||||
import { Provider } from 'unistore/preact'
|
||||
import { useState, useEffect } from 'preact/hooks';
|
||||
import { Route, Router, route } from 'preact-router';
|
||||
|
||||
import mcratesStore from './store';
|
||||
// import mcratesStore from './store';
|
||||
import actions from './actions';
|
||||
|
||||
import Header from './components/header';
|
||||
@@ -13,23 +13,35 @@ import Register from './routes/register';
|
||||
import Search from './routes/search';
|
||||
import User from './routes/user';
|
||||
|
||||
const store = mcratesStore();
|
||||
// const store = mcratesStore();
|
||||
|
||||
console.log(Home);
|
||||
function App() {
|
||||
const [user, setUser] = useState(null);
|
||||
|
||||
const App = () => (
|
||||
<Provider store={store}>
|
||||
useEffect(() => {
|
||||
async function getLogin() {
|
||||
const user = await actions.getLogin();
|
||||
if (!user) return route('/login');
|
||||
}
|
||||
|
||||
getLogin();
|
||||
}, []);
|
||||
|
||||
function loginSuccess(user) {
|
||||
setUser(user);
|
||||
}
|
||||
|
||||
return (
|
||||
<div id="app">
|
||||
<Header />
|
||||
<Router>
|
||||
<Route path="/" component={Home} />
|
||||
<Route path="/login" component={Login} />
|
||||
<Route path="/register" component={Register} />
|
||||
<Route path="/login" component={Login} success={loginSuccess} />
|
||||
<Route path="/search" component={Search} />
|
||||
<Route path="/user/:userId" component={User} />
|
||||
</Router>
|
||||
</div>
|
||||
</Provider>
|
||||
);
|
||||
);
|
||||
}
|
||||
|
||||
export default App;
|
||||
@@ -1,13 +1,15 @@
|
||||
import { h } from 'preact';
|
||||
import { useState, useEffect } from 'preact/hooks';
|
||||
import { useState } from 'preact/hooks';
|
||||
import actions from './../actions';
|
||||
|
||||
const Register = () => {
|
||||
const Login = ({ success }) => {
|
||||
const [mode, setMode] = useState('login')
|
||||
const [email, setEmail] = useState('')
|
||||
const [password, setPassword] = useState('');
|
||||
|
||||
async function postLogin() {
|
||||
actions.postLogin(email, password);
|
||||
const user = await actions.postLogin(email, password);
|
||||
success(user);
|
||||
}
|
||||
|
||||
function handleSubmit(e) {
|
||||
@@ -28,4 +30,4 @@ const Register = () => {
|
||||
);
|
||||
};
|
||||
|
||||
export default Register;
|
||||
export default Login;
|
||||
@@ -1,17 +1,15 @@
|
||||
import { h } from 'preact';
|
||||
import { useState, useEffect } from 'preact/hooks';
|
||||
import axios from 'axios';
|
||||
import { useState } from 'preact/hooks';
|
||||
|
||||
const Register = () => {
|
||||
import actions from './../actions';
|
||||
|
||||
const Register = ({ success }) => {
|
||||
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);
|
||||
const user = await actions.postRegister(email, password);
|
||||
success(user);
|
||||
}
|
||||
|
||||
function handleSubmit(e) {
|
||||
@@ -19,13 +17,21 @@ const Register = () => {
|
||||
postRegister();
|
||||
}
|
||||
|
||||
function emailChange(e) {
|
||||
setEmail(e.target.value);
|
||||
}
|
||||
|
||||
function passwordChange(e) {
|
||||
setPassword(e.target.value);
|
||||
}
|
||||
|
||||
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)} />
|
||||
<input type="email" name="email" id="email" value={email} required onChange={emailChange} />
|
||||
<label for="password">password: </label>
|
||||
<input type="password" name="password" id="password" value={password} required onChange={(e) => setPassword(e.target.value)} />
|
||||
<input type="password" name="password" id="password" value={password} required onChange={passwordChange} />
|
||||
<input type="submit" value="Register" onClick={handleSubmit} />
|
||||
</form>
|
||||
</div>
|
||||
|
||||
@@ -1,12 +1,7 @@
|
||||
import { h } from 'preact';
|
||||
import { useState, useEffect } from 'preact/hooks';
|
||||
import { connect } from 'unistore/preact';
|
||||
import { useState } from 'preact/hooks';
|
||||
import axios from 'axios';
|
||||
|
||||
import style from './style.less';
|
||||
import actions from './../actions';
|
||||
import Bundle from './../components/bundle';
|
||||
|
||||
function result(r) {
|
||||
return (
|
||||
<div>{r.title}</div>
|
||||
|
||||
Reference in new issue
Block a user