Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
20f64296
Unverified
Commit
20f64296
authored
Apr 13, 2020
by
Luis Alvarez D
Committed by
GitHub
Apr 13, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Replaced express with next-connect (#11747)
parent
ee372f47
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
18 additions
and
23 deletions
+18
-23
package.json
examples/with-passport/package.json
+1
-1
login.js
examples/with-passport/pages/api/login.js
+17
-22
No files found.
examples/with-passport/package.json
View file @
20f64296
...
...
@@ -8,8 +8,8 @@
"dependencies"
:
{
"@hapi/iron"
:
"6.0.0"
,
"cookie"
:
"0.4.0"
,
"express"
:
"4.17.1"
,
"next"
:
"latest"
,
"next-connect"
:
"0.6.1"
,
"passport"
:
"0.4.1"
,
"passport-local"
:
"1.0.0"
,
"react"
:
"latest"
,
...
...
examples/with-passport/pages/api/login.js
View file @
20f64296
import
express
from
'express'
import
passport
from
'passport'
import
nextConnect
from
'next-connect'
import
{
localStrategy
}
from
'../../lib/password-local'
import
{
encryptSession
}
from
'../../lib/iron'
import
{
setTokenCookie
}
from
'../../lib/auth-cookies'
const
app
=
express
()
const
authenticate
=
(
method
,
req
,
res
)
=>
new
Promise
((
resolve
,
reject
)
=>
{
passport
.
authenticate
(
method
,
{
session
:
false
},
(
error
,
token
)
=>
{
...
...
@@ -16,26 +15,22 @@ const authenticate = (method, req, res) =>
})(
req
,
res
)
})
app
.
disable
(
'x-powered-by'
)
app
.
use
(
passport
.
initialize
())
passport
.
use
(
localStrategy
)
app
.
post
(
'/api/login'
,
async
(
req
,
res
)
=>
{
try
{
const
user
=
await
authenticate
(
'local'
,
req
,
res
)
// session is the payload to save in the token, it may contain basic info about the user
const
session
=
{
...
user
}
// The token is a string with the encrypted session
const
token
=
await
encryptSession
(
session
)
export
default
nextConnect
()
.
use
(
passport
.
initialize
())
.
post
(
async
(
req
,
res
)
=>
{
try
{
const
user
=
await
authenticate
(
'local'
,
req
,
res
)
// session is the payload to save in the token, it may contain basic info about the user
const
session
=
{
...
user
}
// The token is a string with the encrypted session
const
token
=
await
encryptSession
(
session
)
setTokenCookie
(
res
,
token
)
res
.
status
(
200
).
send
({
done
:
true
})
}
catch
(
error
)
{
console
.
error
(
error
)
res
.
status
(
401
).
send
(
error
.
message
)
}
})
export
default
app
setTokenCookie
(
res
,
token
)
res
.
status
(
200
).
send
({
done
:
true
})
}
catch
(
error
)
{
console
.
error
(
error
)
res
.
status
(
401
).
send
(
error
.
message
)
}
})
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment