diff --git a/src/components/auth/LoginForm.tsx b/src/components/auth/LoginForm.tsx index f7b5329..43ee12e 100644 --- a/src/components/auth/LoginForm.tsx +++ b/src/components/auth/LoginForm.tsx @@ -13,11 +13,18 @@ import { Label } from "@/components/ui/label" import { useState } from "react" import { loginUser } from "@/handlers/api/user.handler" import { useConfig } from "@/contexts/ConfigContext" +import { IUser } from "@/types/user" +import Head from "next/head" export const description = "A login form with email and password. There's an option to login with Google and a link to sign up if you don't have an account." -export function LoginForm() { +interface ILoginFormProps { + onLogin: (user: IUser) => void +} +export function LoginForm( + { onLogin }: ILoginFormProps +) { const { exImmichUrl } = useConfig() const [formData, setFormData] = useState({ email: "", @@ -34,9 +41,9 @@ export function LoginForm() { const handleSubmit = (e: React.FormEvent) => { e.preventDefault() setLoading(true) - loginUser(formData.email, formData.password).then((user) => { - console.log(user) - }).catch((error) => { + loginUser(formData.email, formData.password) + .then(onLogin) + .catch((error) => { setErrorMessage(error.message) }).finally(() => { setLoading(false) @@ -45,49 +52,54 @@ export function LoginForm() { return ( -
- - - Immich Power Tools - Login to Immich - - Login to your connected Immich instance ({exImmichUrl}) - - - -
-
- - -
-
- - -
- - -
- -
-
-
+ <> + + Immich Power Tools - Login + +
+ + + Immich Power Tools + Login to Immich + + Login to your connected Immich instance ({exImmichUrl}) + + + +
+
+ + +
+
+ + +
+ + +
+ +
+
+
+ ) } diff --git a/src/components/layouts/RootLayout.tsx b/src/components/layouts/RootLayout.tsx index 0055795..b03ed48 100644 --- a/src/components/layouts/RootLayout.tsx +++ b/src/components/layouts/RootLayout.tsx @@ -32,13 +32,16 @@ export default function RootLayout({ children }: RootLayoutProps) { .finally(() => setLoading(false)); }; + const handleLoginCompletion = (user: IUser) => { + setUser(user); + } + useEffect(() => { fetchData(); }, []); if (loading) return ; if (errorMessage) - return (
); - if (!user) return ; + if (!user) return ; return ( - +
{children}
diff --git a/src/components/shared/ProfileInfo.tsx b/src/components/shared/ProfileInfo.tsx index 4627fa1..055babd 100644 --- a/src/components/shared/ProfileInfo.tsx +++ b/src/components/shared/ProfileInfo.tsx @@ -1,44 +1,26 @@ -import { getMe } from "@/handlers/api/user.handler"; -import { IUser } from "@/types/user"; -import React, { useEffect, useState } from "react"; +import React 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'; +import { useCurrentUser } from "@/contexts/CurrentUserContext"; +import { useToast } from "../ui/use-toast"; export default function ProfileInfo() { - const router = useRouter(); - const { immichURL,exImmichUrl } = useConfig(); - const [user, setUser] = React.useState(null); - const [loading, setLoading] = React.useState(true); - const [errorMessage, setErrorMessage] = useState(null); + const { updateContext, ...user } = useCurrentUser() + const { immichURL, exImmichUrl } = useConfig(); + const toast = useToast(); - const fetchData = () => { - return getMe() - .then(setUser) - .catch((error) => { - setErrorMessage(error.message); - }) - .finally(() => setLoading(false)); - }; - const handleLogout = () => { - - logoutUser().then((status) => { - console.log(status) - router.push('/login'); + const handleLogout = () => logoutUser() + .then(() => { + updateContext(null) }).catch((error) => { - setErrorMessage(error.message) + toast.toast({ + title: "Error", + description: error.message, + }) }) - } - - useEffect(() => { - fetchData(); - }, []); - - if (loading) return
Loading...
; - if (errorMessage) return
{errorMessage}
; return ( <> @@ -51,10 +33,14 @@ export default function ProfileInfo() { {immichURL} -

{user?.name}

- + {user && ( + <> +

{user?.name}

+ + + )}

diff --git a/src/components/shared/Sidebar.tsx b/src/components/shared/Sidebar.tsx index 458f8c1..e3ef727 100644 --- a/src/components/shared/Sidebar.tsx +++ b/src/components/shared/Sidebar.tsx @@ -53,7 +53,6 @@ export default function Sidebar() {

-
diff --git a/src/contexts/CurrentUserContext.tsx b/src/contexts/CurrentUserContext.tsx index 8e74e30..a81bf4e 100644 --- a/src/contexts/CurrentUserContext.tsx +++ b/src/contexts/CurrentUserContext.tsx @@ -1,7 +1,9 @@ import { IUser } from "@/types/user"; import { createContext, useContext } from "react"; -interface IUserContextType extends IUser {} +interface IUserContextType extends IUser { + updateContext: (user: IUser | null) => void; +} const UserContext = createContext(null);