authjs #2

Merged
christian merged 3 commits from authjs into main 2024-05-16 21:42:41 +00:00
6 changed files with 47 additions and 25 deletions
Showing only changes of commit fab52dbfaf - Show all commits

View File

@ -1,2 +1,3 @@
import { handlers } from "@/auth" // Referring to the auth.ts we just created import { handlers } from "@/auth" // Referring to the auth.ts we just created
export const { GET, POST } = handlers export const { GET, POST } = handlers

View File

@ -1,6 +1,7 @@
import type { Metadata } from "next"; import type { Metadata } from "next";
import { Inter } from "next/font/google"; import { Inter } from "next/font/google";
import "./globals.css"; import "./globals.css";
import { SessionProvider } from "next-auth/react";
const inter = Inter({ subsets: ["latin"] }); const inter = Inter({ subsets: ["latin"] });

View File

@ -1,22 +1,14 @@
import { getUsers } from "@/drizzle/db"; import { getUsers } from "@/drizzle/db";
import Image from "next/image";
import avatar from "../public/leverpostej.jpg";
import SignIn from "@/components/SignIn"; import SignIn from "@/components/SignIn";
import { auth } from "@/auth";
import { Session } from "next-auth";
export default async function Home() { export default async function Home() {
const users = await getUsers(); const users = await getUsers();
console.log(users); const session: Session | null = await auth();
const name = users[0] ? users[0].name : "guest";
return ( return (
<main className="bg-black text-white"> <main className="bg-black text-white">
<h1>Hello {name}!</h1> <SignIn session={session} />
<Image
src={avatar}
width={100}
height={100}
alt={`Picture of ${users[0].name}`}
/>
<SignIn />
</main> </main>
); );
} }

View File

@ -1,14 +1,31 @@
import { signIn } from "@/auth" "use client";
export default async function SignIn() { import { signIn, signOut } from "next-auth/react";
return ( import { SyntheticEvent } from "react";
<form import { Session } from "next-auth";
action={async () => {
"use server" interface SignInProps {
await signIn("github") session: Session | null;
}} }
>
<button type="submit">Signin with GitHub</button> export default async function SignIn({ session }: SignInProps) {
</form> const handleSignIn = async (event: SyntheticEvent) => {
) event.preventDefault();
await signIn("github");
};
const handleSignOut = async (event: SyntheticEvent) => {
event.preventDefault();
await signOut();
};
return session?.user ? (
<form onSubmit={handleSignOut}>
<button type="submit">Sign out</button>
</form>
) : (
<form onSubmit={handleSignIn}>
<button type="submit">Sign in with GitHub</button>
</form>
);
} }

10
package-lock.json generated
View File

@ -9,6 +9,7 @@
"version": "0.1.0", "version": "0.1.0",
"dependencies": { "dependencies": {
"@next/env": "^14.2.3", "@next/env": "^14.2.3",
"@types/next-auth": "^3.15.0",
"@vercel/postgres": "^0.8.0", "@vercel/postgres": "^0.8.0",
"drizzle-orm": "^0.30.10", "drizzle-orm": "^0.30.10",
"next": "14.2.3", "next": "14.2.3",
@ -1327,6 +1328,15 @@
"integrity": "sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ==", "integrity": "sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ==",
"dev": true "dev": true
}, },
"node_modules/@types/next-auth": {
"version": "3.15.0",
"resolved": "https://registry.npmjs.org/@types/next-auth/-/next-auth-3.15.0.tgz",
"integrity": "sha512-ZVfejlu81YiIRX1m0iKAfvZ3nK7K9EyZWhNARNKsFop8kNAgEvMnlKpTpwN59xkK2OhyWLagPuiDAVBYSO9jSA==",
"deprecated": "This is a stub types definition. next-auth provides its own type definitions, so you do not need this installed.",
"dependencies": {
"next-auth": "*"
}
},
"node_modules/@types/node": { "node_modules/@types/node": {
"version": "20.12.12", "version": "20.12.12",
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.12.12.tgz", "resolved": "https://registry.npmjs.org/@types/node/-/node-20.12.12.tgz",

View File

@ -10,6 +10,7 @@
}, },
"dependencies": { "dependencies": {
"@next/env": "^14.2.3", "@next/env": "^14.2.3",
"@types/next-auth": "^3.15.0",
"@vercel/postgres": "^0.8.0", "@vercel/postgres": "^0.8.0",
"drizzle-orm": "^0.30.10", "drizzle-orm": "^0.30.10",
"next": "14.2.3", "next": "14.2.3",