[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";
|
---|
[13f1472] | 8 | import { GoogleLogin } from 'react-google-login';
|
---|
| 9 | import FacebookLogin from 'react-facebook-login';
|
---|
[d76b7ee] | 10 |
|
---|
| 11 | const setAuthCookie = (token) => {
|
---|
[a26f6a1] | 12 | localStorage.setItem('Auth','Bearer '+token)
|
---|
[d76b7ee] | 13 | }
|
---|
| 14 |
|
---|
| 15 | const Login = ({setUser}) => {
|
---|
| 16 | const history = useNavigate()
|
---|
| 17 |
|
---|
| 18 | const [loading, setLoading] = useState(false)
|
---|
| 19 |
|
---|
| 20 | const login = (attr) => {
|
---|
| 21 | setLoading(true)
|
---|
| 22 | axios.post(env.api+'Users/login',{email: attr.email, password: attr.password}).then(res => {
|
---|
| 23 | setAuthCookie(res.data.token)
|
---|
| 24 | console.log(res.data.token)
|
---|
[899b19d] | 25 | setUser(res.data)
|
---|
[d76b7ee] | 26 | setLoading(false)
|
---|
[899b19d] | 27 | history(res.data.isAdmin ? '/dashboard' : '/')
|
---|
[d76b7ee] | 28 | }).catch(el => {
|
---|
| 29 | Modal.error({
|
---|
| 30 | title: 'Погрешен мејл или лозинка',
|
---|
| 31 | });
|
---|
| 32 | setLoading(false)
|
---|
| 33 | })
|
---|
| 34 | }
|
---|
| 35 |
|
---|
[13f1472] | 36 | const googleLoginResponse = (response)=>{
|
---|
| 37 | console.log(response)
|
---|
| 38 | setLoading(true)
|
---|
| 39 | console.log(response.tokenId)
|
---|
| 40 | setAuthCookie('Bearer '+response.tokenId)
|
---|
| 41 | axios.post(env.api+'Users/register',{email: response.profileObj.email, password:'', isConfirmed: true}).then(res => {
|
---|
| 42 | setUser(res.data)
|
---|
| 43 | setLoading(false)
|
---|
| 44 | history(res.data.isAdmin ? '/dashboard' : '/')
|
---|
| 45 | })
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | const facebookLoginResponse = (response) =>{
|
---|
| 49 | console.log(response)
|
---|
| 50 | setLoading(true)
|
---|
| 51 | setAuthCookie('Bearer '+response.accessToken)
|
---|
| 52 | axios.post(env.api+'Users/register',{email: response.email, password:'', isConfirmed: true}).then(res => {
|
---|
| 53 | setUser(res.data)
|
---|
| 54 | setLoading(false)
|
---|
| 55 | history(res.data.isAdmin ? '/dashboard' : '/')
|
---|
| 56 | })
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[d76b7ee] | 59 | return(
|
---|
| 60 | <div style={{width:'100vw',height:'100vh', textAlign:'center',backgroundColor:'#F2F2F2'}}>
|
---|
| 61 | <div className='center'>
|
---|
| 62 | <Card style={{backgroundColor:'white', width:'100%', borderRadius:'20px'}} className='center'>
|
---|
| 63 | <h1>Најави се</h1>
|
---|
| 64 | <Form
|
---|
| 65 | name="normal_login"
|
---|
| 66 | onFinish={res=>login(res)}
|
---|
| 67 | >
|
---|
| 68 | <Form.Item
|
---|
| 69 | name="email"
|
---|
| 70 | rules={[
|
---|
| 71 | {
|
---|
| 72 | required: true,
|
---|
| 73 | message: 'Внесете мејл',
|
---|
| 74 | },
|
---|
| 75 | {
|
---|
| 76 | type: 'email',
|
---|
| 77 | message: 'Внесете валиден мејл',
|
---|
| 78 | },
|
---|
| 79 | ]}
|
---|
| 80 | >
|
---|
| 81 | <Input prefix={<UserOutlined className="site-form-item-icon" />} placeholder="Email" />
|
---|
| 82 | </Form.Item>
|
---|
| 83 | <Form.Item
|
---|
| 84 | name="password"
|
---|
| 85 | rules={[
|
---|
| 86 | {
|
---|
| 87 | required: true,
|
---|
| 88 | message: 'Внесете лозинка!',
|
---|
| 89 | }
|
---|
| 90 | ]}
|
---|
| 91 | >
|
---|
| 92 | <Input
|
---|
| 93 | prefix={<LockOutlined className="site-form-item-icon" />}
|
---|
| 94 | type="password"
|
---|
| 95 | placeholder="Password"
|
---|
| 96 | />
|
---|
| 97 | </Form.Item>
|
---|
| 98 |
|
---|
| 99 | <Form.Item style={{margin:'0px'}}>
|
---|
[13f1472] | 100 | <Button type="primary" htmlType="submit" className="login-form-button" loading={loading} style={{marginBottom:'5px'}}>
|
---|
[d76b7ee] | 101 | Log in
|
---|
| 102 | </Button>
|
---|
[13f1472] | 103 | <br/>
|
---|
| 104 | <GoogleLogin
|
---|
| 105 | clientId="748923557453-ljii8ulhti93man769d2dd9soi7038j5.apps.googleusercontent.com"
|
---|
| 106 | buttonText="Логирај се со Google"
|
---|
| 107 | onSuccess={googleLoginResponse}
|
---|
| 108 | />
|
---|
| 109 | <FacebookLogin
|
---|
| 110 | appId="1455131645009417"
|
---|
| 111 | size={'small'}
|
---|
| 112 | textButton={'Логирај се со FB'}
|
---|
| 113 | icon="fa-facebook"
|
---|
| 114 | fields="name,email"
|
---|
| 115 | callback={facebookLoginResponse} />
|
---|
[d76b7ee] | 116 | <div>
|
---|
| 117 | Или <a href={"/register"}>регистрирај се!</a>
|
---|
| 118 | </div>
|
---|
[49b0bbd] | 119 | <div>
|
---|
| 120 | <a href={"/reset-password"}>Заборави лозинка?</a>
|
---|
| 121 | </div>
|
---|
[d76b7ee] | 122 | </Form.Item>
|
---|
| 123 | </Form>
|
---|
| 124 | </Card>
|
---|
| 125 | </div>
|
---|
| 126 | </div>
|
---|
| 127 | )
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | const Register = ({setUser}) => {
|
---|
| 131 |
|
---|
| 132 | const [loading, setLoading] = useState(false)
|
---|
| 133 | const history = useNavigate()
|
---|
| 134 | const register = (attr) => {
|
---|
| 135 | if (attr.password !== attr.confirm) {
|
---|
| 136 | Modal.error({
|
---|
| 137 | title: 'Лозинките не се исти',
|
---|
| 138 | });
|
---|
| 139 | return;
|
---|
| 140 | }
|
---|
| 141 | setLoading(true)
|
---|
[13f1472] | 142 | axios.post(env.api+'Users/register',{email: attr.email, password: attr.password,isConfirmed:false}).then(res => {
|
---|
[d76b7ee] | 143 | setAuthCookie(res.data.token)
|
---|
[899b19d] | 144 | setUser(res.data)
|
---|
[d76b7ee] | 145 | setLoading(false)
|
---|
[a26f6a1] | 146 | history('/confirm-email')
|
---|
[d76b7ee] | 147 | })
|
---|
| 148 | }
|
---|
[13f1472] | 149 |
|
---|
| 150 | const googleLoginResponse = (response)=>{
|
---|
| 151 | console.log(response)
|
---|
| 152 | setLoading(true)
|
---|
| 153 | console.log(response.tokenId)
|
---|
| 154 | setAuthCookie('Bearer '+response.tokenId)
|
---|
| 155 | axios.post(env.api+'Users/register',{email: response.profileObj.email, password:'', isConfirmed: true}).then(res => {
|
---|
| 156 | setUser(res.data)
|
---|
| 157 | setLoading(false)
|
---|
| 158 | history('/')
|
---|
| 159 | })
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | const facebookLoginResponse = (response) =>{
|
---|
| 163 | console.log(response)
|
---|
| 164 | setLoading(true)
|
---|
| 165 | setAuthCookie('Bearer '+response.accessToken)
|
---|
| 166 | axios.post(env.api+'Users/register',{email: response.email, password:'', isConfirmed: true}).then(res => {
|
---|
| 167 | setUser(res.data)
|
---|
| 168 | setLoading(false)
|
---|
| 169 | history('/')
|
---|
| 170 | })
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 |
|
---|
[d76b7ee] | 174 | return(
|
---|
| 175 | <div style={{width:'100vw',height:'100vh', textAlign:'center', backgroundColor:'#F2F2F2'}}>
|
---|
| 176 | <div className='center'>
|
---|
| 177 | <Card style={{backgroundColor:'white', width:'100%', borderRadius:'20px'}} className='center'>
|
---|
| 178 | <h1>Регистрирај се</h1>
|
---|
| 179 | <Form
|
---|
| 180 | name="normal_login"
|
---|
| 181 | onFinish={(attr)=>register(attr)}
|
---|
| 182 | >
|
---|
| 183 | <Form.Item
|
---|
| 184 | name="email"
|
---|
| 185 | rules={[
|
---|
| 186 | {
|
---|
| 187 | required: true,
|
---|
| 188 | message: 'Внесете мејл',
|
---|
| 189 | },
|
---|
| 190 | {
|
---|
| 191 | type: 'email',
|
---|
| 192 | message: 'Внесете валиден мејл',
|
---|
| 193 | },
|
---|
| 194 | ]}
|
---|
| 195 | >
|
---|
| 196 | <Input prefix={<UserOutlined className="site-form-item-icon" />} placeholder="Email" />
|
---|
| 197 | </Form.Item>
|
---|
| 198 | <Form.Item
|
---|
| 199 | name="password"
|
---|
| 200 | rules={[
|
---|
| 201 | {
|
---|
| 202 | required: true,
|
---|
| 203 | message: 'Внесете лозинка!',
|
---|
| 204 | },
|
---|
| 205 | ]}
|
---|
| 206 | >
|
---|
| 207 | <Input
|
---|
| 208 | prefix={<LockOutlined className="site-form-item-icon" />}
|
---|
| 209 | type="password"
|
---|
| 210 | placeholder="Password"
|
---|
| 211 | />
|
---|
| 212 | </Form.Item>
|
---|
| 213 | <Form.Item
|
---|
| 214 | name="confirm"
|
---|
| 215 | rules={[
|
---|
| 216 | {
|
---|
| 217 | required: true,
|
---|
| 218 | message: 'Потврдете лозинка!',
|
---|
| 219 | },
|
---|
| 220 | ]}
|
---|
| 221 | >
|
---|
| 222 | <Input
|
---|
| 223 | prefix={<LockOutlined className="site-form-item-icon" />}
|
---|
| 224 | type="password"
|
---|
| 225 | placeholder="Confirm password"
|
---|
| 226 | />
|
---|
| 227 | </Form.Item>
|
---|
| 228 |
|
---|
| 229 | <Form.Item style={{margin:'0px'}}>
|
---|
[13f1472] | 230 | <Button type="primary" htmlType="submit" className="login-form-button" loading={loading} style={{marginBottom:'5px'}}>
|
---|
[d76b7ee] | 231 | Register
|
---|
| 232 | </Button>
|
---|
[13f1472] | 233 | <br/>
|
---|
| 234 | <GoogleLogin
|
---|
| 235 | clientId="748923557453-ljii8ulhti93man769d2dd9soi7038j5.apps.googleusercontent.com"
|
---|
| 236 | buttonText="Регистрирај се со Google"
|
---|
| 237 | onSuccess={googleLoginResponse}
|
---|
| 238 | />
|
---|
| 239 | <div style={{margin:'5px'}}/>
|
---|
| 240 | <FacebookLogin
|
---|
| 241 | appId="1455131645009417"
|
---|
| 242 | size={'small'}
|
---|
| 243 | textButton={'Регистрирај се со FB'}
|
---|
| 244 | icon="fa-facebook"
|
---|
| 245 | fields="name,email"
|
---|
| 246 | callback={facebookLoginResponse} />
|
---|
[d76b7ee] | 247 | <div>
|
---|
| 248 | Или <a href={"/login"}>најави се!</a>
|
---|
| 249 | </div>
|
---|
| 250 | </Form.Item>
|
---|
| 251 | </Form>
|
---|
| 252 | </Card>
|
---|
| 253 | </div>
|
---|
| 254 | </div>
|
---|
| 255 | )
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | export {Login,Register}; |
---|