fix: login and logout updates

Signed-off-by: Varun Raj <mailme@varunraj.in>
This commit is contained in:
Varun Raj committed 2024-09-04 19:00:38 +05:30
1 parent 025fce634b
commit 30526e8e46
5 files changed
+93 -88

No files matched your search

+60 -48
View File
@@ -13,11 +13,18 @@ import { Label } from "@/components/ui/label"
import { useState } from "react" import { useState } from "react"
import { loginUser } from "@/handlers/api/user.handler" import { loginUser } from "@/handlers/api/user.handler"
import { useConfig } from "@/contexts/ConfigContext" import { useConfig } from "@/contexts/ConfigContext"
import { IUser } from "@/types/user"
import Head from "next/head"
export const description = 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." "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 { exImmichUrl } = useConfig()
const [formData, setFormData] = useState({ const [formData, setFormData] = useState({
email: "", email: "",
@@ -34,9 +41,9 @@ export function LoginForm() {
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => { const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault() e.preventDefault()
setLoading(true) setLoading(true)
loginUser(formData.email, formData.password).then((user) => { loginUser(formData.email, formData.password)
console.log(user) .then(onLogin)
}).catch((error) => { .catch((error) => {
setErrorMessage(error.message) setErrorMessage(error.message)
}).finally(() => { }).finally(() => {
setLoading(false) setLoading(false)
@@ -45,49 +52,54 @@ export function LoginForm() {
return ( return (
<div className="relative flex min-h-screen justify-center flex-col bg-background"> <>
<Card className="mx-auto max-w-sm"> <Head>
<CardHeader> <title>Immich Power Tools - Login</title>
<img </Head>
src="/favicon.png" <div className="relative flex min-h-screen justify-center flex-col bg-background">
width={32} <Card className="mx-auto max-w-sm">
height={32} <CardHeader>
alt="Immich Power Tools" <img
className="w-8 h-8" src="/favicon.png"
/> width={32}
<CardTitle className="text-2xl">Login to Immich</CardTitle> height={32}
<CardDescription> alt="Immich Power Tools"
Login to your connected Immich instance <Link href={exImmichUrl} className="text-xs" target="_blank">({exImmichUrl})</Link> className="w-8 h-8"
</CardDescription> />
</CardHeader> <CardTitle className="text-2xl">Login to Immich</CardTitle>
<CardContent> <CardDescription>
<form className="grid gap-4" onSubmit={handleSubmit}> Login to your connected Immich instance <Link href={exImmichUrl} className="text-xs" target="_blank">({exImmichUrl})</Link>
<div className="grid gap-2"> </CardDescription>
<Label htmlFor="email">Email</Label> </CardHeader>
<Input <CardContent>
id="email" <form className="grid gap-4" onSubmit={handleSubmit}>
type="email" <div className="grid gap-2">
placeholder="m@example.com" <Label htmlFor="email">Email</Label>
required <Input
onChange={handleChange} id="email"
/> type="email"
</div> placeholder="m@example.com"
<div className="grid gap-2"> required
<Label htmlFor="password">Password</Label> onChange={handleChange}
<Input id="password" type="password" required />
onChange={handleChange} </div>
/> <div className="grid gap-2">
</div> <Label htmlFor="password">Password</Label>
<Button type="submit" className="w-full" disabled={loading}> <Input id="password" type="password" required
Login onChange={handleChange}
</Button> />
<Button variant="outline" className="w-full" disabled={loading}> </div>
Login with OAuth <Button type="submit" className="w-full" disabled={loading}>
</Button> Login
</form> </Button>
<Button variant="outline" className="w-full" disabled={loading}>
</CardContent> Login with OAuth
</Card> </Button>
</div> </form>
</CardContent>
</Card>
</div>
</>
) )
} }
+9 -3
View File
@@ -32,13 +32,16 @@ export default function RootLayout({ children }: RootLayoutProps) {
.finally(() => setLoading(false)); .finally(() => setLoading(false));
}; };
const handleLoginCompletion = (user: IUser) => {
setUser(user);
}
useEffect(() => { useEffect(() => {
fetchData(); fetchData();
}, []); }, []);
if (loading) return <Loader />; if (loading) return <Loader />;
if (errorMessage) if (errorMessage)
return ( return (
<div className="min-h-screen flex flex-col justify-center"> <div className="min-h-screen flex flex-col justify-center">
<ErrorBlock <ErrorBlock
@@ -61,10 +64,13 @@ export default function RootLayout({ children }: RootLayoutProps) {
</div> </div>
); );
if (!user) return <LoginForm />; if (!user) return <LoginForm onLogin={handleLoginCompletion} />;
return ( return (
<UserContext.Provider value={user}> <UserContext.Provider value={{
...user,
updateContext: setUser,
}}>
<div className="grid max-h-screen min-h-screen w-full md:grid-cols-[200px_1fr] lg:grid-cols-[240px_1fr]"> <div className="grid max-h-screen min-h-screen w-full md:grid-cols-[200px_1fr] lg:grid-cols-[240px_1fr]">
<Sidebar /> <Sidebar />
<div className="flex flex-col">{children}</div> <div className="flex flex-col">{children}</div>
+21 -35
View File
@@ -1,44 +1,26 @@
import { getMe } from "@/handlers/api/user.handler"; import React from "react";
import { IUser } from "@/types/user";
import React, { useEffect, useState } from "react";
import { Button } from "@/components/ui/button" import { Button } from "@/components/ui/button"
import Link from "next/link"; import Link from "next/link";
import { useConfig } from "@/contexts/ConfigContext"; import { useConfig } from "@/contexts/ConfigContext";
import { logoutUser } from "@/handlers/api/user.handler" 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() { export default function ProfileInfo() {
const router = useRouter(); const { updateContext, ...user } = useCurrentUser()
const { immichURL,exImmichUrl } = useConfig(); const { immichURL, exImmichUrl } = useConfig();
const [user, setUser] = React.useState<IUser | null>(null); const toast = useToast();
const [loading, setLoading] = React.useState(true);
const [errorMessage, setErrorMessage] = useState<string | null>(null);
const fetchData = () => { const handleLogout = () => logoutUser()
return getMe() .then(() => {
.then(setUser) updateContext(null)
.catch((error) => {
setErrorMessage(error.message);
})
.finally(() => setLoading(false));
};
const handleLogout = () => {
logoutUser().then((status) => {
console.log(status)
router.push('/login');
}).catch((error) => { }).catch((error) => {
setErrorMessage(error.message) toast.toast({
title: "Error",
description: error.message,
})
}) })
}
useEffect(() => {
fetchData();
}, []);
if (loading) return <div>Loading...</div>;
if (errorMessage) return <div>{errorMessage}</div>;
return ( return (
<> <>
@@ -51,10 +33,14 @@ export default function ProfileInfo() {
<Link href={immichURL} target="_blank" className="font-mono"> <Link href={immichURL} target="_blank" className="font-mono">
{immichURL} {immichURL}
</Link> </Link>
<p className="text-sm">{user?.name}</p> {user && (
<Button variant="destructive" className="mx-4" onClick={handleLogout}> <>
Log Out <p className="text-sm">{user?.name}</p>
</Button> <Button variant="destructive" className="mx-4" onClick={handleLogout}>
Log Out
</Button>
</>
)}
</div> </div>
<div className="border text-muted-foreground text-xs text-center py-2"> <div className="border text-muted-foreground text-xs text-center py-2">
<p> <p>
-1
View File
@@ -53,7 +53,6 @@ export default function Sidebar() {
</div> </div>
<div> <div>
<ProfileInfo /> <ProfileInfo />
</div> </div>
</div> </div>
</div> </div>
+3 -1
View File
@@ -1,7 +1,9 @@
import { IUser } from "@/types/user"; import { IUser } from "@/types/user";
import { createContext, useContext } from "react"; import { createContext, useContext } from "react";
interface IUserContextType extends IUser {} interface IUserContextType extends IUser {
updateContext: (user: IUser | null) => void;
}
const UserContext = createContext<IUserContextType | null>(null); const UserContext = createContext<IUserContextType | null>(null);