From 6c625f12734b99ccde5998b703d981e96d8c3796 Mon Sep 17 00:00:00 2001 From: sai80082 Date: Tue, 3 Sep 2024 12:29:49 +0530 Subject: [PATCH] feat: added logout and redirection to login after logout --- src/components/shared/ProfileInfo.tsx | 18 ++++++++++++- src/config/routes.ts | 1 + src/handlers/api/user.handler.ts | 6 ++++- src/handlers/serverUtils/user.utils.ts | 17 ++++++++++++ src/pages/api/users/logout.ts | 36 ++++++++++++++++++++++++++ 5 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 src/pages/api/users/logout.ts diff --git a/src/components/shared/ProfileInfo.tsx b/src/components/shared/ProfileInfo.tsx index 587d012..4627fa1 100644 --- a/src/components/shared/ProfileInfo.tsx +++ b/src/components/shared/ProfileInfo.tsx @@ -1,11 +1,14 @@ import { getMe } from "@/handlers/api/user.handler"; import { IUser } from "@/types/user"; import React, { useEffect, useState } from "react"; - +import { Button } from "@/components/ui/button" import Link from "next/link"; import { useConfig } from "@/contexts/ConfigContext"; +import { logoutUser } from "@/handlers/api/user.handler" +import { useRouter } from 'next/router'; export default function ProfileInfo() { + const router = useRouter(); const { immichURL,exImmichUrl } = useConfig(); const [user, setUser] = React.useState(null); const [loading, setLoading] = React.useState(true); @@ -19,6 +22,16 @@ export default function ProfileInfo() { }) .finally(() => setLoading(false)); }; + const handleLogout = () => { + + logoutUser().then((status) => { + console.log(status) + router.push('/login'); + }).catch((error) => { + setErrorMessage(error.message) + }) + + } useEffect(() => { fetchData(); @@ -39,6 +52,9 @@ export default function ProfileInfo() { {immichURL}

{user?.name}

+

diff --git a/src/config/routes.ts b/src/config/routes.ts index 2f55c49..4aa8025 100644 --- a/src/config/routes.ts +++ b/src/config/routes.ts @@ -3,6 +3,7 @@ export const BASE_PROXY_ENDPOINT = BASE_API_ENDPOINT + '/immich-proxy'; export const GET_ME_PATH = BASE_API_ENDPOINT + "/users/me"; export const LOGIN_PATH = BASE_API_ENDPOINT + "/users/login"; +export const LOGOUT_PATH = BASE_API_ENDPOINT + "/users/logout"; export const LIST_PEOPLE_PATH = BASE_API_ENDPOINT + "/people/list"; diff --git a/src/handlers/api/user.handler.ts b/src/handlers/api/user.handler.ts index 32c8c55..5aa974c 100644 --- a/src/handlers/api/user.handler.ts +++ b/src/handlers/api/user.handler.ts @@ -1,4 +1,4 @@ -import { GET_ME_PATH, LOGIN_PATH } from "@/config/routes"; +import { GET_ME_PATH, LOGIN_PATH, LOGOUT_PATH } from "@/config/routes"; import API from "@/lib/api"; export const getMe = async () => { @@ -7,4 +7,8 @@ export const getMe = async () => { export const loginUser = async (email: string, password: string) => { return API.post(LOGIN_PATH, { email, password }); +} + +export const logoutUser = async () => { + return API.post(LOGOUT_PATH); } \ No newline at end of file diff --git a/src/handlers/serverUtils/user.utils.ts b/src/handlers/serverUtils/user.utils.ts index 1a97283..a3c732b 100644 --- a/src/handlers/serverUtils/user.utils.ts +++ b/src/handlers/serverUtils/user.utils.ts @@ -63,5 +63,22 @@ export const loginUser = async (email: string, password: string) => { return res.json() } + return null +} + +export const logoutUser = async (Authorization: string) => { + const res = await fetch(ENV.IMMICH_URL + "/api/auth/logout", { + method: "POST", + headers: { + "Content-Type": "application/json", + "Authorization" : "Bearer " + Authorization + }, + + }) + + if (res.ok) { + return res.json() + } + return null } \ No newline at end of file diff --git a/src/pages/api/users/logout.ts b/src/pages/api/users/logout.ts new file mode 100644 index 0000000..ea04370 --- /dev/null +++ b/src/pages/api/users/logout.ts @@ -0,0 +1,36 @@ +// pages/api/proxy.js + +import { appConfig } from "@/config/app.config"; +import { getCurrentUser, logoutUser } from "@/handlers/serverUtils/user.utils"; +import { serializeCookie } from "@/lib/cookie"; +import { NextApiRequest, NextApiResponse } from "next"; + +export const config = { + api: { + bodyParser: true, + }, +}; + +export default async function handler( + req: NextApiRequest, + res: NextApiResponse +) { + if (req.method !== "POST") { + return res.status(405).json({ message: "Method Not Allowed" }); + } + + + try { + const user = await getCurrentUser(req); + const result = await logoutUser(user.accessToken as string); + if (result.successful) { + res.setHeader("Set-Cookie", serializeCookie(appConfig.sessionCookieName, "")); + return res.status(200).json({ message: "Logged out successfully" }); + } else { + return res.status(403).json({ message: "Invalid User Credentials" }); + } + } catch (error) { + console.error("Logout error:", error); + return res.status(500).json({ message: "Internal Server Error" }); + } +}