简体   繁体   English

在无服务器功能上使用 Auth0(使用 Passport.js)的 Next.js

[英]Next.js with Auth0 (using Passport.js) on Serverless Function

Usually authentication isn't a big deal with auth0 and Lock.js, but it's a little tricky with Next.js and SSR.通常身份验证对于 auth0 和 Lock.js 来说不是什么大问题,但是对于 Next.js 和 SSR 来说有点棘手。 I decided to try passport.js based on a tutorial I found ( nextjs-passport ).我决定根据我找到的教程( nextjs-passport )尝试passport.js。

I want to deploy this to Firebase, so I don't have an express server available.我想将它部署到 Firebase,所以我没有可用的快速服务器。 Passport should work with the custom callback as well, but I'm only getting a blank page and no errors. Passport 也应该与自定义回调一起使用,但我只得到一个空白页并且没有错误。

Anyone had more luck implementing Auth0 authentication with the latest Next.js?任何人在使用最新的 Next.js 实施 Auth0 身份验证时都更幸运?

/pages/login.js /页面/登录.js

import React from 'react'

export default () => (
  <form action='/api/login' method='post'>
    <div className='field'>
      <p className='control has-icons-left has-icons-right'>
        <input className='input' name='email' type='email' placeholder='Email' />
      </p>
    </div>
    <div className='field'>
      <p className='control has-icons-left'>
        <input className='input' name='password' type='password' placeholder='Password' />
      </p>
    </div>
    <div className='field'>
      <p className='control'>
        <input type='submit' className='button is-success' />
      </p>
    </div>
  </form>
)

/pages/api/login.js /pages/api/login.js

import passport from 'passport'
import Cors from 'micro-cors'

const cors = Cors({
  allowedMethods: ['POST', 'HEAD']
})

function Login (req, res, next) {
  return passport.authenticate('auth0', {
    scope: 'openid email profile',
    successRedirect: '/',
    failureFlash: true,
    failureRedirect: '/login'
  }, function (err, user, info) {
    console.log(err, user, info)
    if (err) { return next(err) }
    if (!user) { return res.redirect('/login') }
    req.logIn(user, function (err) {
      if (err) { return next(err) }
      return res.redirect('/users/' + user.username)
    })
  })(req, res, next)
}

export default cors(Login)

I'm making the assumption that you want to use Nextjs on Now V2 so that it is serverless.我假设您想在 Now V2 上使用 Nextjs,以便它是无服务器的。 If that is not the case, my answer probably won't help you.如果不是这种情况,我的回答可能对您没有帮助。

I just made a minimal repo for Auth0 and Nextjs serverless.我刚刚为 Auth0 和 Nextjs serverless 创建了一个最小的 repo。

You were on the right track realizing that Next.js Authentication Tutorial uses express to extend the Nextjs server, but since we are in serverless world the authentication needs to be abstracted to an api.您在正确的轨道上意识到Next.js 身份验证教程使用 express 扩展 Nextjs 服务器,但由于我们处于无服务器世界,身份验证需要抽象为 api。 It looks like you started that, but you didn't put a full express server in the api.看起来你开始了,但你没有在 api 中放置一个完整的快速服务器。 In my repo, my auth lambda is ./auth/auth.js.在我的 repo 中,我的 auth lambda 是 ./auth/auth.js。 For the above repo, the user data is called in the HOC located in ./utils/withAuth.js.对于上述 repo,用户数据在位于 ./utils/withAuth.js 的 HOC 中调用。

I hope that helps!我希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM