mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 10:42:08 +00:00
fix logout
This commit is contained in:
@@ -23,7 +23,7 @@ const Navbar = () => {
|
||||
|
||||
const authenticatedPages: readonly Page[] = [
|
||||
{ slug: '/account', name: 'Account' },
|
||||
{ slug: '/logout', name: 'Logout' },
|
||||
{ slug: '/api/users/logout', name: 'Logout' },
|
||||
];
|
||||
|
||||
const unauthenticatedPages: readonly Page[] = [
|
||||
@@ -44,7 +44,7 @@ const Navbar = () => {
|
||||
return (
|
||||
<nav className="navbar bg-primary text-primary-content">
|
||||
<div className="flex-1">
|
||||
<Link className="btn btn-ghost text-3xl normal-case" href="/">
|
||||
<Link className="btn-ghost btn text-3xl normal-case" href="/">
|
||||
<span className="cursor-pointer text-xl font-bold">The Biergarten App</span>
|
||||
</Link>
|
||||
</div>
|
||||
@@ -69,7 +69,7 @@ const Navbar = () => {
|
||||
</div>
|
||||
<div className="flex-none lg:hidden">
|
||||
<div className="dropdown-end dropdown">
|
||||
<label tabIndex={0} className="btn btn-ghost btn-circle">
|
||||
<label tabIndex={0} className="btn-ghost btn-circle btn">
|
||||
<span className="w-10 rounded-full">
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
|
||||
@@ -9,6 +9,9 @@ const useUser = () => {
|
||||
error,
|
||||
isLoading,
|
||||
} = useSWR('/api/users/current', async (url) => {
|
||||
if (!document.cookie.includes('token')) {
|
||||
return null;
|
||||
}
|
||||
const response = await fetch(url);
|
||||
|
||||
if (!response.ok) {
|
||||
|
||||
@@ -21,11 +21,7 @@ router.all(async (req, res) => {
|
||||
|
||||
removeTokenCookie(res);
|
||||
|
||||
res.status(200).json({
|
||||
message: 'Logged out.',
|
||||
statusCode: 200,
|
||||
success: true,
|
||||
});
|
||||
res.redirect('/');
|
||||
});
|
||||
|
||||
const handler = router.handler(NextConnectOptions);
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { setLoginSession } from '@/config/auth/session';
|
||||
import { NextApiRequest, NextApiResponse } from 'next';
|
||||
import { z } from 'zod';
|
||||
import ServerError from '@/config/util/ServerError';
|
||||
@@ -35,6 +36,11 @@ const registerUser = async (req: RegisterUserRequest, res: NextApiResponse) => {
|
||||
}
|
||||
|
||||
const user = await createNewUser(req.body);
|
||||
|
||||
await setLoginSession(res, {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
});
|
||||
res.status(201).json({
|
||||
message: 'User created successfully.',
|
||||
payload: user,
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
import { NextPage } from 'next';
|
||||
import { useRouter } from 'next/router';
|
||||
import { useEffect } from 'react';
|
||||
|
||||
const LogoutPage: NextPage = () => {
|
||||
const router = useRouter();
|
||||
|
||||
useEffect(() => {
|
||||
document.cookie = 'token=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
|
||||
router.reload();
|
||||
router.push('/');
|
||||
}, [router]);
|
||||
return null;
|
||||
};
|
||||
|
||||
export default LogoutPage;
|
||||
@@ -10,6 +10,7 @@ import sendRegisterUserRequest from '@/requests/sendRegisterUserRequest';
|
||||
import CreateUserValidationSchema from '@/services/User/schema/CreateUserValidationSchema';
|
||||
import { zodResolver } from '@hookform/resolvers/zod';
|
||||
import { NextPage } from 'next';
|
||||
import { useRouter } from 'next/router';
|
||||
import { useState } from 'react';
|
||||
import { useForm } from 'react-hook-form';
|
||||
import { FaUserCircle } from 'react-icons/fa';
|
||||
@@ -18,6 +19,7 @@ import { z } from 'zod';
|
||||
interface RegisterUserProps {}
|
||||
|
||||
const RegisterUserPage: NextPage<RegisterUserProps> = () => {
|
||||
const router = useRouter();
|
||||
const { reset, register, handleSubmit, formState } = useForm<
|
||||
z.infer<typeof CreateUserValidationSchema>
|
||||
>({
|
||||
@@ -31,7 +33,7 @@ const RegisterUserPage: NextPage<RegisterUserProps> = () => {
|
||||
const onSubmit = async (data: z.infer<typeof CreateUserValidationSchema>) => {
|
||||
try {
|
||||
await sendRegisterUserRequest(data);
|
||||
reset();
|
||||
router.push('/', undefined, { shallow: true });
|
||||
} catch (error) {
|
||||
setServerResponseError(
|
||||
error instanceof Error
|
||||
|
||||
Reference in New Issue
Block a user