passport-lnurl-auth
passport-lnurl-auth copied to clipboard
has anyone successfully used this with Next.js?
Here I tried to use next-connect to do this in the Next api. It doesn't work but feels like I am close. https://www.npmjs.com/package/next-connect
// pages/api/hello.js
import nc from 'next-connect'
import passport from 'passport'
const MongoStore = require('connect-mongo')
const path = require('path')
const LnurlAuth = require('passport-lnurl-auth')
const session = require('cookie-session')
const mongoOptions = {
//httpOnly: false,
mongoUrl:
'mongodb+srv://[email protected]/mydbname?retryWrites=true&w=majority'
}
const config = {
host: 'localhost',
port: 3001,
url: 'localhost:3001'
}
const handler = nc({
onError: (err, req, res, next) => {
console.error(err)
res.status(500).end('Something broke!')
},
onNoMatch: (req, res) => {
res.status(404).end('Page is not found')
}
})
handler.use(
session({
secret: '1jlfaksdjlfajkdl2345',
store: MongoStore.create(mongoOptions),
resave: false,
saveUninitialized: true,
cookie: {
// path: "/",//if / the cookies will be sent for all paths
httpOnly: false, // if true, the cookie cannot be accessed from within the client-side javascript code.
//secure: true, // true->cookie has to be sent over HTTPS
maxAge: 2 * 24 * 60 * 60 * 1000
//sameSite: 'none' //- `none` will set the `SameSite` attribute to `None` for an explicit cross-site cookie.
}
})
)
handler.use(passport.initialize())
handler.use(passport.session())
const map = {
user: new Map()
}
passport.serializeUser(function (user, done) {
done(null, user.id)
})
passport.deserializeUser(function (id, done) {
done(null, map.user.get(id) || null)
})
passport.use(
new LnurlAuth.Strategy(function (linkingPublicKey, done) {
let user = map.user.get(linkingPublicKey)
if (!user) {
user = { id: linkingPublicKey }
map.user.set(linkingPublicKey, user)
}
done(null, user)
})
)
handler.use(passport.authenticate('lnurl-auth'))
// handler.get(function (req, res) {
// if (!req.user) {
// return res.send(
// 'You are not authenticated. To login go <a href="/login">here</a>.'
// )
// // return res.redirect('/login');
// }
// res.send('Logged-in')
// })
handler.get(
function (req, res, next) {
console.log('here34', req)
if (req.user) {
console.log('here35')
// Already authenticated.
return res.redirect('http://localhost:3001/home')
}
next()
},
new LnurlAuth.Middleware({
callbackUrl: config.url + '/api/login',
cancelUrl: 'http://localhost:3001/',
loginTemplateFilePath: '././pages/api/login.html'
})
)
handler.get('/user', (req, res) => {
res.send(req.user)
})
handler.get('/logout', function (req, res, next) {
if (req.user) {
req.session.destroy()
res.json({ message: 'user logged out' })
// Already authenticated.
// return res.redirect('http://localhost:3001/')
}
next()
})
export default handler
ended up figuring this out, but I had to use a custom server. here's how i did it: https://github.com/Jared-Dahlke/Nextjs-lightning-auth-template
The custom server causes some problems with getServerSideProps and env variables, so I'm still trying to figure out how to do this using a regular pages/api route.
fyi also figured out how to do this without using a custom server using NextAuth
https://github.com/Jared-Dahlke/nextauth-lnurl-template