-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.ts
90 lines (81 loc) · 2.05 KB
/
server.ts
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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 '@auth/core/providers/github'
import NextAuthPlugin from 'fastify-next-auth'
import type { AuthOptions } from 'fastify-next-auth'
import fastifyStatic from '@fastify/static'
import { getSession } from 'authey'
const schema = {
type: 'object',
required: ['NEXTAUTH_URL', 'AUTH_SECRET', 'GITHUB_CLIENT_ID', 'GITHUB_CLIENT_SECRET'],
properties: {
NEXTAUTH_URL: {
type: 'string',
default: 'http://localhost:3000',
},
AUTH_SECRET: {
type: 'string',
},
GITHUB_CLIENT_ID: {
type: 'string',
},
GITHUB_CLIENT_SECRET: {
type: 'string',
},
},
}
const fastify = Fastify({ logger: false })
const __dirname = dirname(fileURLToPath(import.meta.url))
async function initialize() {
fastify
.register(fastifyStatic, {
root: path.join(__dirname, 'public'),
})
.register(fastifyEnv, {
schema,
dotenv: true,
})
await fastify.after()
await fastify.register(NextAuthPlugin, {
secret: process.env.AUTH_SECRET,
trustHost: true,
providers: [
GithubProvider({
clientId: process.env.GITHUB_CLIENT_ID as string,
clientSecret: process.env.GITHUB_CLIENT_SECRET as string,
}),
],
} as AuthOptions)
}
fastify.get('/', (req, reply) => {
return reply.sendFile('index.html')
})
fastify.get('/api/user', async (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
})
initialize()
async function startServer() {
try {
await fastify.ready()
await fastify.listen({
port: 3000,
})
console.log('listening on port 3000')
}
catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
startServer()