{"size": 705, "ext": "js", "max_stars_repo_path": "auth/auth.js", "max_stars_repo_name": "RajapandiR/Graphqlserver", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "auth/auth.js", "max_issues_repo_name": "RajapandiR/Graphqlserver", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "auth/auth.js", "max_forks_repo_name": "RajapandiR/Graphqlserver", "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": 28.2, "max_line_length": 62, "alphanum_fraction": 0.6269503546}
import { SECRET } from '../config';
import User  from "../models/user";
import jwt from 'jsonwebtoken';

import { AuthenticationError } from 'apollo-server-express';

const getAuthUser = async (req) => {
	const header = req.headers.authorization;
    if (header) {
      // const token = header.replace("Bearer ", "");
      const token = jwt.verify(header, SECRET);
      let user = await User.find({_id:token.userId});
      // console.log("User", user);
      if (!user) {
      throw new AuthenticationError("Invalid user.");
      }
      return user;
    }
    if (requireAuth) {
      throw new AuthenticationError("You must be logged in.");
    }
    return null;
  };

export default getAuthUser;