Force auth
This commit is contained in:
parent
1cc1beb63e
commit
c8aefcab04
5 changed files with 26 additions and 9 deletions
|
|
@ -18,10 +18,10 @@ export async function POST(req: Request) {
|
||||||
const { messages, previewToken } = json
|
const { messages, previewToken } = json
|
||||||
const session = await auth()
|
const session = await auth()
|
||||||
|
|
||||||
if (process.env.VERCEL_ENV === 'preview') {
|
|
||||||
if (session == null) {
|
if (session == null) {
|
||||||
return new Response('Unauthorized', { status: 401 })
|
return new Response('Unauthorized', {
|
||||||
}
|
status: 401
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
if (previewToken) {
|
if (previewToken) {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,13 @@
|
||||||
|
import { auth } from '@/auth'
|
||||||
import { LoginButton } from '@/components/login-button'
|
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 (
|
return (
|
||||||
<div className="flex h-[calc(100vh-theme(spacing.16))] items-center justify-center py-10">
|
<div className="flex h-[calc(100vh-theme(spacing.16))] items-center justify-center py-10">
|
||||||
<LoginButton />
|
<LoginButton />
|
||||||
|
|
|
||||||
8
auth.ts
8
auth.ts
|
|
@ -17,11 +17,11 @@ export const {
|
||||||
token.image = profile.picture
|
token.image = profile.picture
|
||||||
}
|
}
|
||||||
return token
|
return token
|
||||||
|
},
|
||||||
|
// @ts-ignore
|
||||||
|
authorized({ auth }) {
|
||||||
|
return !!auth?.user
|
||||||
}
|
}
|
||||||
// @TODO
|
|
||||||
// authorized({ request, auth }) {
|
|
||||||
// return !!auth?.user
|
|
||||||
// }
|
|
||||||
},
|
},
|
||||||
pages: {
|
pages: {
|
||||||
signIn: '/sign-in'
|
signIn: '/sign-in'
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ import {
|
||||||
import { useState } from 'react'
|
import { useState } from 'react'
|
||||||
import { Button } from './ui/button'
|
import { Button } from './ui/button'
|
||||||
import { Input } from './ui/input'
|
import { Input } from './ui/input'
|
||||||
|
import { toast } from 'react-hot-toast'
|
||||||
|
|
||||||
const IS_PREVIEW = process.env.VERCEL_ENV === 'preview'
|
const IS_PREVIEW = process.env.VERCEL_ENV === 'preview'
|
||||||
export interface ChatProps extends React.ComponentProps<'div'> {
|
export interface ChatProps extends React.ComponentProps<'div'> {
|
||||||
|
|
@ -40,6 +41,11 @@ export function Chat({ id, initialMessages, className }: ChatProps) {
|
||||||
body: {
|
body: {
|
||||||
id,
|
id,
|
||||||
previewToken
|
previewToken
|
||||||
|
},
|
||||||
|
onResponse(response) {
|
||||||
|
if (response.status === 401) {
|
||||||
|
toast.error(response.statusText)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return (
|
return (
|
||||||
|
|
|
||||||
|
|
@ -1 +1,5 @@
|
||||||
export { auth as middleware } from './auth'
|
export { auth as middleware } from './auth'
|
||||||
|
|
||||||
|
export const config = {
|
||||||
|
matcher: ['/', '/api/chat']
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue