generated from supabase-community/vercel-ai-chatbot
-
Notifications
You must be signed in to change notification settings - Fork 12
/
auth.ts
38 lines (31 loc) · 1.03 KB
/
auth.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
import { jsonResponse, verifyToken } from '@/lib/utils'
import { cookies } from 'next/headers'
import { createClient } from '@supabase/supabase-js'
import { createServerActionClient } from '@supabase/auth-helpers-nextjs'
export const auth = async () => {
const address = cookies().get('address')?.value || ''
const web3jwt = cookies().get('web3jwt')?.value || ''
const validToken = await verifyToken(web3jwt, address)
if (web3jwt && validToken) {
const url = process.env.NEXT_PUBLIC_SUPABASE_URL || ''
const anonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY || ''
const headers = {
global: {
headers: { Authorization: `Bearer ${web3jwt}` }
},
auth: { persistSession: false }
}
const supabase = createClient(url, anonKey, headers)
const {
data: { user }
} = await supabase.auth.getUser()
console.log('user: ', user)
if (user) {
return user
}
} else {
if (address) cookies().delete('address')
if (web3jwt) cookies().delete('web3jwt')
}
return null
}