hoangvvo / nextjs-mongodb-app

A Next.js and MongoDB web application, designed with simplicity for learning and real-world applicability in mind.
https://nextjs-mongodb.now.sh/
MIT License
1.52k stars 287 forks source link

use with passport-lnurl-auth #158

Open Jared-Dahlke opened 1 year ago

Jared-Dahlke commented 1 year ago

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:Qdgdh@gitlabgo-nbdkt.mongodb.net/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 commented 1 year ago

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