Skip to content

Commit 9797979

Browse files
committed
feat : chat ui and login , signup and list of friends
1 parent fcd7f93 commit 9797979

File tree

19 files changed

+1815
-2054
lines changed

19 files changed

+1815
-2054
lines changed

client/package-lock.json

Lines changed: 1492 additions & 2043 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"@testing-library/jest-dom": "^5.17.0",
77
"@testing-library/react": "^13.4.0",
88
"@testing-library/user-event": "^13.5.0",
9+
"axios": "^1.7.2",
910
"bootstrap": "^5.3.3",
1011
"react": "^18.3.1",
1112
"react-bootstrap": "^2.10.4",

client/src/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@ import './index.css';
55
import reportWebVitals from './reportWebVitals';
66
import Login from './pages/login';
77
import Signup from './pages/signup';
8+
import Chat from './pages/chat';
89
import { BrowserRouter, Routes, Route } from "react-router-dom";
910

1011
export default function App() {
1112
return (
1213
<BrowserRouter>
1314
<Routes>
1415
<Route path="/">
15-
<Route index element={<Login />} />
16+
<Route path='login' element={<Login />} />
1617
<Route path="signup" element={<Signup />} />
18+
<Route path="chat" element={<Chat />} />
19+
1720

1821
</Route>
1922
</Routes>

client/src/pages/chat.jsx

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
import React from 'react'
2+
import 'bootstrap/dist/css/bootstrap.min.css';
3+
import AuthGuard from '../services/AuthGuard';
4+
import { useEffect } from 'react';
5+
import axios from 'axios';
6+
import { useState } from 'react';
7+
8+
function Chat() {
9+
10+
const [users , setUsers] = useState([])
11+
12+
useEffect(() => {
13+
const fetchData = async () => {
14+
const accessToken = localStorage.user.access;
15+
console.log(accessToken);
16+
try {
17+
const response = await axios.get(
18+
'http://127.0.0.1:8000/api/users/',
19+
{},
20+
{
21+
headers: {
22+
Authorization: `Bearer ${accessToken}`,
23+
},
24+
}
25+
);
26+
setUsers(response.data)
27+
console.log("response is here?",response.data); // Handle the response data here
28+
} catch (error) {
29+
console.error('Error fetching data:', error);
30+
// Handle error
31+
}
32+
};
33+
34+
fetchData();
35+
}, []);
36+
37+
return (
38+
<div>
39+
<div class="container py-5">
40+
41+
<div class="row">
42+
43+
<div class="col-md-6 col-lg-5 col-xl-4 mb-4 mb-md-0">
44+
45+
<h5 class="font-weight-bold mb-3 text-center text-lg-start">Member</h5>
46+
47+
{users.map((user) => (
48+
<div className="card" key={user.id}>
49+
<div className="card-body">
50+
<ul className="list-unstyled mb-0">
51+
<li className="p-2 border-bottom bg-body-tertiary">
52+
<a href="#!" className="d-flex justify-content-between">
53+
<div className="d-flex flex-row">
54+
{/* <img
55+
src="https://mdbcdn.b-cdn.net/img/Photos/Avatars/avatar-8.webp"
56+
alt="avatar"
57+
className="rounded-circle d-flex align-self-center me-3 shadow-1-strong"
58+
width="60"
59+
/> */}
60+
<div className="pt-1">
61+
<p className="fw-bold mb-0">{user.username}</p>
62+
<p className="small text-muted">{user.email}</p>
63+
</div>
64+
</div>
65+
</a>
66+
</li>
67+
</ul>
68+
</div>
69+
</div>
70+
))}
71+
72+
73+
74+
</div>
75+
76+
<div class="col-md-6 col-lg-7 col-xl-8">
77+
78+
<ul class="list-unstyled">
79+
<li class="d-flex justify-content-between mb-4">
80+
<img src="https://mdbcdn.b-cdn.net/img/Photos/Avatars/avatar-6.webp" alt="avatar"
81+
class="rounded-circle d-flex align-self-start me-3 shadow-1-strong" width="60"/>
82+
<div class="card">
83+
<div class="card-header d-flex justify-content-between p-3">
84+
<p class="fw-bold mb-0">Brad Pitt</p>
85+
<p class="text-muted small mb-0"><i class="far fa-clock"></i> 12 mins ago</p>
86+
</div>
87+
<div class="card-body">
88+
<p class="mb-0">
89+
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
90+
labore et dolore magna aliqua.
91+
</p>
92+
</div>
93+
</div>
94+
</li>
95+
<li class="d-flex justify-content-between mb-4">
96+
<div class="card w-100">
97+
<div class="card-header d-flex justify-content-between p-3">
98+
<p class="fw-bold mb-0">Lara Croft</p>
99+
<p class="text-muted small mb-0"><i class="far fa-clock"></i> 13 mins ago</p>
100+
</div>
101+
<div class="card-body">
102+
<p class="mb-0">
103+
Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque
104+
laudantium.
105+
</p>
106+
</div>
107+
</div>
108+
<img src="https://mdbcdn.b-cdn.net/img/Photos/Avatars/avatar-5.webp" alt="avatar"
109+
class="rounded-circle d-flex align-self-start ms-3 shadow-1-strong" width="60"/>
110+
</li>
111+
<li class="d-flex justify-content-between mb-4">
112+
<img src="https://mdbcdn.b-cdn.net/img/Photos/Avatars/avatar-6.webp" alt="avatar"
113+
class="rounded-circle d-flex align-self-start me-3 shadow-1-strong" width="60"/>
114+
<div class="card">
115+
<div class="card-header d-flex justify-content-between p-3">
116+
<p class="fw-bold mb-0">Brad Pitt</p>
117+
<p class="text-muted small mb-0"><i class="far fa-clock"></i> 10 mins ago</p>
118+
</div>
119+
<div class="card-body">
120+
<p class="mb-0">
121+
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
122+
labore et dolore magna aliqua.
123+
</p>
124+
</div>
125+
</div>
126+
</li>
127+
<li class="bg-white mb-3">
128+
<div data-mdb-input-init class="form-outline">
129+
<textarea class="form-control bg-body-tertiary" id="textAreaExample2" rows="4"></textarea>
130+
<label class="form-label" for="textAreaExample2">Message</label>
131+
</div>
132+
</li>
133+
<button type="button" data-mdb-button-init data-mdb-ripple-init class="btn btn-info btn-rounded float-end">Send</button>
134+
</ul>
135+
136+
</div>
137+
138+
</div>
139+
140+
</div>
141+
</div>
142+
)
143+
}
144+
145+
export default AuthGuard(Chat)

client/src/pages/login.jsx

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
11
import 'bootstrap/dist/css/bootstrap.min.css';
2+
import axios from 'axios';
3+
import AuthService from '../services/auth.service'
4+
import { useState } from 'react';
5+
import { useNavigate } from "react-router-dom";
26
function Login() {
7+
const [username, setUsername] = useState("");
8+
const [password, setPassword] = useState("");
9+
const navigate = useNavigate();
10+
11+
const handleLogin = async (e) => {
12+
e.preventDefault();
13+
try {
14+
await AuthService.login(username, password).then(
15+
() => {
16+
console.log(localStorage.user);
17+
navigate("/chat");
18+
window.location.reload();
19+
},
20+
(error) => {
21+
console.log(error);
22+
}
23+
);
24+
} catch (err) {
25+
console.log(err);
26+
}
27+
};
28+
329
return (
430
<div className="App">
531
<section class="vh-100" styleclass="background-color: #eee;">
@@ -13,25 +39,25 @@ function Login() {
1339

1440
<p class="text-center h1 fw-bold mb-5 mx-1 mx-md-4 mt-4">Login</p>
1541

16-
<form class="mx-1 mx-md-4">
42+
<form class="mx-1 mx-md-4" onSubmit={handleLogin}>
1743

1844
<div class="d-flex flex-row align-items-center mb-4">
1945
<i class="fas fa-user fa-lg me-3 fa-fw"></i>
2046
<div data-mdb-input-init class="form-outline flex-fill mb-0">
21-
<input type="text" id="form3Example1c" class="form-control" />
47+
<input type="text" id="form3Example1c" class="form-control" onChange={(e) => setUsername(e.target.value)} />
2248
<label class="form-label" for="form3Example1c">Username</label>
2349
</div>
2450
</div>
2551

2652
<div class="d-flex flex-row align-items-center mb-4">
2753
<i class="fas fa-lock fa-lg me-3 fa-fw"></i>
2854
<div data-mdb-input-init class="form-outline flex-fill mb-0">
29-
<input type="password" id="form3Example4c" class="form-control" />
55+
<input type="password" id="form3Example4c" class="form-control" onChange={(e) => setPassword(e.target.value)} />
3056
<label class="form-label" for="form3Example4c">Password</label>
3157
</div>
3258
</div>
3359
<div class="d-flex justify-content-center mx-4 mb-3 mb-lg-4">
34-
<button type="button" data-mdb-button-init data-mdb-ripple-init class="btn btn-primary btn-lg">Register</button>
60+
<button type="submit" data-mdb-button-init data-mdb-ripple-init class="btn btn-primary btn-lg">Register</button>
3561
</div>
3662

3763
</form>

client/src/pages/signup.jsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
import 'bootstrap/dist/css/bootstrap.min.css';
22
function Signup() {
3+
4+
const user = JSON.parse(localStorage.getItem("user"));
5+
console.log(user)
6+
// if (user && user.accessToken) {
7+
// // return { Authorization: 'Bearer ' + user.accessToken };
8+
// return { "x-auth-token": user.accessToken };
9+
// } else {
10+
// return {};
11+
// }
12+
313
return (
414
<div className="Signup">
515
<section class="vh-100" styleclass="background-color: #eee;">

client/src/services/AuthGuard.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import React from 'react';
2+
import { Navigate } from 'react-router-dom';
3+
4+
const AuthGuard = (Component) => {
5+
return (props) => {
6+
const user = JSON.parse(localStorage.getItem('user'));
7+
if (!user || !user.access) {
8+
return <Navigate to="/login" />;
9+
}
10+
return <Component {...props} />;
11+
};
12+
};
13+
14+
export default AuthGuard;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import axios from "axios";
2+
3+
const API_URL = "http://127.0.0.1:8000/api";
4+
5+
const signup = (email, password) => {
6+
return axios
7+
.post(API_URL + "/signup", {
8+
email,
9+
password,
10+
})
11+
.then((response) => {
12+
if (response.data.accessToken) {
13+
localStorage.setItem("user", JSON.stringify(response.data));
14+
}
15+
16+
return response.data;
17+
});
18+
};
19+
20+
const login = (username, password) => {
21+
return axios
22+
.post(API_URL + "/login/", {
23+
username,
24+
password,
25+
})
26+
.then((response) => {
27+
if (response.data.access) {
28+
// console.log("Data is here?", response.data)
29+
30+
localStorage.setItem("user", JSON.stringify(response.data));
31+
}
32+
// console.log(response.data.access )
33+
return response.data;
34+
});
35+
};
36+
37+
const logout = () => {
38+
localStorage.removeItem("user");
39+
};
40+
41+
const getCurrentUser = () => {
42+
return JSON.parse(localStorage.getItem("user"));
43+
};
44+
45+
const authService = {
46+
signup,
47+
login,
48+
logout,
49+
getCurrentUser,
50+
};
51+
52+
export default authService;

server/db.sqlite3

12 KB
Binary file not shown.
234 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)