Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

has anyone successfully used this with Next.js? #10

Open
Jared-Dahlke opened this issue Feb 12, 2023 · 2 comments
Open

has anyone successfully used this with Next.js? #10

Jared-Dahlke opened this issue Feb 12, 2023 · 2 comments

Comments

@Jared-Dahlke
Copy link

Jared-Dahlke commented Feb 12, 2023

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


@Jared-Dahlke
Copy link
Author

Jared-Dahlke commented Feb 20, 2023

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.

@Jared-Dahlke
Copy link
Author

fyi also figured out how to do this without using a custom server using NextAuth

https://github.com/Jared-Dahlke/nextauth-lnurl-template

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant