[d76b7ee] | 1 | import React, {useState} from 'react'
|
---|
| 2 | import {useNavigate} from "react-router-dom";
|
---|
| 3 | import {Button, Card, Form, Input, Modal, notification} from "antd";
|
---|
| 4 | import { UserOutlined, LockOutlined } from '@ant-design/icons';
|
---|
| 5 | import axios from "axios";
|
---|
[a26f6a1] | 6 | import '../App.css'
|
---|
| 7 | import env from "../env";
|
---|
[d76b7ee] | 8 |
|
---|
| 9 | const setAuthCookie = (token) => {
|
---|
[a26f6a1] | 10 | localStorage.setItem('Auth','Bearer '+token)
|
---|
[d76b7ee] | 11 | }
|
---|
| 12 |
|
---|
| 13 | const 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)
|
---|
[899b19d] | 23 | setUser(res.data)
|
---|
[d76b7ee] | 24 | setLoading(false)
|
---|
[899b19d] | 25 | history(res.data.isAdmin ? '/dashboard' : '/')
|
---|
[d76b7ee] | 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 | </Form.Item>
|
---|
| 82 | </Form>
|
---|
| 83 | </Card>
|
---|
| 84 | </div>
|
---|
| 85 | </div>
|
---|
| 86 | )
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | const Register = ({setUser}) => {
|
---|
| 90 |
|
---|
| 91 | const [loading, setLoading] = useState(false)
|
---|
| 92 | const history = useNavigate()
|
---|
| 93 | const register = (attr) => {
|
---|
| 94 | if (attr.password !== attr.confirm) {
|
---|
| 95 | Modal.error({
|
---|
| 96 | title: 'Лозинките не се исти',
|
---|
| 97 | });
|
---|
| 98 | return;
|
---|
| 99 | }
|
---|
| 100 | setLoading(true)
|
---|
| 101 | axios.post(env.api+'Users/register',{email: attr.email, password: attr.password}).then(res => {
|
---|
| 102 | setAuthCookie(res.data.token)
|
---|
[899b19d] | 103 | setUser(res.data)
|
---|
[d76b7ee] | 104 | setLoading(false)
|
---|
[a26f6a1] | 105 | history('/confirm-email')
|
---|
[d76b7ee] | 106 | })
|
---|
| 107 | }
|
---|
| 108 | return(
|
---|
| 109 | <div style={{width:'100vw',height:'100vh', textAlign:'center', backgroundColor:'#F2F2F2'}}>
|
---|
| 110 | <div className='center'>
|
---|
| 111 | <Card style={{backgroundColor:'white', width:'100%', borderRadius:'20px'}} className='center'>
|
---|
| 112 | <h1>Регистрирај се</h1>
|
---|
| 113 | <Form
|
---|
| 114 | name="normal_login"
|
---|
| 115 | onFinish={(attr)=>register(attr)}
|
---|
| 116 | >
|
---|
| 117 | <Form.Item
|
---|
| 118 | name="email"
|
---|
| 119 | rules={[
|
---|
| 120 | {
|
---|
| 121 | required: true,
|
---|
| 122 | message: 'Внесете мејл',
|
---|
| 123 | },
|
---|
| 124 | {
|
---|
| 125 | type: 'email',
|
---|
| 126 | message: 'Внесете валиден мејл',
|
---|
| 127 | },
|
---|
| 128 | ]}
|
---|
| 129 | >
|
---|
| 130 | <Input prefix={<UserOutlined className="site-form-item-icon" />} placeholder="Email" />
|
---|
| 131 | </Form.Item>
|
---|
| 132 | <Form.Item
|
---|
| 133 | name="password"
|
---|
| 134 | rules={[
|
---|
| 135 | {
|
---|
| 136 | required: true,
|
---|
| 137 | message: 'Внесете лозинка!',
|
---|
| 138 | },
|
---|
| 139 | ]}
|
---|
| 140 | >
|
---|
| 141 | <Input
|
---|
| 142 | prefix={<LockOutlined className="site-form-item-icon" />}
|
---|
| 143 | type="password"
|
---|
| 144 | placeholder="Password"
|
---|
| 145 | />
|
---|
| 146 | </Form.Item>
|
---|
| 147 | <Form.Item
|
---|
| 148 | name="confirm"
|
---|
| 149 | rules={[
|
---|
| 150 | {
|
---|
| 151 | required: true,
|
---|
| 152 | message: 'Потврдете лозинка!',
|
---|
| 153 | },
|
---|
| 154 | ]}
|
---|
| 155 | >
|
---|
| 156 | <Input
|
---|
| 157 | prefix={<LockOutlined className="site-form-item-icon" />}
|
---|
| 158 | type="password"
|
---|
| 159 | placeholder="Confirm password"
|
---|
| 160 | />
|
---|
| 161 | </Form.Item>
|
---|
| 162 |
|
---|
| 163 | <Form.Item style={{margin:'0px'}}>
|
---|
| 164 | <Button type="primary" htmlType="submit" className="login-form-button" loading={loading}>
|
---|
| 165 | Register
|
---|
| 166 | </Button>
|
---|
| 167 | <div>
|
---|
| 168 | Или <a href={"/login"}>најави се!</a>
|
---|
| 169 | </div>
|
---|
| 170 | </Form.Item>
|
---|
| 171 | </Form>
|
---|
| 172 | </Card>
|
---|
| 173 | </div>
|
---|
| 174 | </div>
|
---|
| 175 | )
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | export {Login,Register}; |
---|