'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.SignInButtons = exports.LinkAccount = exports.LinkAccounts = undefined;

var _keys = require('babel-runtime/core-js/object/keys');

var _keys2 = _interopRequireDefault(_keys);

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

var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);

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

var _regenerator2 = _interopRequireDefault(_regenerator);

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

var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);

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

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

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

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

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

var _createClass3 = _interopRequireDefault(_createClass2);

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

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _head = require('next/dist/lib/head.js');

var _head2 = _interopRequireDefault(_head);

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

var _index2 = _interopRequireDefault(_index);

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

var _link2 = _interopRequireDefault(_link);

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/index.js?entry';


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

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

              case 2:
                _context.t0 = _context.sent;
                _context.next = 5;
                return _nextAuthClient.NextAuth.linked({ req: req });

              case 5:
                _context.t1 = _context.sent;
                _context.next = 8;
                return _nextAuthClient.NextAuth.providers({ req: req });

              case 8:
                _context.t2 = _context.sent;
                return _context.abrupt('return', {
                  session: _context.t0,
                  linkedAccounts: _context.t1,
                  providers: _context.t2
                });

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

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

      return getInitialProps;
    }()
  }]);

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

    var _this = (0, _possibleConstructorReturn3.default)(this, (_class.__proto__ || (0, _getPrototypeOf2.default)(_class)).call(this, props));

    _this.state = {
      email: '',
      session: _this.props.session
    };
    _this.handleEmailChange = _this.handleEmailChange.bind(_this);
    _this.handleSignInSubmit = _this.handleSignInSubmit.bind(_this);
    return _this;
  }

  (0, _createClass3.default)(_class, [{
    key: 'handleEmailChange',
    value: function handleEmailChange(event) {
      this.setState({
        email: event.target.value
      });
    }
  }, {
    key: 'handleSignInSubmit',
    value: function handleSignInSubmit(event) {
      var _this2 = this;

      event.preventDefault();

      if (!this.state.email) return;

      // Save current URL so user is redirected back here after signing in
      var cookies = new _universalCookie2.default();
      cookies.set('redirect_url', window.location.pathname);

      _nextAuthClient.NextAuth.signin(this.state.email).then(function () {
        _index2.default.push('/auth/check-email?email=' + _this2.state.email);
      }).catch(function (err) {
        _index2.default.push('/auth/error?action=signin&type=email&email=' + _this2.state.email);
      });
    }
  }, {
    key: 'render',
    value: function render() {
      if (this.props.session.user) {
        return _react2.default.createElement('div', { className: 'container', __source: {
            fileName: _jsxFileName,
            lineNumber: 55
          }
        }, _react2.default.createElement(_head2.default, {
          __source: {
            fileName: _jsxFileName,
            lineNumber: 56
          }
        }, _react2.default.createElement('link', { rel: 'stylesheet', href: 'https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css', integrity: 'sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm', crossOrigin: 'anonymous', __source: {
            fileName: _jsxFileName,
            lineNumber: 57
          }
        }), _react2.default.createElement('script', { src: 'https://cdn.polyfill.io/v2/polyfill.min.js', __source: {
            fileName: _jsxFileName,
            lineNumber: 58
          }
        })), _react2.default.createElement('div', { className: 'text-center', __source: {
            fileName: _jsxFileName,
            lineNumber: 60
          }
        }, _react2.default.createElement('h1', { className: 'display-4 mt-3', __source: {
            fileName: _jsxFileName,
            lineNumber: 61
          }
        }, 'NextAuth Example'), _react2.default.createElement('p', { className: 'lead mt-3 mb-1', __source: {
            fileName: _jsxFileName,
            lineNumber: 62
          }
        }, 'You are signed in as ', _react2.default.createElement('span', { className: 'font-weight-bold', __source: {
            fileName: _jsxFileName,
            lineNumber: 62
          }
        }, this.props.session.user.name || this.props.session.user.email), '.')), _react2.default.createElement('div', { className: 'row', __source: {
            fileName: _jsxFileName,
            lineNumber: 64
          }
        }, _react2.default.createElement('div', { className: 'col-sm-5 mr-auto ml-auto', __source: {
            fileName: _jsxFileName,
            lineNumber: 65
          }
        }, _react2.default.createElement(LinkAccounts, {
          session: this.props.session,
          linkedAccounts: this.props.linkedAccounts,
          __source: {
            fileName: _jsxFileName,
            lineNumber: 66
          }
        }))), _react2.default.createElement('p', { className: 'text-center', __source: {
            fileName: _jsxFileName,
            lineNumber: 72
          }
        }, _react2.default.createElement(_link2.default, { href: '/', __source: {
            fileName: _jsxFileName,
            lineNumber: 73
          }
        }, _react2.default.createElement('a', {
          __source: {
            fileName: _jsxFileName,
            lineNumber: 73
          }
        }, 'Home'))));
      } else {
        return _react2.default.createElement('div', { className: 'container', __source: {
            fileName: _jsxFileName,
            lineNumber: 79
          }
        }, _react2.default.createElement(_head2.default, {
          __source: {
            fileName: _jsxFileName,
            lineNumber: 80
          }
        }, _react2.default.createElement('link', { rel: 'stylesheet', href: 'https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css', integrity: 'sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm', crossOrigin: 'anonymous', __source: {
            fileName: _jsxFileName,
            lineNumber: 81
          }
        }), _react2.default.createElement('script', { src: 'https://cdn.polyfill.io/v2/polyfill.min.js', __source: {
            fileName: _jsxFileName,
            lineNumber: 82
          }
        })), _react2.default.createElement('div', { className: 'text-center', __source: {
            fileName: _jsxFileName,
            lineNumber: 84
          }
        }, _react2.default.createElement('h1', { className: 'display-4 mt-3 mb-3', __source: {
            fileName: _jsxFileName,
            lineNumber: 85
          }
        }, 'NextAuth Example')), _react2.default.createElement('div', { className: 'row', __source: {
            fileName: _jsxFileName,
            lineNumber: 87
          }
        }, _react2.default.createElement('div', { className: 'col-sm-6 mr-auto ml-auto', __source: {
            fileName: _jsxFileName,
            lineNumber: 88
          }
        }, _react2.default.createElement('div', { className: 'card mt-3 mb-3', __source: {
            fileName: _jsxFileName,
            lineNumber: 89
          }
        }, _react2.default.createElement('h4', { className: 'card-header', __source: {
            fileName: _jsxFileName,
            lineNumber: 90
          }
        }, 'Sign In'), _react2.default.createElement('div', { className: 'card-body pb-0', __source: {
            fileName: _jsxFileName,
            lineNumber: 91
          }
        }, _react2.default.createElement(SignInButtons, { providers: this.props.providers, __source: {
            fileName: _jsxFileName,
            lineNumber: 92
          }
        }), _react2.default.createElement('form', { id: 'signin', method: 'post', action: '/auth/email/signin', onSubmit: this.handleSignInSubmit, __source: {
            fileName: _jsxFileName,
            lineNumber: 93
          }
        }, _react2.default.createElement('input', { name: '_csrf', type: 'hidden', value: this.state.session.csrfToken, __source: {
            fileName: _jsxFileName,
            lineNumber: 94
          }
        }), _react2.default.createElement('p', {
          __source: {
            fileName: _jsxFileName,
            lineNumber: 95
          }
        }, _react2.default.createElement('label', { htmlFor: 'email', __source: {
            fileName: _jsxFileName,
            lineNumber: 96
          }
        }, 'Email address'), _react2.default.createElement('br', {
          __source: {
            fileName: _jsxFileName,
            lineNumber: 96
          }
        }), _react2.default.createElement('input', { name: 'email', type: 'text', placeholder: 'j.smith@example.com', id: 'email', className: 'form-control', value: this.state.email, onChange: this.handleEmailChange, __source: {
            fileName: _jsxFileName,
            lineNumber: 97
          }
        })), _react2.default.createElement('p', { className: 'text-right', __source: {
            fileName: _jsxFileName,
            lineNumber: 99
          }
        }, _react2.default.createElement('button', { id: 'submitButton', type: 'submit', className: 'btn btn-outline-primary', __source: {
            fileName: _jsxFileName,
            lineNumber: 100
          }
        }, 'Sign in with email'))))))), _react2.default.createElement('p', { className: 'text-center', __source: {
            fileName: _jsxFileName,
            lineNumber: 107
          }
        }, _react2.default.createElement(_link2.default, { href: '/', __source: {
            fileName: _jsxFileName,
            lineNumber: 108
          }
        }, _react2.default.createElement('a', {
          __source: {
            fileName: _jsxFileName,
            lineNumber: 108
          }
        }, 'Home'))));
      }
    }
  }]);

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

