This commit is contained in:
2022-04-11 11:07:29 +01:00
parent 412b7c15b6
commit 67102497f0
8 changed files with 4266 additions and 2584 deletions
+29 -25
View File
@@ -1,33 +1,37 @@
import NextAuth from "next-auth";
import Providers from "next-auth/providers";
import { Magic } from "@magic-sdk/admin";
const magic = new Magic(process.env.MAGIC_SK);
import { TypeORMLegacyAdapter } from "@next-auth/typeorm-legacy-adapter";
import EmailProvider from "next-auth/providers/email";
import * as entities from "../../../lib/entities";
// For more information on each option (and a full list of options) go to
// https://next-auth.js.org/configuration/options
export default NextAuth({
session: {
jwt: true,
},
pages: {
// override signIn page so we can integrate with Magic
signIn: "/auth/signin",
},
// https://next-auth.js.org/configuration/providers
providers: [
Providers.Credentials({
name: "Magic Link",
credentials: {
didToken: { label: "DID Token", type: "text" },
},
async authorize({ didToken }, req) {
// validate magic DID token
magic.token.validate(didToken);
// fetch user metadata
const metadata = await magic.users.getMetadataByToken(didToken);
// return user info
return { ...metadata };
EmailProvider({
server: {
host: process.env.EMAIL_SERVER_HOST,
port: process.env.EMAIL_SERVER_PORT,
auth: {
user: process.env.EMAIL_SERVER_USER,
pass: process.env.EMAIL_SERVER_PASSWORD,
},
},
from: process.env.EMAIL_FROM,
maxAge: 10 * 60, // Magic links are valid for 10 min only
}),
],
adapter: TypeORMLegacyAdapter({
type: "mysql",
database:
"mysql://nextAuthAdmin:!Sky9fish1972@77.68.17.157:3306/nextauthDB?synchronise=true",
synchronize: false,
}),
secret: "SuperSecret",
session: {
jwt: true,
pages: {},
theme: "dark",
debug: true,
},
});
-36
View File
@@ -1,36 +0,0 @@
import { useRouter } from "next/router";
import { signIn } from "next-auth/client";
// magic-sdk is only availabile in the browser
const magic =
typeof window !== "undefined" &&
new Magic(process.env.NEXT_PUBLIC_MAGIC_PK || "a");
export default function SignIn() {
const router = useRouter();
const { register, handleSubmit } = useForm();
const onSubmit = async ({ email }) => {
if (!magic) throw new Error(`magic not defined`);
// login with Magic
const didToken = await magic.auth.loginWithMagicLink({ email });
// sign in with NextAuth
await signIn("credentials", {
didToken,
callbackUrl: router.query["callbackUrl"],
});
};
return (
<form onSubmit={handleSubmit(onSubmit)}>
<input
{...register("email", { required: true })}
placeholder="nick@example.com"
/>
<button type="submit">Sign in</button>
</form>
);
}