{"size": 715, "ext": "js", "max_stars_repo_path": "06-nextjs-auth/02-finish/orbit/pages/api/user.js", "max_stars_repo_name": "trblackw/advanced-react-security", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "06-nextjs-auth/02-finish/orbit/pages/api/user.js", "max_issues_repo_name": "trblackw/advanced-react-security", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "06-nextjs-auth/02-finish/orbit/pages/api/user.js", "max_forks_repo_name": "trblackw/advanced-react-security", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 18.3333333333, "max_line_length": 56, "alphanum_fraction": 0.5888111888}
import nextConnect from 'next-connect';
import database from './middleware/database';
import authorize from './middleware/authorize';
import { ObjectId } from 'mongodb';

const handler = nextConnect();
handler.use(database);
handler.use(authorize);

handler.get(async (req, res) => {
  try {
    const data = await req.db
      .collection('users')
      .findOne({ _id: ObjectId(req.user.sub) });

    const {
      _id,
      firstName,
      lastName,
      email,
      role,
      exp
    } = data;

    res.json({
      _id,
      firstName,
      lastName,
      email,
      role,
      exp
    });
  } catch (err) {
    res.status(401).json({ message: 'Not authorized' });
  }
});

export default handler;
