Created
March 16, 2021 16:05
-
-
Save kylewelsby/892b3e5859d3cfec2640320e126a1c4f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default { | |
// ... | |
serverMiddleware: [ | |
{ path: '/api/supabase', handler: '~/server-middleware/supabase.mjs' }, | |
], | |
// ... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { Router } = require('@xdn/core/router') | |
const { nuxtRoutes } = require('@xdn/nuxt') | |
module.exports = new Router() | |
.match('/api/supabase/:path/', ({ cache, renderWithApp }) => { | |
renderWithApp() | |
}) | |
.use(nuxtRoutes) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cookieParser from 'cookie-parser' | |
import express from 'express' | |
import { createClient } from '@supabase/supabase-js' | |
const app = express() | |
const URL = process.env.SUPABASE_URL | |
const KEY = process.env.SUPABASE_KEY | |
const supabase = createClient(URL, KEY) | |
app.use(express.json()) | |
app.use(cookieParser()) | |
app.post('/auth', async (req, res) => { | |
try { | |
await supabase.auth.api.setAuthCookie(req, res) | |
} catch (error) { | |
if (error.message === 'Auth event missing!') { | |
res.status(422).send('Auth event missing!') | |
return | |
} | |
// eslint-disable-next-line no-console | |
console.error('Error: ', error) | |
res.status(500).send('Failed to get user') | |
} | |
}) | |
app.get('/user', async (req, res) => { | |
try { | |
const user = await supabase.auth.api.getUserByCookie(req) | |
res.json(user) | |
} catch (error) { | |
// eslint-disable-next-line no-console | |
console.error('Error: ', error) | |
res.status(500).send('Failed to get user') | |
} | |
}) | |
app.all('*', (req, res) => { | |
console.log(req) | |
res.status(418).send("I'm a teapot") | |
}) | |
export default app |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment