Skip to content

Commit

Permalink
Fix login bug
Browse files Browse the repository at this point in the history
  • Loading branch information
blms committed Jul 22, 2020
1 parent 67b914b commit 9b5af26
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 41 deletions.
10 changes: 6 additions & 4 deletions src/pages/api/auth/[...nextauth].js
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ const options = {
},

callbacks: {
session: async (session) => {
const { email } = session.user;
const url = `${process.env.SITE}/api/user/${email}`;
session: async (session, token) => {
const { id } = token.user;
const url = `${process.env.SITE}/api/user/${id}`;
const res = await fetch(url, {
method: 'GET',
headers: { 'Content-Type': 'application/json' },
headers: {
'Content-Type': 'application/json',
},
});
if (res.status === 200) {
const user = await res.json();
Expand Down
37 changes: 0 additions & 37 deletions src/pages/api/user/[email].js

This file was deleted.

30 changes: 30 additions & 0 deletions src/pages/api/user/[id].js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import nc from 'next-connect';
import { ObjectID } from 'mongodb';
import middleware from '../../../middlewares/middleware';

const handler = nc()
.use(middleware)
.get(
async (req, res) => {
await req.db
.collection('users')
.findOne(
{ _id: ObjectID(req.query.id) },
(err, doc) => {
if (doc) {
const {
name, firstName, lastName, affiliation,
} = doc;
if (err) throw err;
res.status(200).json({
name, firstName, lastName, affiliation,
});
} else {
res.status(404).json({ error: '404 Not Found' });
}
},
);
},
);

export default handler;

0 comments on commit 9b5af26

Please sign in to comment.