Advertisement
tosip

Untitled

Dec 14th, 2022
581
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Button from 'react-bootstrap/Button';
  2. import Form from 'react-bootstrap/Form';
  3. import React, { useState } from 'react';
  4. import LoginForm from '../components/LoginForm';
  5. import PocketBase from 'pocketbase';
  6.  
  7. const pb = new PocketBase('http://127.0.0.1:8090');
  8.  
  9. async function fetchData(email, pass) {
  10.   console.log(email, pass);
  11.   // or fetch only the first record that matches the specified filter
  12.   try {
  13.     const record = await pb
  14.       .collection('admins')
  15.       .getFirstListItem(`email="${email}" && password="${pass}"`, {
  16.         expand: 'relField1,relField2.subRelField',
  17.       });
  18.     if (record) {
  19.       console.log(record);
  20.       return record;
  21.     } else {
  22.       console.log('user not found');
  23.     }
  24.   } catch (error) {
  25.     console.log(error);
  26.   }
  27. }
  28.  
  29. function Login() {
  30.   const adminUser = {
  31.     email: '[email protected]',
  32.     password: '123456',
  33.   };
  34.   const [user, setUser] = useState({ email: '' });
  35.   const [error, setError] = useState('');
  36.  
  37.   const Login = (details) => {
  38.     console.log(details);
  39.     const isValid = fetchData(details.email, details.password);
  40.     console.log(isValid);
  41.     if (isValid.length > 0) {
  42.       console.log('Logged IN');
  43.       setUser({
  44.         email: details.email,
  45.       });
  46.     } else {
  47.       console.log('Details do not match');
  48.       setError('Details do not match');
  49.     }
  50.   };
  51.  
  52.   const Logout = () => {
  53.     console.log('logout');
  54.     setUser({ email: '' });
  55.   };
  56.  
  57.   return (
  58.     <div className='container-l'>
  59.       {user.email != '' ? (
  60.         <div>
  61.           <h2>Welcome {user.email}</h2>
  62.           <Button onClick={Logout}>Logout</Button>
  63.         </div>
  64.       ) : (
  65.         <LoginForm Login={Login} error={error} />
  66.       )}
  67.     </div>
  68.   );
  69. }
  70.  
  71. export default Login;
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement