forked from syhner/next-kickstart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.tsx
45 lines (41 loc) · 1.02 KB
/
auth.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
'use client';
import { ReactNode } from 'react';
import { useRouter } from 'next/navigation';
import { AuthProvider } from '~/lib/auth';
import { Button } from './ui/button';
export const Login = ({
provider,
children,
}: {
provider: AuthProvider;
children?: ReactNode;
}) => (
<Button asChild>
<a href={`/auth/login/${provider}`}>{children}</a>
</Button>
);
export const Logout = () => {
const router = useRouter();
return (
<form
action='/api/auth/logout'
method='post'
onSubmit={async (e) => {
e.preventDefault();
const formData = new FormData(e.currentTarget);
const response = await fetch('/api/auth/logout', {
method: 'POST',
body: formData,
redirect: 'manual',
});
if (response.status === 0) {
// Redirected
// When using `redirect: "manual"`, response status 0 is returned
return router.refresh();
}
}}
>
<Button type='submit'>Logout</Button>
</form>
);
};