mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
feat: add logout feature in dashboard
This commit is contained in:
parent
6aa849b305
commit
259007afa5
2 changed files with 44 additions and 10 deletions
|
|
@ -3,16 +3,19 @@ import { useOrg } from '@components/Contexts/OrgContext';
|
||||||
import { useSession } from '@components/Contexts/SessionContext';
|
import { useSession } from '@components/Contexts/SessionContext';
|
||||||
import ToolTip from '@components/StyledElements/Tooltip/Tooltip'
|
import ToolTip from '@components/StyledElements/Tooltip/Tooltip'
|
||||||
import LearnHouseDashboardLogo from '@public/dashLogo.png';
|
import LearnHouseDashboardLogo from '@public/dashLogo.png';
|
||||||
|
import { logout } from '@services/auth/auth';
|
||||||
import Avvvatars from 'avvvatars-react';
|
import Avvvatars from 'avvvatars-react';
|
||||||
import { ArrowLeft, Book, BookCopy, Home, School, Settings } from 'lucide-react'
|
import { ArrowLeft, Book, BookCopy, Home, LogOut, School, Settings } from 'lucide-react'
|
||||||
import Image from 'next/image';
|
import Image from 'next/image';
|
||||||
import Link from 'next/link'
|
import Link from 'next/link'
|
||||||
|
import { useRouter } from 'next/navigation';
|
||||||
import React, { use, useEffect } from 'react'
|
import React, { use, useEffect } from 'react'
|
||||||
|
|
||||||
function LeftMenu() {
|
function LeftMenu() {
|
||||||
const org = useOrg() as any;
|
const org = useOrg() as any;
|
||||||
const session = useSession() as any;
|
const session = useSession() as any;
|
||||||
const [loading, setLoading] = React.useState(true);
|
const [loading, setLoading] = React.useState(true);
|
||||||
|
const route = useRouter();
|
||||||
|
|
||||||
function waitForEverythingToLoad() {
|
function waitForEverythingToLoad() {
|
||||||
if (org && session) {
|
if (org && session) {
|
||||||
|
|
@ -21,6 +24,14 @@ function LeftMenu() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function logOutUI() {
|
||||||
|
const res = await logout();
|
||||||
|
if (res) {
|
||||||
|
route.push('/login');
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (waitForEverythingToLoad()) {
|
if (waitForEverythingToLoad()) {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
|
|
@ -59,20 +70,23 @@ function LeftMenu() {
|
||||||
</ToolTip>
|
</ToolTip>
|
||||||
</div>
|
</div>
|
||||||
<div className='flex flex-col mx-auto pb-7 space-y-2'>
|
<div className='flex flex-col mx-auto pb-7 space-y-2'>
|
||||||
<ToolTip content={session.user.username + "'s Settings"} slateBlack sideOffset={8} side='right' >
|
|
||||||
<Link href={'/dash/user/settings/general'} className='py-3'>
|
<div className="flex items-center flex-col space-y-2">
|
||||||
<Settings className='mx-auto text-neutral-400 cursor-pointer' size={18} />
|
|
||||||
</Link>
|
|
||||||
</ToolTip>
|
|
||||||
<div className="flex items-center flex-col space-y-4">
|
|
||||||
<ToolTip content={session.user.username} slateBlack sideOffset={8} side='right' >
|
<ToolTip content={session.user.username} slateBlack sideOffset={8} side='right' >
|
||||||
<div className="mx-auto shadow-lg">
|
<div className="mx-auto shadow-lg">
|
||||||
<Avvvatars radius={3} border borderColor='white' borderSize={3} size={35} value={session.user.user_uuid} style="shape" />
|
<Avvvatars radius={3} border borderColor='white' borderSize={3} size={35} value={session.user.user_uuid} style="shape" />
|
||||||
</div>
|
</div>
|
||||||
</ToolTip>
|
</ToolTip>
|
||||||
<ToolTip content={'Learnhouse Version'} slateBlack sideOffset={8} side='right' >
|
<div className='flex items-center flex-col space-y-1'>
|
||||||
<div className='py-1 px-3 bg-white/10 opacity-40 rounded-full text-[10px] uppercase justify-center text-center'>alpha</div>
|
<ToolTip content={session.user.username + "'s Settings"} slateBlack sideOffset={8} side='right' >
|
||||||
</ToolTip>
|
<Link href={'/dash/user/settings/general'} className='py-3'>
|
||||||
|
<Settings className='mx-auto text-neutral-400 cursor-pointer' size={18} />
|
||||||
|
</Link>
|
||||||
|
</ToolTip>
|
||||||
|
<ToolTip content={'Logout'} slateBlack sideOffset={8} side='right' >
|
||||||
|
<LogOut onClick={() => logOutUI()} className='mx-auto text-neutral-400 cursor-pointer' size={14} />
|
||||||
|
</ToolTip>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,26 @@ export async function loginAndGetToken(username: string, password: string): Prom
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function logout(): Promise<any> {
|
||||||
|
// Request Config
|
||||||
|
|
||||||
|
// get origin
|
||||||
|
const HeadersConfig = new Headers({ "Content-Type": "application/x-www-form-urlencoded" });
|
||||||
|
const urlencoded = new URLSearchParams();
|
||||||
|
|
||||||
|
const requestOptions: any = {
|
||||||
|
method: "DELETE",
|
||||||
|
headers: HeadersConfig,
|
||||||
|
body: urlencoded,
|
||||||
|
redirect: "follow",
|
||||||
|
credentials: "include",
|
||||||
|
};
|
||||||
|
|
||||||
|
// fetch using await and async
|
||||||
|
const response = await fetch(`${getAPIUrl()}auth/logout`, requestOptions);
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
export async function getUserInfo(token: string): Promise<any> {
|
export async function getUserInfo(token: string): Promise<any> {
|
||||||
const origin = window.location.origin;
|
const origin = window.location.origin;
|
||||||
const HeadersConfig = new Headers({ Authorization: `Bearer ${token}`, Origin: origin });
|
const HeadersConfig = new Headers({ Authorization: `Bearer ${token}`, Origin: origin });
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue