Added authorization

This commit is contained in:
Dmitriy Shishkov 2020-11-29 09:12:02 +05:00
parent 7066d0f233
commit a0dbd1eca8
No known key found for this signature in database
GPG Key ID: D76D70029F55183E
10 changed files with 44 additions and 22 deletions

View File

@ -54,7 +54,7 @@ const Login: React.FC = () => {
return (
<form onSubmit={handleSubmit(resetError, setUserState, gotError, router)}>
<input type="email" name="email" id="email" />
<input type="username" name="username" id="username" />
<input type="password" name="password" id="password" autoComplete="on" />
<input type="submit" />
{error.has ? <ErrorMessage message={error.message} /> : ""}

View File

@ -1,15 +1,12 @@
import React from "react";
import { IRouteCard } from "types/user";
import { formatTimeLength } from "utils";
const RouteCard: React.FC<IRouteCard> = ({ route }) => {
return (
<div>
<h2>{route.name}</h2>
<p>
Данный квесты вы сможете пройти за {formatTimeLength(route.averageTime)}
</p>
<h3>{route.name}</h3>
<p>{route.description}</p>
<p>Длинна маршрута: {route.length} м</p>
<p>Примерное время прохождения: {route.averageTime}</p>
<p>Точка старта:</p>

View File

@ -7,7 +7,7 @@ import styles from "styles/RouteView.module.css";
const RouteView: React.FC<IRouteView> = ({ header, routes }) => {
return (
<>
<h1 className={styles.header}>{header}</h1>
<h2 className={styles.header}>{header}</h2>
{routes.map((route) => (
<RouteCard key={route.id} route={route} />
))}

22
front/pages/activate.tsx Normal file
View File

@ -0,0 +1,22 @@
import { request } from "utils/index";
import React, { useEffect } from "react";
import { useRouter } from "next/router";
import { redirect } from "next/dist/next-server/server/api-utils";
const Activate: React.FC = () => {
const router = useRouter();
useEffect(() => {
(async () => {
const res = await request(
`activate/${router.query.uidb}/${router.query.token}`,
"POST"
);
router.push("/");
})();
});
return null;
};
export default Activate;

View File

@ -1,6 +1,5 @@
import React, { useContext, useEffect, useState } from "react";
import HomeLayout from "layouts/HomeLayout";
import styles from "styles/home.module.css";
import { HomeRefContext } from "context/ref";
import Register from "components/Register";
@ -28,7 +27,7 @@ const advantagesList = [
},
];
const Home: React.FC & { Layout: React.ReactNode } = () => {
const Home: React.FC = () => {
const { setLoading } = useContext(LoadingContext);
const { userState } = useContext(UserContext);
const router = useRouter();
@ -37,7 +36,8 @@ const Home: React.FC & { Layout: React.ReactNode } = () => {
if (userState) {
router.push("/user");
return null;
} else setLoading(false);
}
else setLoading(false);
}, []);
const scrollRef = useContext(HomeRefContext);
@ -80,6 +80,4 @@ const Home: React.FC & { Layout: React.ReactNode } = () => {
);
};
Home.Layout = HomeLayout;
export default Home;

View File

@ -10,6 +10,8 @@ const routes: RouteT[] = [
name: "Пешком",
length: 100,
averageTime: 100,
description:
"Ipsum do irure ut excepteur reprehenderit nulla proident cupidatat ullamco officia pariatur enim consequat.",
startCoordinates: {
latitude: 60.977313,
longitude: 69.039326,

View File

View File

@ -1,5 +1,3 @@
.header {
border-radius: 30px;
background: #cff9ec;
text-align: center;
}

View File

@ -3,6 +3,7 @@ type FCL = React.FC & { Layout: React.ReactNode };
type RouteT = {
id: number;
name: string;
description: string;
length: number;
averageTime?: number;
userTime?: number;

View File

@ -2,24 +2,28 @@ const formatTimeLength = (minutes: number) =>
(Math.floor(minutes / 60) > 0 ? `${Math.floor(minutes / 60)} ч. ` : "") +
(minutes % 60 > 0 ? `${minutes % 60} мин.` : "");
const request = async (uri: string, method: "POST" | "GET", body = null) => {
const url = "http://localhost:4000/api/" + uri;
const request = async (uri: string, method: "POST" | "GET", body?) => {
const token = localStorage.getItem("token");
const url = "http://80.240.25.179/api/" + uri;
const headers = {
"Content-Type": "application/json",
method,
body: method == "POST" ? body : undefined,
Authorization: token != "" ? `Token ${token}` : undefined,
};
const options = {
headers,
body: method == "GET" ? body : undefined,
method,
body: JSON.stringify(body),
};
console.log(uri);
try {
if (process.env.NODE_ENV == "development") {
return JSON.parse(`{ "token": "fhjighdfjgjdfigbvhbsdfuyt47" }`);
}
// if (process.env.NODE_ENV == "development") {
// return JSON.parse(`{ "token": "fhjighdfjgjdfigbvhbsdfuyt47" }`);
// }
const res = await fetch(url, options);
return await res.json();
} catch (err) {