Skip to content

Commit 49f5683

Browse files
committed
feat: Add logout and observe current user.
1 parent b612a46 commit 49f5683

2 files changed

Lines changed: 41 additions & 4 deletions

File tree

src/components/Layout/SettingsLayout/SettingsLayout.tsx

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,48 @@
1-
import React from 'react'
1+
import { User } from 'firebase/auth'
2+
import React, { useEffect, useState } from 'react'
23
import 'react-contexify/dist/ReactContexify.css'
34
import { NavLink, Outlet } from 'react-router-dom'
5+
import toast from 'react-simple-toasts'
46
import { auth } from 'src/features/auth'
57
import './settings.css'
68

7-
const UserInfo = () => {
8-
const userInfo = auth.currentUser
9+
interface UserInfoProps {
10+
userInfo: User // Define the type of userInfo
11+
}
12+
13+
const UserInfo = ({ userInfo }: UserInfoProps) => {
14+
const logout = () => {
15+
auth
16+
.signOut()
17+
.then((result) => {
18+
toast('Account logged out successfuly', { theme: 'defaultToast' })
19+
})
20+
.catch((error) => {
21+
toast('Failed to logged out, please try later !!', { theme: 'failure' })
22+
})
23+
}
924
return (
1025
<div className="userCard">
1126
{userInfo?.photoURL && <img src={userInfo.photoURL} className="userImage"></img>}
1227
<div className="userInfos">
1328
<div className="userName">{userInfo?.displayName}</div>
1429
<div className="userEmail">{userInfo?.email}</div>
1530
</div>
31+
<button className="logoutBtn" onClick={logout}>
32+
Logout
33+
</button>
1634
</div>
1735
)
1836
}
1937

2038
export const SettingsLayout = () => {
39+
const [user, setUser] = useState<User | null>(null)
40+
useEffect(() => {
41+
const unsubscribe = auth.onAuthStateChanged((user) => {
42+
setUser(user)
43+
})
44+
return () => unsubscribe()
45+
})
2146
const navigation = [
2247
{
2348
name: 'Topics',
@@ -42,7 +67,7 @@ export const SettingsLayout = () => {
4267
]
4368
return (
4469
<div className="settings">
45-
<UserInfo />
70+
{user != null && <UserInfo userInfo={user} />}
4671
<div className="horizontalTabsLayout">
4772
<nav className="navigation">
4873
{navigation.map((item) => {

src/components/Layout/SettingsLayout/settings.css

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@
6666
font-size: medium;
6767
margin-top: 6px;
6868
}
69+
.logoutBtn {
70+
font-size: medium;
71+
padding: 8px 20px;
72+
background-color: #e57373;
73+
color: var(--button-text-color);
74+
border: 0;
75+
border-radius: 20px;
76+
font-weight: bold;
77+
font-size: 16px;
78+
text-align: center;
79+
margin-left: auto;
80+
}
6981

7082
@media only screen and (max-width: 768px) {
7183
.horizontalTabsLayout {

0 commit comments

Comments
 (0)