Skip to content

Commit

Permalink
feat: migrate to Auth.js
Browse files Browse the repository at this point in the history
  • Loading branch information
wobsoriano committed Dec 16, 2022
1 parent 989b27d commit 865b0d3
Show file tree
Hide file tree
Showing 14 changed files with 1,775 additions and 1,091 deletions.
32 changes: 19 additions & 13 deletions package.json
Expand Up @@ -52,30 +52,36 @@
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"release": "bumpp && npm publish",
"update-deps": "taze -w && pnpm i",
"dev:playground": "pnpm --filter playground dev"
},
"peerDependencies": {
"next-auth": "^4.10.0"
"@auth/core": ">=0.1.4"
},
"dependencies": {
"fastify-plugin": "^4.3.0",
"node-fetch-native": "^0.1.8"
"@auth/core": "^0.1.4",
"@fastify/middie": "^8.1.0",
"@hattip/adapter-node": "^0.0.22",
"@hattip/polyfills": "^0.0.22",
"authey": "^0.1.5",
"fastify-plugin": "^4.4.0"
},
"devDependencies": {
"@antfu/eslint-config": "^0.25.1",
"@fastify/cookie": "^7.0.0",
"@fastify/env": "^4.0.0",
"@fastify/formbody": "7.0.1",
"@types/node": "^17.0.35",
"@antfu/eslint-config": "^0.25.2",
"@fastify/cookie": "^8.3.0",
"@fastify/env": "^4.2.0",
"@fastify/formbody": "7.4.0",
"@types/connect": "^3.4.35",
"@types/node": "^17.0.45",
"@types/tap": "^15.0.7",
"bumpp": "^8.2.1",
"eslint": "^8.16.0",
"fastify": "4.0.0",
"next-auth": "^4.15.0",
"eslint": "^8.29.0",
"esno": "^0.16.3",
"fastify": "4.10.2",
"taze": "^0.8.4",
"tsup": "5.11.13",
"tsx": "^3.4.2",
"typescript": "4.5.4",
"vitest": "^0.13.0"
"vitest": "^0.13.1"
},
"eslintConfig": {
"extends": "@antfu"
Expand Down
2 changes: 1 addition & 1 deletion playground/.env.example
@@ -1,4 +1,4 @@
GITHUB_CLIENT_ID=
GITHUB_CLIENT_SECRET=
NEXTAUTH_URL=
NEXTAUTH_SECRET=
AUTH_SECRET=
5 changes: 3 additions & 2 deletions playground/package.json
@@ -1,11 +1,12 @@
{
"name": "playground",
"type": "module",
"private": true,
"scripts": {
"dev": "tsx watch ./server.ts"
"dev": "esno ./server.ts"
},
"dependencies": {
"@fastify/static": "^6.4.0",
"@fastify/static": "^6.6.0",
"fastify-next-auth": "workspace:*"
}
}
7 changes: 5 additions & 2 deletions playground/public/index.html
Expand Up @@ -23,7 +23,7 @@
<script src="https://unpkg.com/vue@3"></script>
<script type="module">
import * as Client from "https://cdn.skypack.dev/fastify-next-auth/client"
const { getSession, signIn, signOut } = Client
const { signIn, signOut } = Client
const { createApp, ref, onMounted } = Vue

createApp({
Expand All @@ -32,7 +32,10 @@
const user = ref(null)

onMounted(async() => {
user.value = await getSession()
// user.value = await getSession()
const res = await fetch('/api/user')
const data = await res.json()
console.log(data)
loading.value = false
})

Expand Down
42 changes: 25 additions & 17 deletions playground/server.ts
@@ -1,23 +1,23 @@
import path from 'path'
import path, { dirname } from 'path'
import { fileURLToPath } from 'url'
import type { IncomingMessage } from 'http'
import Fastify from 'fastify'
import fastifyEnv from '@fastify/env'
import GithubProvider from 'next-auth/providers/github'
import GithubProvider from '@auth/core/providers/github'
import NextAuthPlugin from 'fastify-next-auth'
import type { NextAuthOptions } from 'fastify-next-auth'
import type { AuthOptions } from 'fastify-next-auth'
import fastifyStatic from '@fastify/static'
import { getSession } from 'fastify-next-auth/client'
import cookie from '@fastify/cookie'
import formBody from '@fastify/formbody'
import { getSession } from 'authey'

const schema = {
type: 'object',
required: ['NEXTAUTH_URL', 'NEXTAUTH_SECRET', 'GITHUB_CLIENT_ID', 'GITHUB_CLIENT_SECRET'],
required: ['NEXTAUTH_URL', 'AUTH_SECRET', 'GITHUB_CLIENT_ID', 'GITHUB_CLIENT_SECRET'],
properties: {
NEXTAUTH_URL: {
type: 'string',
default: 'http://localhost:3000',
},
NEXTAUTH_SECRET: {
AUTH_SECRET: {
type: 'string',
},
GITHUB_CLIENT_ID: {
Expand All @@ -29,7 +29,9 @@ const schema = {
},
}

const fastify = Fastify({ logger: true })
const fastify = Fastify({ logger: false })

const __dirname = dirname(fileURLToPath(import.meta.url))

async function initialize() {
fastify
Expand All @@ -40,26 +42,32 @@ async function initialize() {
schema,
dotenv: true,
})
.register(cookie)
.register(formBody)
await fastify.after()
fastify.register(NextAuthPlugin, {
secret: process.env.NEXTAUTH_SECRET,
await fastify.register(NextAuthPlugin, {
secret: process.env.AUTH_SECRET,
trustHost: true,
providers: [
GithubProvider({
clientId: process.env.GITHUB_CLIENT_ID,
clientSecret: process.env.GITHUB_CLIENT_SECRET,
clientId: process.env.GITHUB_CLIENT_ID as string,
clientSecret: process.env.GITHUB_CLIENT_SECRET as string,
}),
],
} as NextAuthOptions)
} as AuthOptions)
}

fastify.get('/', (req, reply) => {
return reply.sendFile('index.html')
})

fastify.get('/api/user', async (req) => {
const session = await getSession({ req })
const session = await getSession(req as unknown as IncomingMessage, {
providers: [
GithubProvider({
clientId: process.env.GITHUB_CLIENT_ID as string,
clientSecret: process.env.GITHUB_CLIENT_SECRET as string,
}),
],
})
return session
})

Expand Down

0 comments on commit 865b0d3

Please sign in to comment.