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

use with passport-lnurl-auth #158

Open
Jared-Dahlke opened this issue Feb 17, 2023 · 1 comment
Open

use with passport-lnurl-auth #158

Jared-Dahlke opened this issue Feb 17, 2023 · 1 comment

Comments

@Jared-Dahlke
Copy link

I'm curious if anyone has been able to use passport-lnurl-auth strategy with this.

here is simple working example of how to do with Express server: https://github.com/chill117/passport-lnurl-auth/blob/master/examples/simple.js

Here is my attempt:

// 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://Qyasdghfghfg:[email protected]/dbname?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: 'google.com',
    cancelUrl: 'http://localhost:3001/',
    loginTemplateFilePath: '/',
  })
);

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

ah nevermind I was able to figure it out. I made a template out of it: https://github.com/Jared-Dahlke/Nextjs-lightning-auth-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