Force auth

This commit is contained in:
Jared Palmer 2023-06-22 10:37:35 -07:00
parent 1cc1beb63e
commit c8aefcab04
5 changed files with 26 additions and 9 deletions

View file

@ -18,10 +18,10 @@ export async function POST(req: Request) {
const { messages, previewToken } = json
const session = await auth()
if (process.env.VERCEL_ENV === 'preview') {
if (session == null) {
return new Response('Unauthorized', { status: 401 })
}
return new Response('Unauthorized', {
status: 401
})
}
if (previewToken) {

View file

@ -1,6 +1,13 @@
import { auth } from '@/auth'
import { LoginButton } from '@/components/login-button'
import { redirect } from 'next/navigation'
export default function SignInPage() {
export default async function SignInPage() {
const session = await auth()
// redirect to home if user is already logged in
if (session?.user) {
redirect('/')
}
return (
<div className="flex h-[calc(100vh-theme(spacing.16))] items-center justify-center py-10">
<LoginButton />

View file

@ -17,11 +17,11 @@ export const {
token.image = profile.picture
}
return token
},
// @ts-ignore
authorized({ auth }) {
return !!auth?.user
}
// @TODO
// authorized({ request, auth }) {
// return !!auth?.user
// }
},
pages: {
signIn: '/sign-in'

View file

@ -19,6 +19,7 @@ import {
import { useState } from 'react'
import { Button } from './ui/button'
import { Input } from './ui/input'
import { toast } from 'react-hot-toast'
const IS_PREVIEW = process.env.VERCEL_ENV === 'preview'
export interface ChatProps extends React.ComponentProps<'div'> {
@ -40,6 +41,11 @@ export function Chat({ id, initialMessages, className }: ChatProps) {
body: {
id,
previewToken
},
onResponse(response) {
if (response.status === 401) {
toast.error(response.statusText)
}
}
})
return (

View file

@ -1 +1,5 @@
export { auth as middleware } from './auth'
export const config = {
matcher: ['/', '/api/chat']
}