chatbot-template/auth.ts

28 lines
755 B
TypeScript
Raw Normal View History

import NextAuth from '@auth/nextjs'
import GitHub from '@auth/nextjs/providers/github'
import { NextResponse } from 'next/server'
2023-06-02 09:55:02 -04:00
2023-05-22 10:16:09 -04:00
export const {
handlers: { GET, POST },
auth,
CSRF_experimental
2023-05-22 10:16:09 -04:00
} = NextAuth({
// @ts-ignore
providers: [GitHub],
session: { strategy: 'jwt' },
2023-05-22 10:16:09 -04:00
async authorized({ request, auth }: any) {
const url = request.nextUrl
2023-05-22 10:16:09 -04:00
if (request.method === 'POST') {
const { authToken } = (await request.json()) ?? {}
2023-05-22 10:16:09 -04:00
// If the request has a valid auth token, it is authorized
const valid = true
if (valid) return true
return NextResponse.json('Invalid auth token', { status: 401 })
2023-05-22 10:16:09 -04:00
}
// Logged in users are authenticated, otherwise redirect to login page
return !!auth
}
})