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