-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: passwordless auth proof of concept
- Loading branch information
Showing
7 changed files
with
178 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ PAGARME_ENC_KEY=PAGARME_TEST_ENVIRONMENT_ECRYPTION_KEY | |
ROLLOUT_API_KEY=ROLLOUT_API_KEY | ||
HEROKU_APP_NAME=reditus-local-SOME_RANDOM_NUMBER | ||
[email protected] | ||
MAILER_PASS=anything | ||
MAILER_PASS=anything | ||
MAILER_SERVICE=mailhog |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { NextApiRequest, NextApiResponse } from "next"; | ||
import NextAuth, { InitOptions } from "next-auth"; | ||
import Providers from "next-auth/providers"; | ||
import Adapters from "next-auth/adapters"; | ||
import { PrismaClient } from "@prisma/client"; | ||
|
||
// TODO: Use dotenv to setup test config. | ||
const site = process.env.SITE || "http://localhost:3000"; | ||
const server = | ||
process.env.EMAIL_SERVER || "smtp://mailhog:password@localhost:1025"; | ||
const from = process.env.EMAIL_FROM || "[email protected]"; | ||
|
||
const email: string = process.env.MAILER_EMAIL || ""; | ||
const pass: string = process.env.MAILER_PASS || ""; | ||
const prisma = new PrismaClient(); | ||
|
||
if (!site || !server || !from) throw new Error("Invalid auth configuration."); | ||
|
||
// Ignore some eslint rules due to the way next-auth is setup (functions that have capitalized names) | ||
/* eslint new-cap: ["error", { "capIsNewExceptions": ["Adapters.Prisma.Adapter", "Providers.Email", "NextAuth"] }] */ | ||
|
||
const baseAdapter = Adapters.Prisma.Adapter({ prisma }); | ||
async function notImplemented() { | ||
throw new Error("Not implemented"); | ||
} | ||
const adapter = { | ||
...baseAdapter, | ||
createUser: notImplemented, | ||
deleteUser: notImplemented, | ||
unlinkAccount: notImplemented, | ||
}; | ||
|
||
const options: InitOptions = { | ||
// Configure one or more authentication providers | ||
providers: [ | ||
Providers.Email({ | ||
server: { | ||
host: "localhost", | ||
port: 1025, | ||
auth: { | ||
user: email, | ||
pass, | ||
}, | ||
}, | ||
from, | ||
}), | ||
], | ||
adapter, | ||
}; | ||
|
||
export default (req: NextApiRequest, res: NextApiResponse) => | ||
NextAuth(req, res, options); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,14 @@ import Head from "next/head"; | |
import { GridRow, GridCell } from "@rmwc/grid"; | ||
import { List, SimpleListItem } from "@rmwc/list"; | ||
import styles from "./index.module.css"; | ||
import { signIn, signOut, useSession } from "next-auth/client"; | ||
|
||
// Components | ||
import { Form } from "../components/Form"; | ||
|
||
export default function Home() { | ||
const [session, loading] = useSession(); | ||
|
||
return ( | ||
<div> | ||
<Head> | ||
|
@@ -60,6 +63,27 @@ export default function Home() { | |
> | ||
<img src="./logoReditusWhite.png" /> | ||
<Form /> | ||
{/* Demo on how to use next-auth hooks. */} | ||
{loading} | ||
{!session && ( | ||
<> | ||
Not signed in <br /> | ||
<button onClick={() => signIn()}>Sign in</button> | ||
<button | ||
onClick={() => | ||
signIn("email", { email: "[email protected]" }) | ||
} | ||
> | ||
Sign in with specific Email | ||
</button> | ||
</> | ||
)} | ||
{session && ( | ||
<> | ||
Signed in as {session.user.email} <br /> | ||
<button onClick={() => signOut()}>Sign out</button> | ||
</> | ||
)} | ||
</GridCell> | ||
</GridRow> | ||
</main> | ||
|
37 changes: 37 additions & 0 deletions
37
prisma/migrations/20210211232718_add_minimum_requirements_for_next_auth/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
-- AlterTable | ||
ALTER TABLE "users" ADD COLUMN "email_verified" TIMESTAMP(3), | ||
ADD COLUMN "updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP; | ||
|
||
-- CreateTable | ||
CREATE TABLE "verification_requests" ( | ||
"id" SERIAL NOT NULL, | ||
"identifier" TEXT NOT NULL, | ||
"token" TEXT NOT NULL, | ||
"expires" TIMESTAMP(3) NOT NULL, | ||
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
|
||
PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "sessions" ( | ||
"id" SERIAL NOT NULL, | ||
"user_id" INTEGER NOT NULL, | ||
"expires" TIMESTAMP(3) NOT NULL, | ||
"session_token" TEXT NOT NULL, | ||
"access_token" TEXT NOT NULL, | ||
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
|
||
PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "verification_requests.token_unique" ON "verification_requests"("token"); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "sessions.session_token_unique" ON "sessions"("session_token"); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "sessions.access_token_unique" ON "sessions"("access_token"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters