'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _regenerator = require('babel-runtime/regenerator');

var _regenerator2 = _interopRequireDefault(_regenerator);

var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');

var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);

var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');

var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _style = require('styled-jsx/style.js');

var _style2 = _interopRequireDefault(_style);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _link = require('next/dist/lib/link.js');

var _link2 = _interopRequireDefault(_link);

var _index = require('next/dist/lib/router/index.js');

var _index2 = _interopRequireDefault(_index);

var _universalCookie = require('universal-cookie');

var _universalCookie2 = _interopRequireDefault(_universalCookie);

var _nextAuthClient = require('next-auth-client');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var _jsxFileName = '/Users/iain/Development/next-auth/example/pages/auth/callback.js?entry';


var _class = function (_React$Component) {
  (0, _inherits3.default)(_class, _React$Component);

  function _class() {
    (0, _classCallCheck3.default)(this, _class);

    return (0, _possibleConstructorReturn3.default)(this, (_class.__proto__ || (0, _getPrototypeOf2.default)(_class)).apply(this, arguments));
  }

  (0, _createClass3.default)(_class, [{
    key: 'componentDidMount',
    value: function () {
      var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee() {
        var session;
        return _regenerator2.default.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                _context.next = 2;
                return _nextAuthClient.NextAuth.init({ force: true });

              case 2:
                session = _context.sent;

                _index2.default.push(this.props.redirectTo);

              case 4:
              case 'end':
                return _context.stop();
            }
          }
        }, _callee, this);
      }));

      function componentDidMount() {
        return _ref.apply(this, arguments);
      }

      return componentDidMount;
    }()
  }, {
    key: 'render',
    value: function render() {
      // Provide a link for clients without JavaScript as a fallback.
      return _react2.default.createElement(_react2.default.Fragment, {
        __source: {
          fileName: _jsxFileName,
          lineNumber: 43
        }
      }, _react2.default.createElement(_style2.default, {
        styleId: '2276436731',
        css: 'body{background-color:#fff;}.circle-loader{position:absolute;top:50%;left:50%;width:50%;z-index:100;text-align:center;-webkit-transform:translate(-50%,-50%);-ms-transform:translate(-50%,-50%);transform:translate(-50%,-50%);}.circle-loader .circle{fill:transparent;stroke:rgba(0,0,0,0.2);stroke-width:4px;-webkit-animation:dash 2s ease infinite,rotate 2s linear infinite;animation:dash 2s ease infinite,rotate 2s linear infinite;}@-webkit-keyframes dash{0%{stroke-dasharray:1,95;stroke-dashoffset:0;}50%{stroke-dasharray:85,95;stroke-dashoffset:-25;}100%{stroke-dasharray:85,95;stroke-dashoffset:-93;}}@keyframes dash{0%{stroke-dasharray:1,95;stroke-dashoffset:0;}50%{stroke-dasharray:85,95;stroke-dashoffset:-25;}100%{stroke-dasharray:85,95;stroke-dashoffset:-93;}}@-webkit-keyframes rotate{0%{-webkit-transform:rotate(0deg);-ms-transform:rotate(0deg);transform:rotate(0deg);}100%{-webkit-transform:rotate(360deg);-ms-transform:rotate(360deg);transform:rotate(360deg);}}@keyframes rotate{0%{-webkit-transform:rotate(0deg);-ms-transform:rotate(0deg);transform:rotate(0deg);}100%{-webkit-transform:rotate(360deg);-ms-transform:rotate(360deg);transform:rotate(360deg);}}\n/*# sourceMappingURL=data:application/json;charset=utf-8;base64,  */\n/*@ sourceURL=pages/auth/callback.js?entry */'
      }), _react2.default.createElement('a', { href: this.props.redirectTo, className: 'jsx-2276436731' + ' ' + 'circle-loader',
        __source: {
          fileName: _jsxFileName,
          lineNumber: 85
        }
      }, _react2.default.createElement('svg', { width: '60', height: '60', version: '1.1', xmlns: 'http://www.w3.org/2000/svg', className: 'jsx-2276436731' + ' ' + 'circle',
        __source: {
          fileName: _jsxFileName,
          lineNumber: 86
        }
      }, _react2.default.createElement('circle', { cx: '30', cy: '30', r: '15', className: 'jsx-2276436731',
        __source: {
          fileName: _jsxFileName,
          lineNumber: 87
        }
      }))), _react2.default.createElement('script', { src: 'https://cdn.polyfill.io/v2/polyfill.min.js', className: 'jsx-2276436731',
        __source: {
          fileName: _jsxFileName,
          lineNumber: 90
        }
      }));
    }
  }], [{
    key: 'getInitialProps',
    value: function () {
      var _ref3 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2(_ref2) {
        var req = _ref2.req;
        var session, cookies, redirectTo;
        return _regenerator2.default.wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                _context2.next = 2;
                return _nextAuthClient.NextAuth.init({ force: true, req: req });

              case 2:
                session = _context2.sent;
                cookies = new _universalCookie2.default(req && req.headers && req.headers.cookie ? req.headers.cookie : null);

                // If the user is signed in, we look for a redirect URL cookie and send 
                // them to that page, so that people signing in end up back on the page they
                // were on before signing in. Defaults to '/'.

                redirectTo = '/';

                if (session.user) {
                  // Read redirect URL to redirect to from cookies
                  redirectTo = cookies.get('redirect_url') || redirectTo;

                  // Allow relative paths only - strip protocol/host/port if they exist.
                  redirectTo = redirectTo.replace(/^[a-zA-Z]{3,5}\:\/{2}[a-zA-Z0-9_.:-]+\//, '');
                }

                return _context2.abrupt('return', {
                  session: session,
                  redirectTo: redirectTo
                });

              case 7:
              case 'end':
                return _context2.stop();
            }
          }
        }, _callee2, this);
      }));

      function getInitialProps(_x) {
        return _ref3.apply(this, arguments);
      }

      return getInitialProps;
    }()
  }]);

  return _class;
}(_react2.default.Component);

exports.default = _class;
//# source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