exports.default = _class;
var LinkAccounts = exports.LinkAccounts = function (_React$Component2) {
  (0, _inherits3.default)(LinkAccounts, _React$Component2);

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

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

  (0, _createClass3.default)(LinkAccounts, [{
    key: 'render',
    value: function render() {
      var _this4 = this;

      return _react2.default.createElement('div', { className: 'card mt-3 mb-3', __source: {
          fileName: _jsxFileName,
          lineNumber: 119
        }
      }, _react2.default.createElement('h4', { className: 'card-header', __source: {
          fileName: _jsxFileName,
          lineNumber: 120
        }
      }, 'Link Accounts'), _react2.default.createElement('div', { className: 'card-body pb-0', __source: {
          fileName: _jsxFileName,
          lineNumber: 121
        }
      }, (0, _keys2.default)(this.props.linkedAccounts).map(function (provider, i) {
        return _react2.default.createElement(LinkAccount, { key: i, provider: provider, session: _this4.props.session, linked: _this4.props.linkedAccounts[provider], __source: {
            fileName: _jsxFileName,
            lineNumber: 124
          }
        });
      })));
    }
  }]);

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

var LinkAccount = exports.LinkAccount = function (_React$Component3) {
  (0, _inherits3.default)(LinkAccount, _React$Component3);

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

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

  (0, _createClass3.default)(LinkAccount, [{
    key: 'render',
    value: function render() {
      if (this.props.linked === true) {
        return _react2.default.createElement('form', { method: 'post', action: '/auth/oauth/' + this.props.provider.toLowerCase() + '/unlink', __source: {
            fileName: _jsxFileName,
            lineNumber: 137
          }
        }, _react2.default.createElement('input', { name: '_csrf', type: 'hidden', value: this.props.session.csrfToken, __source: {
            fileName: _jsxFileName,
            lineNumber: 138
          }
        }), _react2.default.createElement('p', {
          __source: {
            fileName: _jsxFileName,
            lineNumber: 139
          }
        }, _react2.default.createElement('button', { className: 'btn btn-block btn-outline-danger', type: 'submit', __source: {
            fileName: _jsxFileName,
            lineNumber: 140
          }
        }, 'Unlink from ', this.props.provider)));
      } else {
        return _react2.default.createElement('p', {
          __source: {
            fileName: _jsxFileName,
            lineNumber: 148
          }
        }, _react2.default.createElement('a', { className: 'btn btn-block btn-outline-primary', href: '/auth/oauth/' + this.props.provider.toLowerCase(), __source: {
            fileName: _jsxFileName,
            lineNumber: 149
          }
        }, 'Link with ', this.props.provider));
      }
    }
  }]);

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

var SignInButtons = exports.SignInButtons = function (_React$Component4) {
  (0, _inherits3.default)(SignInButtons, _React$Component4);

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

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

  (0, _createClass3.default)(SignInButtons, [{
    key: 'render',
    value: function render() {
      var _this7 = this;

      return _react2.default.createElement(_react2.default.Fragment, {
        __source: {
          fileName: _jsxFileName,
          lineNumber: 161
        }
      }, (0, _keys2.default)(this.props.providers).map(function (provider, i) {
        return _react2.default.createElement('p', {
          __source: {
            fileName: _jsxFileName,
            lineNumber: 165
          }
        }, _react2.default.createElement('a', { className: 'btn btn-block btn-outline-secondary', href: _this7.props.providers[provider].signin, __source: {
            fileName: _jsxFileName,
            lineNumber: 166
          }
        }, 'Sign in with ', provider));
      }));
    }
  }]);

  return SignInButtons;
}(_react2.default.Component);
//# sourceMappingURL=data:application/json;charset=utf-8;base64, 