source: frontend/src/Auth/auth.js@ 49b0bbd

Last change on this file since 49b0bbd was 49b0bbd, checked in by Danilo <danilo.najkov@…>, 21 months ago

minor fix

  • Property mode set to 100644
File size: 7.1 KB
Line 
1import React, {useState} from 'react'
2import {useNavigate} from "react-router-dom";
3import {Button, Card, Form, Input, Modal, notification} from "antd";
4import { UserOutlined, LockOutlined } from '@ant-design/icons';
5import axios from "axios";
6import '../App.css'
7import env from "../env";
8
9const setAuthCookie = (token) => {
10 localStorage.setItem('Auth','Bearer '+token)
11}
12
13const Login = ({setUser}) => {
14 const history = useNavigate()
15
16 const [loading, setLoading] = useState(false)
17
18 const login = (attr) => {
19 setLoading(true)
20 axios.post(env.api+'Users/login',{email: attr.email, password: attr.password}).then(res => {
21 setAuthCookie(res.data.token)
22 console.log(res.data.token)
23 setUser(res.data)
24 setLoading(false)
25 history(res.data.isAdmin ? '/dashboard' : '/')
26 }).catch(el => {
27 Modal.error({
28 title: 'Погрешен мејл или лозинка',
29 });
30 setLoading(false)
31 })
32 }
33
34 return(
35 <div style={{width:'100vw',height:'100vh', textAlign:'center',backgroundColor:'#F2F2F2'}}>
36 <div className='center'>
37 <Card style={{backgroundColor:'white', width:'100%', borderRadius:'20px'}} className='center'>
38 <h1>Најави се</h1>
39 <Form
40 name="normal_login"
41 onFinish={res=>login(res)}
42 >
43 <Form.Item
44 name="email"
45 rules={[
46 {
47 required: true,
48 message: 'Внесете мејл',
49 },
50 {
51 type: 'email',
52 message: 'Внесете валиден мејл',
53 },
54 ]}
55 >
56 <Input prefix={<UserOutlined className="site-form-item-icon" />} placeholder="Email" />
57 </Form.Item>
58 <Form.Item
59 name="password"
60 rules={[
61 {
62 required: true,
63 message: 'Внесете лозинка!',
64 }
65 ]}
66 >
67 <Input
68 prefix={<LockOutlined className="site-form-item-icon" />}
69 type="password"
70 placeholder="Password"
71 />
72 </Form.Item>
73
74 <Form.Item style={{margin:'0px'}}>
75 <Button type="primary" htmlType="submit" className="login-form-button" loading={loading}>
76 Log in
77 </Button>
78 <div>
79 Или <a href={"/register"}>регистрирај се!</a>
80 </div>
81 <div>
82 <a href={"/reset-password"}>Заборави лозинка?</a>
83 </div>
84 </Form.Item>
85 </Form>
86 </Card>
87 </div>
88 </div>
89 )
90}
91
92const Register = ({setUser}) => {
93
94 const [loading, setLoading] = useState(false)
95 const history = useNavigate()
96 const register = (attr) => {
97 if (attr.password !== attr.confirm) {
98 Modal.error({
99 title: 'Лозинките не се исти',
100 });
101 return;
102 }
103 setLoading(true)
104 axios.post(env.api+'Users/register',{email: attr.email, password: attr.password}).then(res => {
105 setAuthCookie(res.data.token)
106 setUser(res.data)
107 setLoading(false)
108 history('/confirm-email')
109 })
110 }
111 return(
112 <div style={{width:'100vw',height:'100vh', textAlign:'center', backgroundColor:'#F2F2F2'}}>
113 <div className='center'>
114 <Card style={{backgroundColor:'white', width:'100%', borderRadius:'20px'}} className='center'>
115 <h1>Регистрирај се</h1>
116 <Form
117 name="normal_login"
118 onFinish={(attr)=>register(attr)}
119 >
120 <Form.Item
121 name="email"
122 rules={[
123 {
124 required: true,
125 message: 'Внесете мејл',
126 },
127 {
128 type: 'email',
129 message: 'Внесете валиден мејл',
130 },
131 ]}
132 >
133 <Input prefix={<UserOutlined className="site-form-item-icon" />} placeholder="Email" />
134 </Form.Item>
135 <Form.Item
136 name="password"
137 rules={[
138 {
139 required: true,
140 message: 'Внесете лозинка!',
141 },
142 ]}
143 >
144 <Input
145 prefix={<LockOutlined className="site-form-item-icon" />}
146 type="password"
147 placeholder="Password"
148 />
149 </Form.Item>
150 <Form.Item
151 name="confirm"
152 rules={[
153 {
154 required: true,
155 message: 'Потврдете лозинка!',
156 },
157 ]}
158 >
159 <Input
160 prefix={<LockOutlined className="site-form-item-icon" />}
161 type="password"
162 placeholder="Confirm password"
163 />
164 </Form.Item>
165
166 <Form.Item style={{margin:'0px'}}>
167 <Button type="primary" htmlType="submit" className="login-form-button" loading={loading}>
168 Register
169 </Button>
170 <div>
171 Или <a href={"/login"}>најави се!</a>
172 </div>
173 </Form.Item>
174 </Form>
175 </Card>
176 </div>
177 </div>
178 )
179}
180
181export {Login,Register};
Note: See TracBrowser for help on using the repository browser.