fixing auth, added registration page

master
franciszek 4 years ago
parent 4ea0a209f5
commit 3a785c212a

@ -1,27 +1,43 @@
import React, { ReactElement, useEffect, useState } from "react"; import axios from "axios";
import React, { ReactElement } from "react";
import "./styles/nav.scss"; import "./styles/nav.scss";
interface Props { interface Props {
isLogged: boolean; isLogged: boolean;
logoutState: () => void;
} }
const NOT_LOGGED = [ const NOT_LOGGED = [
{ name: "aplikacja", href: "/" }, { name: "aplikacja", href: "/" },
{ name: "logowanie", href: "login.html" }, { name: "logowanie", href: "login.html" },
{ name: "rejestracja", href: "register.html" },
]; ];
const LOGGED = [
{ name: "aplikacja", href: "/" },
{ name: "wyloguj", href: "login.html" },
];
function Nav({ isLogged }: Props): ReactElement { const LOGGED = [{ name: "aplikacja", href: "/" }];
function Nav({ isLogged, logoutState }: Props): ReactElement {
const logout = async () => {
await axios.delete(
"http://localhost:8080/api/v1/collections/sessions/current"
);
logoutState();
};
return ( return (
<nav className="navbar"> <nav className="navbar">
<h2 className="navbar__title">Zgłaszańsko web</h2> <h2 className="navbar__title">Zgłaszańsko web</h2>
<div className="navbar__links"> <div className="navbar__links">
{isLogged {isLogged
? LOGGED.map((ele) => ( ? LOGGED.map((ele) => (
<a href={ele.href} className="button-link"> <>
{ele.name} <a href={ele.href} className="button-link">
</a> {ele.name}
</a>
<button
onClick={logout}
className="button-link"
>
Wyloguj
</button>
</>
)) ))
: NOT_LOGGED.map((ele) => ( : NOT_LOGGED.map((ele) => (
<a href={ele.href} className="button-link"> <a href={ele.href} className="button-link">

@ -23,6 +23,12 @@ import { getAddress } from "./location";
import { FileType } from "./file"; import { FileType } from "./file";
const App: FC = () => { const App: FC = () => {
const [isLogged, setIsLogged] = useState(false); const [isLogged, setIsLogged] = useState(false);
const [fileState, fileDispatch] = useReducer(
fileReducer,
filesInitialState
);
const [formState, formDispatch] = useReducer(formReducer, fromInitialState);
const [mapPin, setMapPin] = useState({ lat: 0, lon: 0 });
const checkUser = async (): Promise<void> => { const checkUser = async (): Promise<void> => {
try { try {
@ -39,22 +45,17 @@ const App: FC = () => {
void checkUser(); void checkUser();
}, []); }, []);
//This reducer handles adding and selecting files. useEffect(() => {
//I'm passing in initial state "{files: []}" from fileReducer file. void getAddress(mapPin).then((blob) =>
//State can be changed by using dispatch function with object contaning right type and payload. formDispatch({
//Example: dispatch({type: "ADD_FILE", payload: {img: thisIsImgURL, selected: false}) type: "CHANGE_FIELD",
//State can be accsesed by using fileState object payload: {
//Example: fileState.files field: "address",
//Reducer it self is imported from fileReducer.ts in src/ dir value: blob,
const [fileState, fileDispatch] = useReducer( },
fileReducer, })
filesInitialState );
); }, [mapPin]);
const [formState, formDispatch] = useReducer(formReducer, fromInitialState);
//This hook handles pining location on the map
const [mapPin, setMapPin] = useState({ lat: 0, lon: 0 });
const handleSubmit = async ( const handleSubmit = async (
event: FormEvent<HTMLFormElement> | MouseEvent<HTMLButtonElement> event: FormEvent<HTMLFormElement> | MouseEvent<HTMLButtonElement>
@ -93,18 +94,6 @@ const App: FC = () => {
} }
}; };
useEffect(() => {
void getAddress(mapPin).then((blob) =>
formDispatch({
type: "CHANGE_FIELD",
payload: {
field: "address",
value: blob,
},
})
);
}, [mapPin]);
const MapComponent = () => { const MapComponent = () => {
useMapEvents({ useMapEvents({
click: async (e) => { click: async (e) => {
@ -131,7 +120,7 @@ const App: FC = () => {
return ( return (
<> <>
<Nav isLogged={isLogged} /> <Nav isLogged={isLogged} logoutState={() => setIsLogged(false)} />
<div className="container"> <div className="container">
<section className="container__section"> <section className="container__section">
<h2 className="container__section__title">Zdjęcia</h2> <h2 className="container__section__title">Zdjęcia</h2>

@ -9,6 +9,12 @@ import axios from "axios";
export default function Login(): ReactElement { export default function Login(): ReactElement {
const [isLogged, setIsLogged] = useState(false); const [isLogged, setIsLogged] = useState(false);
const [error, setError] = useState(null);
const [loginState, setLoginState] = useState({
username: "",
password: "",
});
const checkUser = async (): Promise<void> => { const checkUser = async (): Promise<void> => {
try { try {
await axios.get( await axios.get(
@ -22,34 +28,21 @@ export default function Login(): ReactElement {
useEffect(() => { useEffect(() => {
void checkUser(); void checkUser();
}, []); }, []);
const [loginState, setLoginState] = useState({
username: "",
password: "",
});
const [error, setError] = useState(null);
const postLogin = async () => { const postLogin = async () => {
try { try {
await axios.post("http://localhost:8080/api/v1/sessions/", { await axios.post("http://localhost:8080/api/v1/sessions/", {
username: loginState.username, username: loginState.username,
password: loginState.password, password: loginState.password,
}); });
void checkUser();
} catch (e) { } catch (e) {
console.log(e); console.log(e);
} }
}; };
const logout = async () => {
const who = await axios.get( const notLoggedForm = () => {
"http://localhost:8080/api/v1/collections/users/me" return (
);
console.log(who);
const logout = await axios.delete(
"http://localhost:8080/api/v1/collections/sessions/current"
);
console.log(logout);
};
return (
<>
<Nav isLogged={isLogged} />
<form> <form>
<label htmlFor="username"> <label htmlFor="username">
Nazwa użytkownika Nazwa użytkownika
@ -89,8 +82,12 @@ export default function Login(): ReactElement {
Zaloguj Zaloguj
</button> </button>
</form> </form>
);
<button onClick={logout}>Wyloguj</button> };
return (
<>
<Nav isLogged={isLogged} logoutState={() => setIsLogged(false)} />
{isLogged ? <h1>Jesteś zalogowany</h1> : notLoggedForm()}
</> </>
); );
} }

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<link
href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.0.3/leaflet.css"
rel="stylesheet"
/>
<title>Zgłaszańsko</title>
</head>
<body>
<div id="root"></div>
<script src="./register.tsx"></script>
</body>
</html>

@ -0,0 +1,78 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import React, { ReactElement, useEffect, useState } from "react";
import ReactDOM from "react-dom";
import Nav from "./Nav";
import "./styles/reset.css";
import "./styles/app.scss";
import "regenerator-runtime/runtime";
import axios from "axios";
export default function Register(): ReactElement {
const [isLogged, setIsLogged] = useState(false);
const [registerState, setRegisterState] = useState({
email: "",
});
const [error, setError] = useState(null);
const checkUser = async (): Promise<void> => {
try {
await axios.get(
"http://localhost:8080/api/v1/collections/users/me"
);
setIsLogged(true);
} catch (error) {
setIsLogged(false);
}
};
useEffect(() => {
void checkUser();
}, []);
const postRegister = async () => {
try {
await axios.post(
"http://localhost:8080/api/v1/collections/registration-intents",
{
email: registerState.email,
}
);
void checkUser();
} catch (e) {
console.log(e);
}
};
const notLoggedForm = () => {
return (
<form>
<label htmlFor="email">
Adres email
<input
type="email"
name="email"
value={registerState.email}
onChange={(e) =>
setRegisterState({
email: e.target.value,
})
}
/>
</label>
<button
type="submit"
onClick={(e) => {
e.preventDefault();
console.log(postRegister());
}}
>
Zaloguj
</button>
</form>
);
};
return (
<>
<Nav isLogged={isLogged} logoutState={() => setIsLogged(false)} />
{isLogged ? <h1>Jesteś zalogowany</h1> : notLoggedForm()}
</>
);
}
ReactDOM.render(<Register />, document.getElementById("root"));

@ -23,4 +23,5 @@
border: none; border: none;
cursor: pointer; cursor: pointer;
margin: 0 15px; margin: 0 15px;
font-size: 0.9em;
} }

Loading…
Cancel
Save